Skip to content

fix: archive dir error#2 #735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pkg/nfs/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
return nil, status.Errorf(codes.Internal, "failed to mount nfs server: %v", err.Error())
}
defer func() {
// make sure archiving is completed before unmounting
time.Sleep(time.Second * 2)
if err = cs.internalUnmount(ctx, nfsVol); err != nil {
klog.Warningf("failed to unmount nfs server: %v", err.Error())
}
Expand All @@ -263,6 +261,11 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
if err = os.Rename(internalVolumePath, archivedInternalVolumePath); err != nil {
return nil, status.Errorf(codes.Internal, "archive subdirectory(%s, %s) failed with %v", internalVolumePath, archivedInternalVolumePath, err.Error())
}
// make sure internalVolumePath does not exist with 1 minute timeout
if err = waitForPathNotExistWithTimeout(internalVolumePath, time.Minute); err != nil {
return nil, status.Errorf(codes.Internal, "DeleteVolume: internalVolumePath(%s) still exists after 1 minute", internalVolumePath)
}
klog.V(2).Infof("archived subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath)
} else {
// delete subdirectory under base-dir
klog.V(2).Infof("removing subdirectory at %v", internalVolumePath)
Expand Down Expand Up @@ -365,12 +368,12 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS

srcPath := getInternalVolumePath(cs.Driver.workingMountDir, srcVol)
dstPath := filepath.Join(snapInternalVolPath, snapshot.archiveName())
klog.V(2).Infof("archiving %v -> %v", srcPath, dstPath)
klog.V(2).Infof("tar %v -> %v", srcPath, dstPath)
out, err := exec.Command("tar", "-C", srcPath, "-czvf", dstPath, ".").CombinedOutput()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create archive for snapshot: %v: %v", err, string(out))
}
klog.V(2).Infof("archived %s -> %s", srcPath, dstPath)
klog.V(2).Infof("tar %s -> %s complete", srcPath, dstPath)

var snapshotSize int64
fi, err := os.Stat(dstPath)
Expand Down
19 changes: 19 additions & 0 deletions pkg/nfs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"strings"
"sync"
"time"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
Expand Down Expand Up @@ -196,3 +197,21 @@ func setKeyValueInMap(m map[string]string, key, value string) {
}
m[key] = value
}

func waitForPathNotExistWithTimeout(path string, timeout time.Duration) error {
// Loop until the path no longer exists or the timeout is reached
timeoutTime := time.Now().Add(time.Duration(timeout) * time.Second)
for {
if _, err := os.Lstat(path); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}

if time.Now().After(timeoutTime) {
return fmt.Errorf("time out waiting for path %s not exist", path)
}
time.Sleep(500 * time.Microsecond)
}
}
30 changes: 30 additions & 0 deletions pkg/nfs/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"strings"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -357,3 +358,32 @@ func TestValidateOnDeleteValue(t *testing.T) {
}
}
}

func TestWaitForPathNotExistWithTimeout(t *testing.T) {
tests := []struct {
desc string
path string
timeout int
expected error
}{
{
desc: "path does not exist",
path: "non-existent-path",
timeout: 1,
expected: nil,
},
{
desc: "path exists",
path: "/",
timeout: 2,
expected: fmt.Errorf("time out waiting for path / not exist"),
},
}

for _, test := range tests {
err := waitForPathNotExistWithTimeout(test.path, time.Duration(test.timeout))
if !reflect.DeepEqual(err, test.expected) {
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, err, test.expected)
}
}
}
Loading