Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor controllers to use patch to update snapshot status #1110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pkg/common-controller/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,13 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
return true, nil, err
}

err = json.Unmarshal(modified, content)
//json.Unmarshal will not clear the fields that are not present in the patch
var newContent = crdv1.VolumeSnapshotContent{}
err = json.Unmarshal(modified, &newContent)
if err != nil {
return true, nil, err
}
content = &newContent

storedVer, _ := strconv.Atoi(content.ResourceVersion)
content.ResourceVersion = strconv.Itoa(storedVer + 1)
Expand Down Expand Up @@ -351,10 +354,13 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
return true, nil, err
}

err = json.Unmarshal(modified, storedSnapshot)
//json.Unmarshal will not clear the fields that are not present in the patch
var newSnapshot = crdv1.VolumeSnapshot{}
err = json.Unmarshal(modified, &newSnapshot)
if err != nil {
return true, nil, err
}
storedSnapshot = &newSnapshot

storedVer, _ := strconv.Atoi(storedSnapshot.ResourceVersion)
storedSnapshot.ResourceVersion = strconv.Itoa(storedVer + 1)
Expand Down Expand Up @@ -1047,6 +1053,14 @@ func newSnapshot(
if targetContentName != "" {
snapshot.Spec.Source.VolumeSnapshotContentName = &targetContentName
}

// Perform marshal and unmarshal to simulate the real behavior of the API server
// for example, field like metav1.Time precision should be normalized
buf, _ := json.Marshal(snapshot)
normalized := &crdv1.VolumeSnapshot{}
_ = json.Unmarshal(buf, normalized)
snapshot = *normalized

if withAllFinalizers {
return withSnapshotFinalizers([]*crdv1.VolumeSnapshot{&snapshot}, utils.VolumeSnapshotAsSourceFinalizer, utils.VolumeSnapshotBoundFinalizer)[0]
}
Expand Down Expand Up @@ -1201,10 +1215,18 @@ func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, bo
}

func newVolumeError(message string) *crdv1.VolumeSnapshotError {
return &crdv1.VolumeSnapshotError{
snapErrIn := &crdv1.VolumeSnapshotError{
Time: &metav1.Time{},
Message: &message,
}

// Marshal and unmarshal to simulate the real behavior of the API server
// for example, metav1.Time precision should be normalized
buf, _ := json.Marshal(snapErrIn)
snapErrOut := &crdv1.VolumeSnapshotError{}
_ = json.Unmarshal(buf, snapErrOut)

return snapErrOut
}

func testSyncSnapshot(ctrl *csiSnapshotCommonController, reactor *snapshotReactor, test controllerTest) error {
Expand Down
32 changes: 25 additions & 7 deletions pkg/common-controller/groupsnapshot_controller_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,32 @@ func (ctrl *csiSnapshotCommonController) updateGroupSnapshotErrorStatusWithEvent
return nil
}
groupSnapshotClone := groupSnapshot.DeepCopy()
if groupSnapshotClone.Status == nil {
groupSnapshotClone.Status = &crdv1alpha1.VolumeGroupSnapshotStatus{}
newStatus := &crdv1alpha1.VolumeGroupSnapshotStatus{}
if groupSnapshot.Status != nil {
newStatus = groupSnapshotClone.Status.DeepCopy()
}
statusError := &crdv1.VolumeSnapshotError{
newStatus.Error = &crdv1.VolumeSnapshotError{
Time: &metav1.Time{
Time: time.Now(),
},
Message: &message,
}
groupSnapshotClone.Status.Error = statusError

// Only update ReadyToUse in VolumeGroupSnapshot's Status to false if setReadyToFalse is true.
if setReadyToFalse {
ready := false
groupSnapshotClone.Status.ReadyToUse = &ready
newStatus.ReadyToUse = &ready
}
newSnapshot, err := ctrl.clientset.GroupsnapshotV1alpha1().VolumeGroupSnapshots(groupSnapshotClone.Namespace).UpdateStatus(context.TODO(), groupSnapshotClone, metav1.UpdateOptions{})

patches := []utils.PatchOp{
{
Op: "replace",
Path: "/status",
Value: newStatus,
},
}

newSnapshot, err := utils.PatchVolumeGroupSnapshot(groupSnapshotClone, patches, ctrl.clientset, "status")

// Emit the event even if the status update fails so that user can see the error
ctrl.eventRecorder.Event(newSnapshot, eventtype, reason, message)
Expand Down Expand Up @@ -680,7 +690,15 @@ func (ctrl *csiSnapshotCommonController) updateGroupSnapshotStatus(groupSnapshot
ctrl.eventRecorder.Event(groupSnapshot, v1.EventTypeNormal, "GroupSnapshotReady", msg)
}

newGroupSnapshotObj, err := ctrl.clientset.GroupsnapshotV1alpha1().VolumeGroupSnapshots(groupSnapshotClone.Namespace).UpdateStatus(context.TODO(), groupSnapshotClone, metav1.UpdateOptions{})
patches := []utils.PatchOp{
{
Op: "replace",
Path: "/status",
Value: newStatus,
},
}

newGroupSnapshotObj, err := utils.PatchVolumeGroupSnapshot(groupSnapshotClone, patches, ctrl.clientset, "status")
if err != nil {
return nil, newControllerUpdateError(utils.GroupSnapshotKey(groupSnapshot), err.Error())
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/common-controller/snapshot_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,14 @@ func (ctrl *csiSnapshotCommonController) updateSnapshotErrorStatusWithEvent(snap
ready := false
snapshotClone.Status.ReadyToUse = &ready
}
newSnapshot, err := ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshotClone.Namespace).UpdateStatus(context.TODO(), snapshotClone, metav1.UpdateOptions{})
patches := []utils.PatchOp{
{
Op: "replace",
Path: "/status",
Value: snapshotClone.Status,
},
}
newSnapshot, err := utils.PatchVolumeSnapshot(snapshotClone, patches, ctrl.clientset, "status")

// Emit the event even if the status update fails so that user can see the error
ctrl.eventRecorder.Event(newSnapshot, eventtype, reason, message)
Expand Down Expand Up @@ -1250,7 +1257,15 @@ func (ctrl *csiSnapshotCommonController) updateSnapshotStatus(snapshot *crdv1.Vo
ctrl.eventRecorder.Event(snapshot, v1.EventTypeNormal, "SnapshotReady", msg)
}

newSnapshotObj, err := ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshotClone.Namespace).UpdateStatus(context.TODO(), snapshotClone, metav1.UpdateOptions{})
patches := []utils.PatchOp{
{
Op: "replace",
Path: "/status",
Value: newStatus,
},
}
newSnapshotObj, err := utils.PatchVolumeSnapshot(snapshotClone, patches, ctrl.clientset, "status")
//newSnapshotObj, err := ctrl.clientset.SnapshotV1().VolumeSnapshots(snapshotClone.Namespace).UpdateStatus(context.TODO(), snapshotClone, metav1.UpdateOptions{})
if err != nil {
return nil, newControllerUpdateError(utils.SnapshotKey(snapshot), err.Error())
}
Expand Down
Loading