From cc1a7b22f394688dc84d08e19319809f75e65272 Mon Sep 17 00:00:00 2001 From: Shubham Pampattiwar Date: Wed, 12 Jul 2023 15:10:51 -0700 Subject: [PATCH] Update VolumeSnapshot and VolumeSnapshotContent using JSON patch --- pkg/common-controller/snapshot_controller.go | 66 +++++++++++++------ pkg/sidecar-controller/snapshot_controller.go | 19 ++++-- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/pkg/common-controller/snapshot_controller.go b/pkg/common-controller/snapshot_controller.go index 60b3cb062..6aeee6faa 100644 --- a/pkg/common-controller/snapshot_controller.go +++ b/pkg/common-controller/snapshot_controller.go @@ -1380,9 +1380,16 @@ func (ctrl *csiSnapshotCommonController) SetDefaultSnapshotClass(snapshot *crdv1 return nil, snapshot, fmt.Errorf("%d default snapshot classes were found", len(defaultClasses)) } klog.V(5).Infof("setDefaultSnapshotClass [%s]: default VolumeSnapshotClassName [%s]", snapshot.Name, defaultClasses[0].Name) - snapshotClone := snapshot.DeepCopy() - snapshotClone.Spec.VolumeSnapshotClassName = &(defaultClasses[0].Name) - newSnapshot, err := ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshotClone.Namespace).Update(context.TODO(), snapshotClone, metav1.UpdateOptions{}) + + patches := []utils.PatchOp{ + { + Op: "replace", + Path: "/spec/volumeSnapshotClassName", + Value: &(defaultClasses[0].Name), + }, + } + + newSnapshot, err := utils.PatchVolumeSnapshot(snapshot, patches, ctrl.clientset) if err != nil { klog.V(4).Infof("updating VolumeSnapshot[%s] default class failed %v", utils.SnapshotKey(snapshot), err) } @@ -1515,14 +1522,22 @@ func (ctrl *csiSnapshotCommonController) removeSnapshotFinalizer(snapshot *crdv1 return newControllerUpdateError(snapshot.Name, err.Error()) } - snapshotClone := snapshot.DeepCopy() + var patches []utils.PatchOp if removeSourceFinalizer { - snapshotClone.ObjectMeta.Finalizers = utils.RemoveString(snapshotClone.ObjectMeta.Finalizers, utils.VolumeSnapshotAsSourceFinalizer) + patches = append(patches, utils.PatchOp{ + Op: "remove", + Path: "/metadata/finalizers", + Value: utils.VolumeSnapshotAsSourceFinalizer, + }) } if removeBoundFinalizer { - snapshotClone.ObjectMeta.Finalizers = utils.RemoveString(snapshotClone.ObjectMeta.Finalizers, utils.VolumeSnapshotBoundFinalizer) + patches = append(patches, utils.PatchOp{ + Op: "remove", + Path: "/metadata/finalizers", + Value: utils.VolumeSnapshotBoundFinalizer, + }) } - newSnapshot, err := ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshotClone.Namespace).Update(context.TODO(), snapshotClone, metav1.UpdateOptions{}) + newSnapshot, err := utils.PatchVolumeSnapshot(snapshot, patches, ctrl.clientset) if err != nil { return newControllerUpdateError(snapshot.Name, err.Error()) } @@ -1606,18 +1621,23 @@ func (ctrl *csiSnapshotCommonController) checkAndSetInvalidContentLabel(content return content, nil } - contentClone := content.DeepCopy() + var patches []utils.PatchOp if hasLabel { // Need to remove the label - delete(contentClone.Labels, utils.VolumeSnapshotContentInvalidLabel) + patches = append(patches, utils.PatchOp{ + Op: "remove", + Path: "/metadata/labels/" + utils.VolumeSnapshotContentInvalidLabel, + }) + } else { // Snapshot content is invalid and does not have the label. Need to add the label - if contentClone.ObjectMeta.Labels == nil { - contentClone.ObjectMeta.Labels = make(map[string]string) - } - contentClone.ObjectMeta.Labels[utils.VolumeSnapshotContentInvalidLabel] = "" + patches = append(patches, utils.PatchOp{ + Op: "add", + Path: "/metadata/labels/" + utils.VolumeSnapshotContentInvalidLabel, + Value: "", + }) } - updatedContent, err := ctrl.clientset.SnapshotV1().VolumeSnapshotContents().Update(context.TODO(), contentClone, metav1.UpdateOptions{}) + updatedContent, err := utils.PatchVolumeSnapshotContent(content, patches, ctrl.clientset) if err != nil { return content, newControllerUpdateError(content.Name, err.Error()) } @@ -1647,19 +1667,23 @@ func (ctrl *csiSnapshotCommonController) checkAndSetInvalidSnapshotLabel(snapsho return snapshot, nil } - snapshotClone := snapshot.DeepCopy() + var patches []utils.PatchOp if hasLabel { // Need to remove the label - delete(snapshotClone.Labels, utils.VolumeSnapshotInvalidLabel) + patches = append(patches, utils.PatchOp{ + Op: "remove", + Path: "/metadata/labels/" + utils.VolumeSnapshotInvalidLabel, + }) } else { // Snapshot is invalid and does not have the label. Need to add the label - if snapshotClone.ObjectMeta.Labels == nil { - snapshotClone.ObjectMeta.Labels = make(map[string]string) - } - snapshotClone.ObjectMeta.Labels[utils.VolumeSnapshotInvalidLabel] = "" + patches = append(patches, utils.PatchOp{ + Op: "add", + Path: "/metadata/labels/" + utils.VolumeSnapshotInvalidLabel, + Value: "", + }) } - updatedSnapshot, err := ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshot.Namespace).Update(context.TODO(), snapshotClone, metav1.UpdateOptions{}) + updatedSnapshot, err := utils.PatchVolumeSnapshot(snapshot, patches, ctrl.clientset) if err != nil { return snapshot, newControllerUpdateError(utils.SnapshotKey(snapshot), err.Error()) } diff --git a/pkg/sidecar-controller/snapshot_controller.go b/pkg/sidecar-controller/snapshot_controller.go index 0ccf2e53a..0840fa1f9 100644 --- a/pkg/sidecar-controller/snapshot_controller.go +++ b/pkg/sidecar-controller/snapshot_controller.go @@ -542,10 +542,14 @@ func (ctrl csiSnapshotSideCarController) removeContentFinalizer(content *crdv1.V // the finalizer does not exit, return directly return nil } - contentClone := content.DeepCopy() - contentClone.ObjectMeta.Finalizers = utils.RemoveString(contentClone.ObjectMeta.Finalizers, utils.VolumeSnapshotContentFinalizer) + var patches []utils.PatchOp + patches = append(patches, utils.PatchOp{ + Op: "remove", + Path: "/metadata/finalizers", + Value: utils.VolumeSnapshotContentFinalizer, + }) - updatedContent, err := ctrl.clientset.SnapshotV1().VolumeSnapshotContents().Update(context.TODO(), contentClone, metav1.UpdateOptions{}) + updatedContent, err := utils.PatchVolumeSnapshotContent(content, patches, ctrl.clientset) if err != nil { return newControllerUpdateError(content.Name, err.Error()) } @@ -637,10 +641,13 @@ func (ctrl csiSnapshotSideCarController) removeAnnVolumeSnapshotBeingCreated(con // the annotation does not exist, return directly return content, nil } - contentClone := content.DeepCopy() - delete(contentClone.ObjectMeta.Annotations, utils.AnnVolumeSnapshotBeingCreated) + var patches []utils.PatchOp + patches = append(patches, utils.PatchOp{ + Op: "remove", + Path: "/metadata/annotations/" + utils.AnnVolumeSnapshotBeingCreated, + }) - updatedContent, err := ctrl.clientset.SnapshotV1().VolumeSnapshotContents().Update(context.TODO(), contentClone, metav1.UpdateOptions{}) + updatedContent, err := utils.PatchVolumeSnapshotContent(content, patches, ctrl.clientset) if err != nil { return content, newControllerUpdateError(content.Name, err.Error()) }