From 8dc4fa9e9bc70c9faaed110ea7435d0f4e69a474 Mon Sep 17 00:00:00 2001 From: Blake Devcich Date: Thu, 25 Jan 2024 14:39:46 -0600 Subject: [PATCH] Fix PV/PVC recreation issue Signed-off-by: Blake Devcich --- .../controller/lustrefilesystem_controller.go | 62 +++++++++++++------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/internal/controller/lustrefilesystem_controller.go b/internal/controller/lustrefilesystem_controller.go index 97c1665..7cb1809 100644 --- a/internal/controller/lustrefilesystem_controller.go +++ b/internal/controller/lustrefilesystem_controller.go @@ -70,6 +70,7 @@ type LustreFileSystemReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.10.0/pkg/reconcile func (r *LustreFileSystemReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) { + log.FromContext(ctx).Info("Reconcile start") fs := &lusv1beta1.LustreFileSystem{} if err := r.Get(ctx, req.NamespacedName, fs); err != nil { @@ -149,27 +150,29 @@ func (r *LustreFileSystemReconciler) Reconcile(ctx context.Context, req ctrl.Req } } - status := fs.Status.Namespaces[namespace].Modes[mode] - if status.State != lusv1beta1.NamespaceAccessReady { - pv, err := r.createOrUpdatePersistentVolume(ctx, fs, namespace, mode) - if err != nil { - return ctrl.Result{}, err - } + pv, err := r.createOrUpdatePersistentVolume(ctx, fs, namespace, mode) + if err != nil { + return ctrl.Result{}, err + } - pvc, err := r.createOrUpdatePersistentVolumeClaim(ctx, fs, namespace, mode) - if err != nil { - return ctrl.Result{}, err - } + pvc, err := r.createOrUpdatePersistentVolumeClaim(ctx, fs, namespace, mode) + if err != nil { + return ctrl.Result{}, err + } - fs.Status.Namespaces[namespace].Modes[mode] = lusv1beta1.LustreFileSystemNamespaceAccessStatus{ - State: lusv1beta1.NamespaceAccessReady, - PersistentVolumeRef: &corev1.LocalObjectReference{ - Name: pv.Name, - }, - PersistentVolumeClaimRef: &corev1.LocalObjectReference{ - Name: pvc.Name, - }, - } + state := lusv1beta1.NamespaceAccessReady + if !r.isPersistentVolumeClaimBound(ctx, fs, namespace, mode) { + state = lusv1beta1.NamespaceAccessPending + } + + fs.Status.Namespaces[namespace].Modes[mode] = lusv1beta1.LustreFileSystemNamespaceAccessStatus{ + State: state, + PersistentVolumeRef: &corev1.LocalObjectReference{ + Name: pv.Name, + }, + PersistentVolumeClaimRef: &corev1.LocalObjectReference{ + Name: pvc.Name, + }, } } } @@ -215,6 +218,27 @@ func (r *LustreFileSystemReconciler) Reconcile(ctx context.Context, req ctrl.Req return ctrl.Result{}, nil } +func (r *LustreFileSystemReconciler) isPersistentVolumeClaimBound(ctx context.Context, fs *lusv1beta1.LustreFileSystem, namespace string, mode corev1.PersistentVolumeAccessMode) bool { + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: fs.PersistentVolumeClaimName(namespace, mode), + Namespace: namespace, + }, + } + + // If we can't find it, then it's not bound + if err := r.Get(ctx, client.ObjectKeyFromObject(pvc), pvc); err != nil { + return false + } + + // PVC is terminating or not bound + if !pvc.GetDeletionTimestamp().IsZero() || pvc.Status.Phase != corev1.ClaimBound { + return false + } + + return true +} + func (r *LustreFileSystemReconciler) createOrUpdatePersistentVolumeClaim(ctx context.Context, fs *lusv1beta1.LustreFileSystem, namespace string, mode corev1.PersistentVolumeAccessMode) (*corev1.PersistentVolumeClaim, error) { pvc := &corev1.PersistentVolumeClaim{