Skip to content

Commit 58d8381

Browse files
gnufiedsunnylovestiramisu
authored andcommitted
Cleanup conditions and update event message
1 parent 2bd6190 commit 58d8381

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

pkg/modifycontroller/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (ctrl *modifyController) syncPVC(key string) error {
278278
}
279279

280280
vacName := pvc.Spec.VolumeAttributesClassName
281-
if vacName != nil && *vacName != "" && pvc.Status.Phase == v1.ClaimBound {
281+
if (vacName != nil && *vacName != "" && pvc.Status.Phase == v1.ClaimBound) || (util.CurrentModificationInfeasible(pvc) && util.IsVacRolledBack(pvc)) {
282282
_, _, err, _ := ctrl.modify(pvc, pv)
283283
if err != nil {
284284
return err

pkg/modifycontroller/modify_status.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ func (ctrl *modifyController) markCotrollerModifyVolumeRollbackCompeleted(
9191

9292
// Update PV
9393
newPV := pv.DeepCopy()
94-
newPV.Spec.VolumeAttributesClassName = pvc.Spec.VolumeAttributesClassName
94+
if pvc.Spec.VolumeAttributesClassName != nil && *pvc.Spec.VolumeAttributesClassName == "" {
95+
// PV does not support empty string, set VolumeAttributesClassName to nil
96+
newPV.Spec.VolumeAttributesClassName = nil
97+
} else {
98+
newPV.Spec.VolumeAttributesClassName = pvc.Spec.VolumeAttributesClassName
99+
}
95100

96101
// Update PV before PVC to avoid PV not getting updated but PVC did
97102
updatedPV, err := util.PatchPersistentVolume(ctrl.kubeClient, pv, newPV)

pkg/modifycontroller/modify_volume.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ const (
3939

4040
// The return value bool is only used as a sentinel value when function returns without actually performing modification
4141
func (ctrl *modifyController) modify(pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume) (*v1.PersistentVolumeClaim, *v1.PersistentVolume, error, bool) {
42-
pvcSpecVacName := pvc.Spec.VolumeAttributesClassName
43-
curVacName := pvc.Status.CurrentVolumeAttributesClassName
42+
pvcSpecVACName := pvc.Spec.VolumeAttributesClassName
43+
currentVacName := pvc.Status.CurrentVolumeAttributesClassName
44+
4445
pvcKey, err := cache.MetaNamespaceKeyFunc(pvc)
4546
if err != nil {
4647
return pvc, pv, err, false
@@ -52,38 +53,34 @@ func (ctrl *modifyController) modify(pvc *v1.PersistentVolumeClaim, pv *v1.Persi
5253
return pvc, pv, delayModificationErr, false
5354
}
5455

55-
if pvcSpecVacName != nil && curVacName == nil {
56-
// First time adding VAC to a PVC
56+
if util.CurrentModificationInfeasible(pvc) && util.IsVacRolledBack(pvc) {
57+
return ctrl.validateVACAndRollback(pvc, pv)
58+
}
59+
60+
if pvcSpecVACName == nil {
61+
return pvc, pv, nil, false
62+
}
63+
64+
switch {
65+
case currentVacName == nil:
5766
return ctrl.validateVACAndModifyVolumeWithTarget(pvc, pv)
58-
} else if pvcSpecVacName != nil && curVacName != nil && *pvcSpecVacName != *curVacName {
67+
case *currentVacName != *pvcSpecVACName:
5968
// Check if PVC in uncertain state
6069
_, inUncertainState := ctrl.uncertainPVCs[pvcKey]
6170
if !inUncertainState {
6271
klog.V(3).InfoS("previous operation on the PVC failed with a final error, retrying")
6372
return ctrl.validateVACAndModifyVolumeWithTarget(pvc, pv)
6473
} else {
65-
vac, err := ctrl.vacLister.Get(*pvcSpecVacName)
74+
vac, err := ctrl.vacLister.Get(*pvcSpecVACName)
6675
if err != nil {
6776
if apierrors.IsNotFound(err) {
68-
ctrl.eventRecorder.Eventf(pvc, v1.EventTypeWarning, util.VolumeModifyFailed, "VAC "+*pvcSpecVacName+" does not exist.")
77+
ctrl.eventRecorder.Eventf(pvc, v1.EventTypeWarning, util.VolumeModifyFailed, "VAC "+*pvcSpecVACName+" does not exist.")
6978
}
7079
return pvc, pv, err, false
7180
}
72-
return ctrl.controllerModifyVolumeWithTarget(pvc, pv, vac, pvcSpecVacName)
81+
return ctrl.controllerModifyVolumeWithTarget(pvc, pv, vac, pvcSpecVACName)
7382
}
7483
}
75-
76-
// Rollback infeasible errors for recovery
77-
if pvc.Status.ModifyVolumeStatus != nil && pvc.Status.ModifyVolumeStatus.Status == v1.PersistentVolumeClaimModifyVolumeInfeasible {
78-
targetVacName := pvc.Status.ModifyVolumeStatus.TargetVolumeAttributesClassName
79-
// Case 1: rollback to nil
80-
// Case 2: rollback to previous VAC
81-
if (pvcSpecVacName == nil && curVacName == nil && targetVacName != "") || (pvcSpecVacName != nil && curVacName != nil && *pvcSpecVacName == *curVacName && targetVacName != *curVacName) {
82-
return ctrl.validateVACAndRollback(pvc, pv)
83-
}
84-
}
85-
86-
// No modification required
8784
return pvc, pv, nil, false
8885
}
8986

@@ -123,8 +120,12 @@ func (ctrl *modifyController) validateVACAndRollback(
123120
// The controller does not triggers ModifyVolume because it is only
124121
// for rollbacking infeasible errors
125122
// Record an event to indicate that external resizer is rolling back this volume.
123+
rollbackVACName := "nil"
124+
if pvc.Spec.VolumeAttributesClassName != nil {
125+
rollbackVACName = *pvc.Spec.VolumeAttributesClassName
126+
}
126127
ctrl.eventRecorder.Event(pvc, v1.EventTypeNormal, util.VolumeModify,
127-
fmt.Sprintf("external resizer is rolling back volume %s with infeasible error", pvc.Name))
128+
fmt.Sprintf("external resizer is rolling back volume %s with infeasible error to VAC %s", pvc.Name, rollbackVACName))
128129
// Mark pvc.Status.ModifyVolumeStatus as completed
129130
pvc, pv, err := ctrl.markCotrollerModifyVolumeRollbackCompeleted(pvc, pv)
130131
if err != nil {

pkg/util/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,18 @@ func IsInfeasibleError(err error) bool {
281281
// even start or failed. It is for sure not in progress.
282282
return false
283283
}
284+
285+
func IsVacRolledBack(pvc *v1.PersistentVolumeClaim) bool {
286+
pvcSpecVacName := pvc.Spec.VolumeAttributesClassName
287+
curVacName := pvc.Status.CurrentVolumeAttributesClassName
288+
targetVacName := pvc.Status.ModifyVolumeStatus.TargetVolumeAttributesClassName
289+
// Case 1: rollback to nil or empty string
290+
// Case 2: rollback to previous VAC
291+
return ((pvcSpecVacName == nil || *pvcSpecVacName == "") && (curVacName == nil || *curVacName == "") && targetVacName != "") ||
292+
(pvcSpecVacName != nil && curVacName != nil &&
293+
*pvcSpecVacName == *curVacName && targetVacName != *curVacName)
294+
}
295+
296+
func CurrentModificationInfeasible(pvc *v1.PersistentVolumeClaim) bool {
297+
return pvc.Status.ModifyVolumeStatus != nil && pvc.Status.ModifyVolumeStatus.Status == v1.PersistentVolumeClaimModifyVolumeInfeasible
298+
}

0 commit comments

Comments
 (0)