Skip to content

Commit d14e2ee

Browse files
author
Grant Griffiths
committed
Use patch for snapshot-controller when there are no finalizers
- Also address PR feedback re: avoid a deepCopy for annotations patch Signed-off-by: Grant Griffiths <[email protected]>
1 parent 0ccf801 commit d14e2ee

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

pkg/common-controller/framework_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,6 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
354354

355355
storedVer, _ := strconv.Atoi(storedSnapshot.ResourceVersion)
356356
storedSnapshot.ResourceVersion = strconv.Itoa(storedVer + 1)
357-
358-
// // If we were updating annotations and the new annotations are nil, leave as empty.
359-
// // This seems to be the behavior for merge-patching nil & empty annotations
360-
// if !reflect.DeepEqual(storedSnapshotContent.Annotations, content.Annotations) && content.Annotations == nil {
361-
// content.Annotations = make(map[string]string)
362-
// }
363357
} else {
364358
return true, nil, fmt.Errorf("cannot update snapshot %s: snapshot not found", action.GetName())
365359
}

pkg/common-controller/snapshot_controller.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,23 +1421,16 @@ func (ctrl *csiSnapshotCommonController) addSnapshotFinalizer(snapshot *crdv1.Vo
14211421
var updatedSnapshot *crdv1.VolumeSnapshot
14221422
var err error
14231423

1424-
// Must perform an update if no finalizers exist
1424+
var patches []utils.PatchOp
14251425
if len(snapshot.ObjectMeta.Finalizers) == 0 {
1426-
snapshotClone := snapshot.DeepCopy()
1427-
if addSourceFinalizer {
1428-
snapshotClone.ObjectMeta.Finalizers = append(snapshotClone.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer)
1429-
}
1430-
if addBoundFinalizer {
1431-
snapshotClone.ObjectMeta.Finalizers = append(snapshotClone.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer)
1432-
}
1433-
updatedSnapshot, err = ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshotClone.Namespace).Update(context.TODO(), snapshotClone, metav1.UpdateOptions{})
1434-
if err != nil {
1435-
return newControllerUpdateError(utils.SnapshotKey(snapshot), err.Error())
1436-
}
1426+
// Replace finalizers with new array if there are no other finalizers
1427+
patches = append(patches, utils.PatchOp{
1428+
Op: "add",
1429+
Path: "/metadata/finalizers",
1430+
Value: []string{utils.VolumeSnapshotContentFinalizer},
1431+
})
14371432
} else {
14381433
// Otherwise, perform a patch
1439-
var patches []utils.PatchOp
1440-
14411434
if addSourceFinalizer {
14421435
patches = append(patches, utils.PatchOp{
14431436
Op: "add",
@@ -1452,11 +1445,10 @@ func (ctrl *csiSnapshotCommonController) addSnapshotFinalizer(snapshot *crdv1.Vo
14521445
Value: utils.VolumeSnapshotBoundFinalizer,
14531446
})
14541447
}
1455-
1456-
updatedSnapshot, err = utils.PatchVolumeSnapshot(snapshot, patches, ctrl.clientset)
1457-
if err != nil {
1458-
return newControllerUpdateError(utils.SnapshotKey(snapshot), err.Error())
1459-
}
1448+
}
1449+
updatedSnapshot, err = utils.PatchVolumeSnapshot(snapshot, patches, ctrl.clientset)
1450+
if err != nil {
1451+
return newControllerUpdateError(utils.SnapshotKey(snapshot), err.Error())
14601452
}
14611453

14621454
_, err = ctrl.storeSnapshotUpdate(updatedSnapshot)

pkg/sidecar-controller/snapshot_controller.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,18 +583,23 @@ func (ctrl *csiSnapshotSideCarController) setAnnVolumeSnapshotBeingCreated(conte
583583
}
584584

585585
// Set AnnVolumeSnapshotBeingCreated
586+
// Combine existing annotations with the new annotations.
587+
// If there are no existing annotations, we create a new map.
586588
klog.V(5).Infof("setAnnVolumeSnapshotBeingCreated: set annotation [%s:yes] on content [%s].", utils.AnnVolumeSnapshotBeingCreated, content.Name)
587-
contentClone := content.DeepCopy()
588-
metav1.SetMetaDataAnnotation(&contentClone.ObjectMeta, utils.AnnVolumeSnapshotBeingCreated, "yes")
589+
patchedAnnotations := make(map[string]string)
590+
for k, v := range content.GetAnnotations() {
591+
patchedAnnotations[k] = v
592+
}
593+
patchedAnnotations[utils.AnnVolumeSnapshotBeingCreated] = "yes"
589594

590595
var patches []utils.PatchOp
591596
patches = append(patches, utils.PatchOp{
592597
Op: "replace",
593598
Path: "/metadata/annotations",
594-
Value: contentClone.ObjectMeta.GetAnnotations(),
599+
Value: patchedAnnotations,
595600
})
596601

597-
patchedContent, err := utils.PatchVolumeSnapshotContent(contentClone, patches, ctrl.clientset)
602+
patchedContent, err := utils.PatchVolumeSnapshotContent(content, patches, ctrl.clientset)
598603
if err != nil {
599604
return content, newControllerUpdateError(content.Name, err.Error())
600605
}

0 commit comments

Comments
 (0)