Skip to content

Commit

Permalink
Merge pull request #39 from R-HNF/fix-issue38
Browse files Browse the repository at this point in the history
Delete FluentPVCBinding if it times out without becoming Ready condition
  • Loading branch information
R-HNF authored Jul 5, 2024
2 parents 70dcd15 + 9953d19 commit 4708da2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha1/fluentpvc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const (
FluentPVCBindingConditionFinalizerJobSucceeded FluentPVCBindingConditionType = "FinalizerJobSucceeded"
FluentPVCBindingConditionFinalizerJobFailed FluentPVCBindingConditionType = "FinalizerJobFailed"
FluentPVCBindingConditionUnknown FluentPVCBindingConditionType = "Unknown"
FluentPVCBindingConditionPodMissing FluentPVCBindingConditionType = "PodMissing"
)

type FluentPVCBindingPhase string
Expand All @@ -114,6 +115,7 @@ const (
FluentPVCBindingPhaseFinalizerJobSucceeded FluentPVCBindingPhase = FluentPVCBindingPhase(FluentPVCBindingConditionFinalizerJobSucceeded)
FluentPVCBindingPhaseFinalizerJobFailed FluentPVCBindingPhase = FluentPVCBindingPhase(FluentPVCBindingConditionFinalizerJobFailed)
FluentPVCBindingPhaseUnknown FluentPVCBindingPhase = FluentPVCBindingPhase(FluentPVCBindingConditionUnknown)
FluentPVCBindingPhasePodMissing FluentPVCBindingPhase = FluentPVCBindingPhase(FluentPVCBindingConditionPodMissing)
)

// FluentPVCStatus defines the observed state of FluentPVC
Expand Down
8 changes: 8 additions & 0 deletions api/v1alpha1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (b *FluentPVCBinding) IsConditionUnknown() bool {
return meta.IsStatusConditionTrue(b.Status.Conditions, string(FluentPVCBindingConditionUnknown))
}

func (b *FluentPVCBinding) IsConditionPodMissing() bool {
return meta.IsStatusConditionTrue(b.Status.Conditions, string(FluentPVCBindingConditionPodMissing))
}

func (b *FluentPVCBinding) SetConditionReady(reason, message string) {
b.setConditionTrue(FluentPVCBindingConditionReady, reason, message)
}
Expand All @@ -56,6 +60,10 @@ func (b *FluentPVCBinding) SetConditionUnknown(reason, message string) {
b.setConditionTrue(FluentPVCBindingConditionUnknown, reason, message)
}

func (b *FluentPVCBinding) SetConditionPodMissing(reason, message string) {
b.setConditionTrue(FluentPVCBindingConditionPodMissing, reason, message)
}

func (b *FluentPVCBinding) SetConditionNotReady(reason, message string) {
b.setConditionFalse(FluentPVCBindingConditionReady, reason, message)
}
Expand Down
18 changes: 15 additions & 3 deletions controllers/fluentpvcbinding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func (r *fluentPVCBindingReconciler) Reconcile(ctx context.Context, req ctrl.Req
pvcFound = filled
}

if !pvcFound && b.IsConditionPodMissing() {
logger.Info(fmt.Sprintf("fluentpvcbinding='%s' is missing the target pod, so delete fluentpvcbinding='%s'.", b.Name, b.Name))
if err := r.deleteFluentPVCBinding(ctx, b); err != nil {
return ctrl.Result{}, xerrors.Errorf("Unexpected error occurred.: %w", err)
}
return ctrl.Result{}, nil
}

