Skip to content

Commit d2906ab

Browse files
authored
Merge pull request #335 from saikat-royc/iss-333
Call dynamic VS content creation unconditionally
2 parents 0c5b535 + 73697eb commit d2906ab

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

pkg/common-controller/framework_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,14 @@ func testUpdateSnapshotClass(ctrl *csiSnapshotCommonController, reactor *snapsho
11451145
return err
11461146
}
11471147

1148+
func testNewSnapshotContentCreation(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
1149+
if err := ctrl.syncUnreadySnapshot(test.initialSnapshots[0]); err != nil {
1150+
return fmt.Errorf("syncUnreadySnapshot failed: %v", err)
1151+
}
1152+
1153+
return nil
1154+
}
1155+
11481156
var (
11491157
classEmpty string
11501158
classGold = "gold"

pkg/common-controller/snapshot_controller.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -453,25 +453,25 @@ func (ctrl *csiSnapshotCommonController) syncUnreadySnapshot(snapshot *crdv1.Vol
453453
}
454454
klog.V(5).Infof("bindandUpdateVolumeSnapshot %v", newSnapshot)
455455
return nil
456-
} else if snapshot.Status == nil || snapshot.Status.Error == nil || isControllerUpdateFailError(snapshot.Status.Error) {
457-
if snapshot.Spec.Source.PersistentVolumeClaimName == nil {
458-
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotPVCSourceMissing", fmt.Sprintf("PVC source for snapshot %s is missing", uniqueSnapshotName))
459-
return fmt.Errorf("expected PVC source for snapshot %s but got nil", uniqueSnapshotName)
460-
}
461-
var err error
462-
var content *crdv1.VolumeSnapshotContent
463-
if content, err = ctrl.createSnapshotContent(snapshot); err != nil {
464-
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotContentCreationFailed", fmt.Sprintf("Failed to create snapshot content with error %v", err))
465-
return err
466-
}
456+
}
467457

468-
// Update snapshot status with BoundVolumeSnapshotContentName
469-
klog.V(5).Infof("syncUnreadySnapshot [%s]: trying to update snapshot status", utils.SnapshotKey(snapshot))
470-
if _, err = ctrl.updateSnapshotStatus(snapshot, content); err != nil {
471-
// update snapshot status failed
472-
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotStatusUpdateFailed", fmt.Sprintf("Snapshot status update failed, %v", err))
473-
return err
474-
}
458+
// If we reach here, it is a dynamically provisioned snapshot, and the volumeSnapshotContent object is not yet created.
459+
if snapshot.Spec.Source.PersistentVolumeClaimName == nil {
460+
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotPVCSourceMissing", fmt.Sprintf("PVC source for snapshot %s is missing", uniqueSnapshotName))
461+
return fmt.Errorf("expected PVC source for snapshot %s but got nil", uniqueSnapshotName)
462+
}
463+
var content *crdv1.VolumeSnapshotContent
464+
if content, err = ctrl.createSnapshotContent(snapshot); err != nil {
465+
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotContentCreationFailed", fmt.Sprintf("Failed to create snapshot content with error %v", err))
466+
return err
467+
}
468+
469+
// Update snapshot status with BoundVolumeSnapshotContentName
470+
klog.V(5).Infof("syncUnreadySnapshot [%s]: trying to update snapshot status", utils.SnapshotKey(snapshot))
471+
if _, err = ctrl.updateSnapshotStatus(snapshot, content); err != nil {
472+
// update snapshot status failed
473+
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotStatusUpdateFailed", fmt.Sprintf("Snapshot status update failed, %v", err))
474+
return err
475475
}
476476
return nil
477477
}

