From 2ca8ca78115a5a5f0cca149f4f14e5e7504d13b7 Mon Sep 17 00:00:00 2001 From: liubo02 Date: Fri, 9 Jun 2023 16:44:30 +0800 Subject: [PATCH] fix(volume): skip delegated modifier if sc is not changed --- pkg/manager/volumes/phase.go | 14 ++++++++------ pkg/manager/volumes/pod_vol_modifier.go | 13 +++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/manager/volumes/phase.go b/pkg/manager/volumes/phase.go index 2dc912eea95..256d11dd911 100644 --- a/pkg/manager/volumes/phase.go +++ b/pkg/manager/volumes/phase.go @@ -82,7 +82,7 @@ func (p *podVolModifier) getVolumePhase(vol *ActualVolume) VolumePhase { return VolumePhaseModified } - if p.waitForNextTime(vol.PVC, vol.Desired.StorageClass) { + if p.waitForNextTime(vol.PVC, vol.StorageClass, vol.Desired.StorageClass) { return VolumePhasePending } @@ -120,18 +120,20 @@ func (p *podVolModifier) validate(vol *ActualVolume) error { return fmt.Errorf("volume expansion is not supported by storageclass %s", vol.StorageClass.Name) } } - m := p.getVolumeModifier(vol.Desired.StorageClass) + + m := p.getVolumeModifier(vol.StorageClass, vol.Desired.StorageClass) if m == nil { return nil } - desiredPVC := vol.PVC.DeepCopy() - desiredPVC.Spec.Resources.Requests[corev1.ResourceStorage] = desired // if no pv permission but have sc permission: cannot change sc if isStorageClassChanged(vol.GetStorageClassName(), vol.Desired.GetStorageClassName()) && vol.PV == nil { return fmt.Errorf("cannot change storage class (%s to %s), because there is no permission to get persistent volume", vol.GetStorageClassName(), vol.Desired.GetStorageClassName()) } + desiredPVC := vol.PVC.DeepCopy() + desiredPVC.Spec.Resources.Requests[corev1.ResourceStorage] = desired + return m.Validate(vol.PVC, desiredPVC, vol.StorageClass, vol.Desired.StorageClass) } @@ -142,7 +144,7 @@ func isPVCRevisionChanged(pvc *corev1.PersistentVolumeClaim) bool { return specRevision != statusRevision } -func (p *podVolModifier) waitForNextTime(pvc *corev1.PersistentVolumeClaim, sc *storagev1.StorageClass) bool { +func (p *podVolModifier) waitForNextTime(pvc *corev1.PersistentVolumeClaim, actualSc, desciredSc *storagev1.StorageClass) bool { str, ok := pvc.Annotations[annoKeyPVCLastTransitionTimestamp] if !ok { return false @@ -153,7 +155,7 @@ func (p *podVolModifier) waitForNextTime(pvc *corev1.PersistentVolumeClaim, sc * } d := time.Since(timestamp) - m := p.getVolumeModifier(sc) + m := p.getVolumeModifier(actualSc, desciredSc) waitDur := defaultModifyWaitingDuration if m != nil { diff --git a/pkg/manager/volumes/pod_vol_modifier.go b/pkg/manager/volumes/pod_vol_modifier.go index 730fe8d715f..8880f9a4602 100644 --- a/pkg/manager/volumes/pod_vol_modifier.go +++ b/pkg/manager/volumes/pod_vol_modifier.go @@ -509,7 +509,7 @@ func (p *podVolModifier) modifyPVCAnnoStatus(ctx context.Context, vol *ActualVol } func (p *podVolModifier) modifyVolume(ctx context.Context, vol *ActualVolume) (bool, error) { - m := p.getVolumeModifier(vol.Desired.StorageClass) + m := p.getVolumeModifier(vol.StorageClass, vol.Desired.StorageClass) if m == nil { // skip modifying volume by delegation.VolumeModifier return false, nil @@ -521,11 +521,16 @@ func (p *podVolModifier) modifyVolume(ctx context.Context, vol *ActualVolume) (b return m.ModifyVolume(ctx, pvc, vol.PV, vol.Desired.StorageClass) } -func (p *podVolModifier) getVolumeModifier(sc *storagev1.StorageClass) delegation.VolumeModifier { - if sc == nil { +func (p *podVolModifier) getVolumeModifier(actualSc, desiredSc *storagev1.StorageClass) delegation.VolumeModifier { + if actualSc == nil || desiredSc == nil { return nil } - return p.modifiers[sc.Provisioner] + // sc is not changed + if actualSc.Name == desiredSc.Name { + return nil + } + + return p.modifiers[desiredSc.Provisioner] } func isLeaderEvictedOrTimeout(tc *v1alpha1.TidbCluster, pod *corev1.Pod) bool {