Skip to content

Commit

Permalink
Fix PV/PVC recreation issue
Browse files Browse the repository at this point in the history
Signed-off-by: Blake Devcich <[email protected]>
  • Loading branch information
bdevcich committed Jan 25, 2024
1 parent eb84599 commit 8dc4fa9
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions internal/controller/lustrefilesystem_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type LustreFileSystemReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/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 {
Expand Down Expand Up @@ -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,
},
}
}
}
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 8dc4fa9

Please sign in to comment.