pkg/common-controller/snapshot_update_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,48 @@ func TestSync(t *testing.T) {
510510
expectSuccess: true,
511511
test: testUpdateSnapshotErrorStatus,
512512
},
513+
{
514+
// Snapshot status nil, no initial content, new content should be created.
515+
name: "8-1 - Snapshot status nil, no initial snapshot content, new content should be created",
516+
initialContents: nocontents,
517+
expectedContents: withContentAnnotations(newContentArrayNoStatus("snapcontent-snapuid8-1", "snapuid8-1", "snap8-1", "sid8-1", validSecretClass, "", "pv-handle8-1", deletionPolicy, nil, nil, false, false), map[string]string{utils.AnnDeletionSecretRefName: "secret", utils.AnnDeletionSecretRefNamespace: "default"}),
518+
initialSnapshots: newSnapshotArray("snap8-1", "snapuid8-1", "claim8-1", "", validSecretClass, "", nil, nil, nil, nil, true, false, nil),
519+
expectedSnapshots: newSnapshotArray("snap8-1", "snapuid8-1", "claim8-1", "", validSecretClass, "snapcontent-snapuid8-1", &False, nil, nil, nil, false, false, nil),
520+
initialClaims: newClaimArray("claim8-1", "pvc-uid8-1", "1Gi", "volume8-1", v1.ClaimBound, &classEmpty),
521+
initialVolumes: newVolumeArray("volume8-1", "pv-uid8-1", "pv-handle8-1", "1Gi", "pvc-uid8-1", "claim8-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
522+
initialSecrets: []*v1.Secret{secret()},
523+
errors: noerrors,
524+
expectSuccess: true,
525+
test: testNewSnapshotContentCreation,
526+
},
527+
{
528+
// Snapshot status with nil error, no initial content, new content should be created.
529+
name: "8-2 - Snapshot status with nil error, no initial snapshot content, new content should be created",
530+
initialContents: nocontents,
531+
expectedContents: withContentAnnotations(newContentArrayNoStatus("snapcontent-snapuid8-2", "snapuid8-2", "snap8-2", "sid8-2", validSecretClass, "", "pv-handle8-2", deletionPolicy, nil, nil, false, false), map[string]string{utils.AnnDeletionSecretRefName: "secret", utils.AnnDeletionSecretRefNamespace: "default"}),
532+
initialSnapshots: newSnapshotArray("snap8-2", "snapuid8-2", "claim8-2", "", validSecretClass, "", nil, nil, nil, nil, false, false, nil),
533+
expectedSnapshots: newSnapshotArray("snap8-2", "snapuid8-2", "claim8-2", "", validSecretClass, "snapcontent-snapuid8-2", &False, nil, nil, nil, false, false, nil),
534+
initialClaims: newClaimArray("claim8-2", "pvc-uid8-2", "1Gi", "volume8-2", v1.ClaimBound, &classEmpty),
535+
initialVolumes: newVolumeArray("volume8-2", "pv-uid8-2", "pv-handle8-2", "1Gi", "pvc-uid8-2", "claim8-2", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
536+
initialSecrets: []*v1.Secret{secret()},
537+
errors: noerrors,
538+
expectSuccess: true,
539+
test: testNewSnapshotContentCreation,
540+
},
541+
{
542+
// Snapshot status with error, no initial content, new content should be created, snapshot error should be cleared.
543+
name: "8-3 - Snapshot status with error, no initial content, new content should be created, snapshot error should be cleared",
544+
initialContents: nocontents,
545+
expectedContents: withContentAnnotations(newContentArrayNoStatus("snapcontent-snapuid8-3", "snapuid8-3", "snap8-3", "sid8-3", validSecretClass, "", "pv-handle8-3", deletionPolicy, nil, nil, false, false), map[string]string{utils.AnnDeletionSecretRefName: "secret", utils.AnnDeletionSecretRefNamespace: "default"}),
546+
initialSnapshots: newSnapshotArray("snap8-3", "snapuid8-3", "claim8-3", "", validSecretClass, "", nil, nil, nil, snapshotErr, false, false, nil),
547+
expectedSnapshots: newSnapshotArray("snap8-3", "snapuid8-3", "claim8-3", "", validSecretClass, "snapcontent-snapuid8-3", &False, nil, nil, nil, false, false, nil),
548+
initialClaims: newClaimArray("claim8-3", "pvc-uid8-3", "1Gi", "volume8-3", v1.ClaimBound, &classEmpty),
549+
initialVolumes: newVolumeArray("volume8-3", "pv-uid8-3", "pv-handle8-3", "1Gi", "pvc-uid8-3", "claim8-3", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty),
550+
initialSecrets: []*v1.Secret{secret()},
551+
errors: noerrors,
552+
expectSuccess: true,
553+
test: testNewSnapshotContentCreation,
554+
},
513555
}
514556

515557
runSyncTests(t, tests, snapshotClasses)

0 commit comments

Comments
 (0)