if pvcFound && pvc.Status.Phase == corev1.ClaimLost {
if err := r.updateConditionUnknownPVCLost(ctx, b); err != nil {
return ctrl.Result{}, xerrors.Errorf("Unexpected error occurred.: %w", err)
Expand All @@ -108,7 +116,7 @@ func (r *fluentPVCBindingReconciler) Reconcile(ctx context.Context, req ctrl.Req

if !podFound && !b.IsConditionReady() {
if isCreatedBefore(b, 1*time.Hour) { // TODO: make it configurable?
if err := r.updateConditionUnknownPodNotFoundLongTime(ctx, b); err != nil {
if err := r.updateConditionPodMissingBindingPodTimeout(ctx, b); err != nil {
return ctrl.Result{}, xerrors.Errorf("Unexpected error occurred.: %w", err)
}
} else {
Expand Down Expand Up @@ -305,12 +313,12 @@ func (r *fluentPVCBindingReconciler) updateConditionUnknownPVCLost(ctx context.C
return r.updateConditionUnknown(ctx, b, "PVCLost", message)
}

func (r *fluentPVCBindingReconciler) updateConditionUnknownPodNotFoundLongTime(ctx context.Context, b *fluentpvcv1alpha1.FluentPVCBinding) error {
func (r *fluentPVCBindingReconciler) updateConditionPodMissingBindingPodTimeout(ctx context.Context, b *fluentpvcv1alpha1.FluentPVCBinding) error {
message := fmt.Sprintf(
"Pod='%s'(UID='%s') is not found even though it hasn't been finalized since it became Ready status.(fluentpvcbinding='%s')",
b.Spec.Pod.Name, b.Spec.Pod.UID, b.Name,
)
return r.updateConditionUnknown(ctx, b, "PodNotFound", message)
return r.updateConditionPodMissing(ctx, b, "BindingPodTimeout", message)
}

func (r *fluentPVCBindingReconciler) updateConditionUnknownPodAndPVCNotFound(ctx context.Context, b *fluentpvcv1alpha1.FluentPVCBinding) error {
Expand Down Expand Up @@ -379,6 +387,10 @@ func (r *fluentPVCBindingReconciler) updateConditionReady(ctx context.Context, b
return r.updateCondition(ctx, b, reason, message, b.SetConditionReady)
}

func (r *fluentPVCBindingReconciler) updateConditionPodMissing(ctx context.Context, b *fluentpvcv1alpha1.FluentPVCBinding, reason, message string) error {
return r.updateCondition(ctx, b, reason, message, b.SetConditionPodMissing)
}

func (r *fluentPVCBindingReconciler) updateCondition(ctx context.Context, b *fluentpvcv1alpha1.FluentPVCBinding, reason, message string, conditionUpdateFunc func(string, string)) error {
logger := ctrl.LoggerFrom(ctx).WithName("fluentPVCBindingReconciler").WithName("updateCondition")
logger.Info(message)
Expand Down
29 changes: 27 additions & 2 deletions controllers/pvc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ func (r *pvcReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
pvc := &corev1.PersistentVolumeClaim{}
if err := r.Get(ctx, req.NamespacedName, pvc); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
logger.Info(fmt.Sprintf("Requeue request because pvc='%s' is not found.", req.NamespacedName))
return requeueResult(10 * time.Second), nil
}
return ctrl.Result{}, xerrors.Errorf("Unexpected error occurred.: %w", err)
}
b := &fluentpvcv1alpha1.FluentPVCBinding{}
if err := r.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: pvc.Name}, b); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
logger.Info(fmt.Sprintf("Requeue request because fluentpvcbinding='%s' (namespace='%s') is not found.", pvc.Name, req.Namespace))
return requeueResult(10 * time.Second), nil
}
return ctrl.Result{}, xerrors.Errorf("Unexpected error occurred.: %w", err)
}
Expand All @@ -65,6 +67,29 @@ func (r *pvcReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
))
return ctrl.Result{}, nil
}
if b.IsConditionPodMissing() {
logger.Info(fmt.Sprintf("fluentpvcbinding='%s' is missing target pod, so delete pvc='%s'.", b.Name, pvc.Name))

logger.Info(fmt.Sprintf("Remove the finalizer='%s' from pvc='%s'", constants.PVCFinalizerName, pvc.Name))
controllerutil.RemoveFinalizer(pvc, constants.PVCFinalizerName)
// Update PVC resource to remove finalizer.
if err := r.Update(ctx, pvc); client.IgnoreNotFound(err) != nil {
if apierrors.IsConflict(err) {
// NOTE: Conflict with deleting the pvc in other pvcReconciler#Reconcile.
return ctrl.Result{}, nil
}
return ctrl.Result{}, xerrors.Errorf(
"Failed to remove finalizer from PVC='%s'.: %w",
pvc.Name, err,
)
}

if err := r.Delete(ctx, pvc, deleteOptionsBackground(&pvc.UID, &pvc.ResourceVersion)); client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, xerrors.Errorf("Unexpected error occurred.: %w", err)
}

return ctrl.Result{}, nil
}
if b.IsConditionUnknown() {
logger.Info(fmt.Sprintf("fluentpvcbinding='%s' is unknown status, so skip processing.", b.Name))
return ctrl.Result{}, nil
Expand Down

0 comments on commit 4708da2

Please sign in to comment.