Skip to content

Commit 091e046

Browse files
committed
chore: append subsid in snapshot id
fix fix fix refine
1 parent f3d3f5a commit 091e046

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

pkg/azurefile/controllerserver.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -863,13 +863,14 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
863863
return nil, status.Error(codes.InvalidArgument, "CreateSnapshot Source Volume ID must be provided")
864864
}
865865

866-
rgName, accountName, fileShareName, _, _, subsID, err := GetFileShareInfo(sourceVolumeID) //nolint:dogsled
866+
rgName, accountName, fileShareName, _, _, srcVolSubsID, err := GetFileShareInfo(sourceVolumeID) //nolint:dogsled
867867
if err != nil {
868868
return nil, status.Error(codes.Internal, fmt.Sprintf("GetFileShareInfo(%s) failed with error: %v", sourceVolumeID, err))
869869
}
870870
if rgName == "" {
871871
rgName = d.cloud.ResourceGroup
872872
}
873+
subsID := srcVolSubsID
873874
if subsID == "" {
874875
subsID = d.cloud.SubscriptionID
875876
}
@@ -902,7 +903,7 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
902903
return &csi.CreateSnapshotResponse{
903904
Snapshot: &csi.Snapshot{
904905
SizeBytes: volumehelper.GiBToBytes(int64(itemSnapshotQuota)),
905-
SnapshotId: sourceVolumeID + "#" + itemSnapshot,
906+
SnapshotId: getSnapshotID(srcVolSubsID, sourceVolumeID, itemSnapshot, subsID),
906907
SourceVolumeId: sourceVolumeID,
907908
CreationTime: timestamppb.New(itemSnapshotTime),
908909
// Since the snapshot of azurefile has no field of ReadyToUse, here ReadyToUse is always set to true.
@@ -965,10 +966,11 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
965966
d.getFileShareSizeCache.Set(key, itemSnapshotQuota)
966967
}
967968
}
969+
968970
createResp := &csi.CreateSnapshotResponse{
969971
Snapshot: &csi.Snapshot{
970972
SizeBytes: volumehelper.GiBToBytes(int64(itemSnapshotQuota)),
971-
SnapshotId: sourceVolumeID + "#" + itemSnapshot,
973+
SnapshotId: getSnapshotID(srcVolSubsID, sourceVolumeID, itemSnapshot, subsID),
972974
SourceVolumeId: sourceVolumeID,
973975
CreationTime: timestamppb.New(itemSnapshotTime),
974976
// Since the snapshot of azurefile has no field of ReadyToUse, here ReadyToUse is always set to true.

pkg/azurefile/utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,17 @@ func isConfidentialRuntimeClass(ctx context.Context, kubeClient clientset.Interf
340340
klog.Infof("runtimeClass %s handler: %s", runtimeClassName, runtimeClass.Handler)
341341
return runtimeClass.Handler == confidentialRuntimeClassHandler, nil
342342
}
343+
344+
// getSnapshotID returns snapshotID based on srcVolSubsID, srcVolumeID, itemSnapshot and subsID
345+
func getSnapshotID(srcVolSubsID, srcVolumeID, itemSnapshot, subsID string) string {
346+
snapshotID := srcVolumeID + "#" + itemSnapshot
347+
if srcVolSubsID == "" {
348+
// if srcVolumeID does not contain subscription id, append it to snapshotID
349+
if strings.HasSuffix(srcVolumeID, "#") {
350+
snapshotID = srcVolumeID + subsID + "#" + itemSnapshot
351+
} else {
352+
snapshotID = srcVolumeID + "#" + subsID + "#" + itemSnapshot
353+
}
354+
}
355+
return snapshotID
356+
}

pkg/azurefile/utils_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,45 @@ func TestIsConfidentialRuntimeClass(t *testing.T) {
814814
t.Fatalf("expected an error, got nil")
815815
}
816816
}
817+
818+
func TestGetSnapshotID(t *testing.T) {
819+
tests := []struct {
820+
desc string
821+
srcVolumeSubsID string
822+
srcVolumeID string
823+
itemSnapshot string
824+
subsID string
825+
expected string
826+
}{
827+
{
828+
desc: "srcVolumeSubsID contains subsID",
829+
srcVolumeSubsID: "subsID",
830+
srcVolumeID: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID",
831+
itemSnapshot: "snapshot",
832+
expected: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID#snapshot",
833+
},
834+
{
835+
desc: "srcVolumeSubsID is empty",
836+
srcVolumeSubsID: "",
837+
srcVolumeID: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace",
838+
itemSnapshot: "snapshot",
839+
subsID: "subsID",
840+
expected: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID#snapshot",
841+
},
842+
{
843+
desc: "srcVolumeSubsID is empty but contains # at the end",
844+
srcVolumeSubsID: "",
845+
srcVolumeID: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#",
846+
itemSnapshot: "snapshot",
847+
subsID: "subsID",
848+
expected: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID#snapshot",
849+
},
850+
}
851+
852+
for _, test := range tests {
853+
result := getSnapshotID(test.srcVolumeSubsID, test.srcVolumeID, test.itemSnapshot, test.subsID)
854+
if result != test.expected {
855+
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, result, test.expected)
856+
}
857+
}
858+
}

0 commit comments

Comments
 (0)