From b1262230517a0fc1bc6c80731f391a1e79cdd435 Mon Sep 17 00:00:00 2001 From: stoneshi-yunify Date: Fri, 22 Nov 2024 17:54:58 +0800 Subject: [PATCH] fix pv name update many times bug Signed-off-by: stoneshi-yunify --- internal/controller/nfs.go | 9 ++++++--- internal/controller/persistentvolumeclaim_controller.go | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/controller/nfs.go b/internal/controller/nfs.go index 525dc5e..1e91d70 100644 --- a/internal/controller/nfs.go +++ b/internal/controller/nfs.go @@ -95,9 +95,12 @@ func (p NFSPVC) ParsePV() (*corev1.PersistentVolume, error) { } } - name, ok := p.Annotations[AnnotationPVName] - if !ok || name == "" { - name = fmt.Sprintf("pvc-%s", uuid.NewUUID()) + name := p.Spec.VolumeName + if name == "" { + name, ok = p.Annotations[AnnotationPVName] + if !ok || name == "" { + name = fmt.Sprintf("pvc-%s", uuid.NewUUID()) + } } var storageClassName string diff --git a/internal/controller/persistentvolumeclaim_controller.go b/internal/controller/persistentvolumeclaim_controller.go index cade7c1..3a7a0d2 100644 --- a/internal/controller/persistentvolumeclaim_controller.go +++ b/internal/controller/persistentvolumeclaim_controller.go @@ -67,6 +67,11 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr return ctrl.Result{}, nil } + if pvc.Status.Phase == corev1.ClaimBound { + logger.V(4).Info("pvc already bound") + return ctrl.Result{}, nil + } + if pvc.Spec.StorageClassName == nil || *pvc.Spec.StorageClassName != "" { logger.V(4).Info("pvc's StorageClassName is not empty string which means pv is not static provision") return ctrl.Result{}, nil @@ -116,6 +121,10 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr } else { r.Recorder.Eventf(pvc, corev1.EventTypeWarning, "CreatePVFailed", "failed to create pv %s, error: %s", pv.Name, err.Error()) } + if err == nil { + pvc.Spec.VolumeName = pv.Name + err = r.Client.Update(ctx, pvc) + } return ctrl.Result{}, err } else { return ctrl.Result{}, err