Skip to content

Commit caa29aa

Browse files
authored
fix createSnapshotWithTransaction workflow (#3597)
1 parent 7de3721 commit caa29aa

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

pkg/common/cns-lib/volume/manager.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,20 +2795,29 @@ func (m *defaultManager) createSnapshotWithImprovedIdempotencyCheck(ctx context.
27952795
// This function ensures that no orphaned snapshots are left behind on the vSphere backend
27962796
// in case of failures during the snapshot creation process
27972797
func (m *defaultManager) createSnapshotWithTransaction(ctx context.Context, volumeID string,
2798-
snapshotID string, extraParams interface{}) (*CnsSnapshotInfo, string, error) {
2798+
snapshotName string, extraParams interface{}) (*CnsSnapshotInfo, string, error) {
27992799
log := logger.GetLogger(ctx)
28002800
var (
28012801
// Reference to the CreateSnapshot task on CNS.
28022802
createSnapshotsTask *object.Task
28032803
// Name of the CnsVolumeOperationRequest instance.
2804-
instanceName = snapshotID + "-" + volumeID
2804+
instanceName = snapshotName + "-" + volumeID
28052805
// Local instance of CreateSnapshot details that needs to be persisted.
28062806
volumeOperationDetails *cnsvolumeoperationrequest.VolumeOperationRequestDetails
28072807
// error
28082808
err error
28092809
quotaInfo *cnsvolumeoperationrequest.QuotaDetails
28102810
isStorageQuotaM2FSSEnabled bool
28112811
)
2812+
// By default, external-snapshotter sets the snapshot name prefix to "snapshot-".
2813+
// This logic will break if the prefix configuration is changed.
2814+
// In Supervisor deployments, we assume this configuration remains unchanged by admin/DevOps.
2815+
// In Vanilla deployments, we publish the deployment manifest with the default configuration to ensure consistency.
2816+
if !strings.HasPrefix(snapshotName, "snapshot-") {
2817+
return nil, csifault.CSIInternalFault,
2818+
logger.LogNewErrorf(log, "invalid snapshotName %q: must start with 'snapshot-'", snapshotName)
2819+
}
2820+
snapshotID := strings.TrimPrefix(snapshotName, "snapshot-")
28122821
if extraParams != nil {
28132822
createSnapParams, ok := extraParams.(*CreateSnapshotExtraParams)
28142823
if !ok {
@@ -2888,8 +2897,20 @@ func (m *defaultManager) createSnapshotWithTransaction(ctx context.Context, volu
28882897
"from vCenter %q with err: %v", m.virtualCenter.Config.Host, err)
28892898
}
28902899
log.Infof("CreateSnapshots: VolumeID: %q, opId: %q", volumeID, createSnapshotsTaskInfo.ActivationId)
2891-
2892-
snapshotCreateResult := interface{}(createSnapshotsTaskInfo).(*cnstypes.CnsSnapshotCreateResult)
2900+
createSnapshotsTaskResult, err := cns.GetTaskResult(ctx, createSnapshotsTaskInfo)
2901+
if err != nil || createSnapshotsTaskResult == nil {
2902+
return nil, "", logger.LogNewErrorf(log, "unable to find the task result for CreateSnapshots task: %q "+
2903+
"from vCenter %q with err: %v", createSnapshotsTaskInfo.Task.Value, m.virtualCenter.Config.Host, err)
2904+
}
2905+
snapshotCreateResult, ok := createSnapshotsTaskResult.(*cnstypes.CnsSnapshotCreateResult)
2906+
if !ok || snapshotCreateResult == nil {
2907+
return nil, "", logger.LogNewErrorf(log,
2908+
"invalid task result: got %T with value %+v", createSnapshotsTaskResult, createSnapshotsTaskResult)
2909+
}
2910+
if snapshotCreateResult.Fault != nil {
2911+
return nil, "", logger.LogNewErrorf(log, "failed to create snapshot %q on volume %q with fault: %+v",
2912+
instanceName, volumeID, snapshotCreateResult.Fault)
2913+
}
28932914
cnsSnapshotInfo := &CnsSnapshotInfo{
28942915
SnapshotID: snapshotCreateResult.Snapshot.SnapshotId.Id,
28952916
SourceVolumeID: snapshotCreateResult.Snapshot.VolumeId.Id,
@@ -2947,14 +2968,15 @@ func (m *defaultManager) CreateSnapshot(
29472968
}
29482969
}
29492970
if createSnapParams != nil && createSnapParams.IsCSITransactionSupportEnabled {
2950-
var snapcontentPrefix = "snapcontent-"
29512971
cnssnapshotInfo, fault, err := m.createSnapshotWithTransaction(ctx, volumeID,
2952-
strings.TrimPrefix(snapshotName, snapcontentPrefix), extraParams)
2972+
snapshotName, extraParams)
29532973
if err != nil {
29542974
if IsNotSupportedFaultType(ctx, fault) {
29552975
log.Infof("Creating Snapshot with Transaction is not supported. " +
29562976
"Re-creating Snapshot without setting Snapshot ID in the spec")
29572977
return m.createSnapshotWithImprovedIdempotencyCheck(ctx, volumeID, snapshotName, extraParams)
2978+
} else {
2979+
return nil, logger.LogNewErrorf(log, "failed to create snapshot. error :%+v", err)
29582980
}
29592981
}
29602982
return cnssnapshotInfo, nil

0 commit comments

Comments
 (0)