Skip to content

Commit ac09a30

Browse files
gnufiedsunnylovestiramisu
authored andcommitted
Cleanup conditions and update event message
1 parent edd362d commit ac09a30

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

pkg/modifycontroller/modify_volume.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ const (
3737

3838
// The return value bool is only used as a sentinel value when function returns without actually performing modification
3939
func (ctrl *modifyController) modify(pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume) (*v1.PersistentVolumeClaim, *v1.PersistentVolume, error, bool) {
40-
pvcSpecVacName := pvc.Spec.VolumeAttributesClassName
41-
curVacName := pvc.Status.CurrentVolumeAttributesClassName
40+
pvcSpecVACName := pvc.Spec.VolumeAttributesClassName
41+
currentVacName := pvc.Status.CurrentVolumeAttributesClassName
42+
4243
pvcKey, err := cache.MetaNamespaceKeyFunc(pvc)
4344
if err != nil {
4445
return pvc, pv, err, false
@@ -50,39 +51,50 @@ func (ctrl *modifyController) modify(pvc *v1.PersistentVolumeClaim, pv *v1.Persi
5051
return pvc, pv, delayModificationErr, false
5152
}
5253

53-
if pvcSpecVacName != nil && curVacName == nil {
54-
// First time adding VAC to a PVC
54+
if currentModificationInfeasible(pvc) && isVacRolledBack(pvc) {
55+
return ctrl.validateVACAndRollback(pvc, pv)
56+
}
57+
58+
if pvcSpecVACName == nil {
59+
return pvc, pv, nil, false
60+
}
61+
62+
switch {
63+
case currentVacName == nil:
5564
return ctrl.validateVACAndModifyVolumeWithTarget(pvc, pv)
56-
} else if pvcSpecVacName != nil && curVacName != nil && *pvcSpecVacName != *curVacName {
65+
case *currentVacName != *pvcSpecVACName:
5766
// Check if PVC in uncertain state
5867
_, inUncertainState := ctrl.uncertainPVCs[pvcKey]
5968
if !inUncertainState {
6069
klog.V(3).InfoS("previous operation on the PVC failed with a final error, retrying")
6170
return ctrl.validateVACAndModifyVolumeWithTarget(pvc, pv)
6271
} else {
63-
vac, err := ctrl.vacLister.Get(*pvcSpecVacName)
72+
vac, err := ctrl.vacLister.Get(*pvcSpecVACName)
6473
if err != nil {
6574
if apierrors.IsNotFound(err) {
66-
ctrl.eventRecorder.Eventf(pvc, v1.EventTypeWarning, util.VolumeModifyFailed, "VAC "+*pvcSpecVacName+" does not exist.")
75+
ctrl.eventRecorder.Eventf(pvc, v1.EventTypeWarning, util.VolumeModifyFailed, "VAC "+*pvcSpecVACName+" does not exist.")
6776
}
6877
return pvc, pv, err, false
6978
}
70-
return ctrl.controllerModifyVolumeWithTarget(pvc, pv, vac, pvcSpecVacName)
79+
return ctrl.controllerModifyVolumeWithTarget(pvc, pv, vac, pvcSpecVACName)
7180
}
7281
}
82+
return pvc, pv, nil, false
83+
}
7384

74-
// Rollback infeasible errors for recovery
75-
if pvc.Status.ModifyVolumeStatus != nil && pvc.Status.ModifyVolumeStatus.Status == v1.PersistentVolumeClaimModifyVolumeInfeasible {
76-
targetVacName := pvc.Status.ModifyVolumeStatus.TargetVolumeAttributesClassName
77-
// Case 1: rollback to nil
78-
// Case 2: rollback to previous VAC
79-
if (pvcSpecVacName == nil && curVacName == nil && targetVacName != "") || (pvcSpecVacName != nil && curVacName != nil && *pvcSpecVacName == *curVacName && targetVacName != *curVacName) {
80-
return ctrl.validateVACAndRollback(pvc, pv)
81-
}
82-
}
85+
func isVacRolledBack(pvc *v1.PersistentVolumeClaim) bool {
86+
pvcSpecVacName := pvc.Spec.VolumeAttributesClassName
87+
curVacName := pvc.Status.CurrentVolumeAttributesClassName
88+
targetVacName := pvc.Status.ModifyVolumeStatus.TargetVolumeAttributesClassName
89+
// Case 1: rollback to nil
90+
// Case 2: rollback to previous VAC
91+
return (pvcSpecVacName == nil && curVacName == nil && targetVacName != "") ||
92+
(pvcSpecVacName != nil && curVacName != nil &&
93+
*pvcSpecVacName == *curVacName && targetVacName != *curVacName)
94+
}
8395

84-
// No modification required
85-
return pvc, pv, nil, false
96+
func currentModificationInfeasible(pvc *v1.PersistentVolumeClaim) bool {
97+
return pvc.Status.ModifyVolumeStatus != nil && pvc.Status.ModifyVolumeStatus.Status == v1.PersistentVolumeClaimModifyVolumeInfeasible
8698
}
8799

88100
// func validateVACAndModifyVolumeWithTarget validate the VAC. The function sets pvc.Status.ModifyVolumeStatus
@@ -121,8 +133,12 @@ func (ctrl *modifyController) validateVACAndRollback(
121133
// The controller does not triggers ModifyVolume because it is only
122134
// for rollbacking infeasible errors
123135
// Record an event to indicate that external resizer is rolling back this volume.
136+
rollbackVACName := "nil"
137+
if pvc.Spec.VolumeAttributesClassName != nil {
138+
rollbackVACName = *pvc.Spec.VolumeAttributesClassName
139+
}
124140
ctrl.eventRecorder.Event(pvc, v1.EventTypeNormal, util.VolumeModify,
125-
fmt.Sprintf("external resizer is rolling back volume %s with infeasible error", pvc.Name))
141+
fmt.Sprintf("external resizer is rolling back volume %s with infeasible error to VAC %s", pvc.Name, rollbackVACName))
126142
// Mark pvc.Status.ModifyVolumeStatus as completed
127143
pvc, pv, err := ctrl.markCotrollerModifyVolumeRollbackCompeleted(pvc, pv)
128144
if err != nil {

0 commit comments

Comments
 (0)