Skip to content

Commit

Permalink
chore: append subsid in snapshot id
Browse files Browse the repository at this point in the history
fix

fix

fix

refine
  • Loading branch information
andyzhangx committed Dec 10, 2024
1 parent f3d3f5a commit 091e046
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/azurefile/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,14 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
return nil, status.Error(codes.InvalidArgument, "CreateSnapshot Source Volume ID must be provided")
}

rgName, accountName, fileShareName, _, _, subsID, err := GetFileShareInfo(sourceVolumeID) //nolint:dogsled
rgName, accountName, fileShareName, _, _, srcVolSubsID, err := GetFileShareInfo(sourceVolumeID) //nolint:dogsled
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("GetFileShareInfo(%s) failed with error: %v", sourceVolumeID, err))
}
if rgName == "" {
rgName = d.cloud.ResourceGroup
}
subsID := srcVolSubsID
if subsID == "" {
subsID = d.cloud.SubscriptionID
}
Expand Down Expand Up @@ -902,7 +903,7 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
SizeBytes: volumehelper.GiBToBytes(int64(itemSnapshotQuota)),
SnapshotId: sourceVolumeID + "#" + itemSnapshot,
SnapshotId: getSnapshotID(srcVolSubsID, sourceVolumeID, itemSnapshot, subsID),
SourceVolumeId: sourceVolumeID,
CreationTime: timestamppb.New(itemSnapshotTime),
// Since the snapshot of azurefile has no field of ReadyToUse, here ReadyToUse is always set to true.
Expand Down Expand Up @@ -965,10 +966,11 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
d.getFileShareSizeCache.Set(key, itemSnapshotQuota)
}
}

createResp := &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
SizeBytes: volumehelper.GiBToBytes(int64(itemSnapshotQuota)),
SnapshotId: sourceVolumeID + "#" + itemSnapshot,
SnapshotId: getSnapshotID(srcVolSubsID, sourceVolumeID, itemSnapshot, subsID),
SourceVolumeId: sourceVolumeID,
CreationTime: timestamppb.New(itemSnapshotTime),
// Since the snapshot of azurefile has no field of ReadyToUse, here ReadyToUse is always set to true.
Expand Down
14 changes: 14 additions & 0 deletions pkg/azurefile/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,17 @@ func isConfidentialRuntimeClass(ctx context.Context, kubeClient clientset.Interf
klog.Infof("runtimeClass %s handler: %s", runtimeClassName, runtimeClass.Handler)
return runtimeClass.Handler == confidentialRuntimeClassHandler, nil
}

// getSnapshotID returns snapshotID based on srcVolSubsID, srcVolumeID, itemSnapshot and subsID
func getSnapshotID(srcVolSubsID, srcVolumeID, itemSnapshot, subsID string) string {
snapshotID := srcVolumeID + "#" + itemSnapshot
if srcVolSubsID == "" {
// if srcVolumeID does not contain subscription id, append it to snapshotID
if strings.HasSuffix(srcVolumeID, "#") {
snapshotID = srcVolumeID + subsID + "#" + itemSnapshot
} else {
snapshotID = srcVolumeID + "#" + subsID + "#" + itemSnapshot
}
}
return snapshotID
}
42 changes: 42 additions & 0 deletions pkg/azurefile/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,45 @@ func TestIsConfidentialRuntimeClass(t *testing.T) {
t.Fatalf("expected an error, got nil")
}
}

func TestGetSnapshotID(t *testing.T) {
tests := []struct {
desc string
srcVolumeSubsID string
srcVolumeID string
itemSnapshot string
subsID string
expected string
}{
{
desc: "srcVolumeSubsID contains subsID",
srcVolumeSubsID: "subsID",
srcVolumeID: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID",
itemSnapshot: "snapshot",
expected: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID#snapshot",
},
{
desc: "srcVolumeSubsID is empty",
srcVolumeSubsID: "",
srcVolumeID: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace",
itemSnapshot: "snapshot",
subsID: "subsID",
expected: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID#snapshot",
},
{
desc: "srcVolumeSubsID is empty but contains # at the end",
srcVolumeSubsID: "",
srcVolumeID: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#",
itemSnapshot: "snapshot",
subsID: "subsID",
expected: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID#snapshot",
},
}

for _, test := range tests {
result := getSnapshotID(test.srcVolumeSubsID, test.srcVolumeID, test.itemSnapshot, test.subsID)
if result != test.expected {
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, result, test.expected)
}
}
}

0 comments on commit 091e046

Please sign in to comment.