Skip to content

Commit

Permalink
Merge pull request #831 from andyzhangx/fix-delete-error-archived
Browse files Browse the repository at this point in the history
fix: delete volume error in archive deletion mode
  • Loading branch information
andyzhangx authored Sep 1, 2024
2 parents b56ca0c + 0cd70c4 commit 54c6421
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/smb/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
)

const (
Expand Down Expand Up @@ -98,6 +99,11 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}
}

if acquired := d.volumeLocks.TryAcquire(name); !acquired {
return nil, status.Errorf(codes.Aborted, volumeOperationAlreadyExistsFmt, name)
}
defer d.volumeLocks.Release(name)

if createSubDir {
// Mount smb base share so we can create a subdirectory
if err := d.internalMount(ctx, smbVol, volCap, secrets); err != nil {
Expand Down Expand Up @@ -147,6 +153,11 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
return &csi.DeleteVolumeResponse{}, nil
}

if acquired := d.volumeLocks.TryAcquire(volumeID); !acquired {
return nil, status.Errorf(codes.Aborted, volumeOperationAlreadyExistsFmt, volumeID)
}
defer d.volumeLocks.Release(volumeID)

var volCap *csi.VolumeCapability
secrets := req.GetSecrets()
mountOptions := getMountOptions(secrets)
Expand All @@ -167,6 +178,16 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)

if len(req.GetSecrets()) > 0 && !strings.EqualFold(smbVol.onDelete, retain) {
klog.V(2).Infof("begin to delete or archive subdirectory since secret is provided")
// check whether volumeID is in the cache
cache, err := d.volDeletionCache.Get(volumeID, azcache.CacheReadTypeDefault)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
if cache != nil {
klog.V(2).Infof("DeleteVolume: volume %s is already deleted", volumeID)
return &csi.DeleteVolumeResponse{}, nil
}

// mount smb base share so we can delete or archive the subdirectory
if err = d.internalMount(ctx, smbVol, volCap, secrets); err != nil {
return nil, status.Errorf(codes.Internal, "failed to mount smb server: %v", err.Error())
Expand Down Expand Up @@ -211,6 +232,7 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
klog.V(2).Infof("DeleteVolume(%s) does not delete subdirectory", volumeID)
}

d.volDeletionCache.Set(volumeID, "")
return &csi.DeleteVolumeResponse{}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type Driver struct {
enableGetVolumeStats bool
// a timed cache storing volume stats <volumeID, volumeStats>
volStatsCache azcache.Resource
// a timed cache storing volume deletion records <volumeID, "">
volDeletionCache azcache.Resource
// this only applies to Windows node
removeSMBMappingDuringUnmount bool
krb5CacheDirectory string
Expand Down Expand Up @@ -120,6 +122,9 @@ func NewDriver(options *DriverOptions) *Driver {
if driver.volStatsCache, err = azcache.NewTimedCache(time.Duration(options.VolStatsCacheExpireInMinutes)*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if driver.volDeletionCache, err = azcache.NewTimedCache(time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
return &driver
}

Expand Down

0 comments on commit 54c6421

Please sign in to comment.