Skip to content

Commit

Permalink
fix pv name update many times bug
Browse files Browse the repository at this point in the history
Signed-off-by: stoneshi-yunify <[email protected]>
  • Loading branch information
stoneshi-yunify committed Nov 22, 2024
1 parent ec4523e commit b126223
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/controller/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/controller/persistentvolumeclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b126223

Please sign in to comment.