From f387e5514b7e642a6ce3b498819bb7a63171d393 Mon Sep 17 00:00:00 2001 From: Atanas Dinov Date: Wed, 24 Jul 2024 12:01:09 +0300 Subject: [PATCH] Add status helpers Signed-off-by: Atanas Dinov --- api/v1alpha1/upgradeplan_types.go | 3 ++ internal/controller/upgradeplan_controller.go | 35 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/api/v1alpha1/upgradeplan_types.go b/api/v1alpha1/upgradeplan_types.go index f9c3de6..65a6afc 100644 --- a/api/v1alpha1/upgradeplan_types.go +++ b/api/v1alpha1/upgradeplan_types.go @@ -29,6 +29,9 @@ const ( // UpgradeInProgress indicates that the upgrade process has started. UpgradeInProgress = "InProgress" + // UpgradeSkipped indicates that the upgrade has been skipped. + UpgradeSkipped = "Skipped" + // UpgradeSucceeded indicates that the upgrade process has been successful. UpgradeSucceeded = "Succeeded" diff --git a/internal/controller/upgradeplan_controller.go b/internal/controller/upgradeplan_controller.go index 68075c2..003b818 100644 --- a/internal/controller/upgradeplan_controller.go +++ b/internal/controller/upgradeplan_controller.go @@ -79,8 +79,7 @@ func (r *UpgradePlanReconciler) executePlan(ctx context.Context, upgradePlan *li } if len(upgradePlan.Status.Conditions) == 0 { - condition := metav1.Condition{Type: lifecyclev1alpha1.KubernetesUpgradedCondition, Status: metav1.ConditionUnknown, Reason: lifecyclev1alpha1.UpgradePending, Message: "Kubernetes upgrade is not yet started"} - meta.SetStatusCondition(&upgradePlan.Status.Conditions, condition) + setPendingCondition(upgradePlan, lifecyclev1alpha1.KubernetesUpgradedCondition, "Kubernetes upgrade is not yet started") // Append OS and other components conditions here... return ctrl.Result{Requeue: true}, nil @@ -118,6 +117,28 @@ func (r *UpgradePlanReconciler) createPlan(ctx context.Context, upgradePlan *lif return nil } +func isHelmUpgradeFinished(plan *lifecyclev1alpha1.UpgradePlan, conditionType string) bool { + condition := meta.FindStatusCondition(plan.Status.Conditions, conditionType) + + if condition == nil { + return false + } + + if condition.Status == metav1.ConditionTrue { + return true + } else if condition.Status == metav1.ConditionFalse && + (condition.Reason == lifecyclev1alpha1.UpgradeSkipped || condition.Reason == lifecyclev1alpha1.UpgradeFailed) { + return true + } + + return false +} + +func setPendingCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, message string) { + condition := metav1.Condition{Type: conditionType, Status: metav1.ConditionUnknown, Reason: lifecyclev1alpha1.UpgradePending, Message: message} + meta.SetStatusCondition(&plan.Status.Conditions, condition) +} + func setInProgressCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, message string) { condition := metav1.Condition{Type: conditionType, Status: metav1.ConditionFalse, Reason: lifecyclev1alpha1.UpgradeInProgress, Message: message} meta.SetStatusCondition(&plan.Status.Conditions, condition) @@ -128,6 +149,16 @@ func setSuccessfulCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, meta.SetStatusCondition(&plan.Status.Conditions, condition) } +func setFailedCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, message string) { + condition := metav1.Condition{Type: conditionType, Status: metav1.ConditionFalse, Reason: lifecyclev1alpha1.UpgradeFailed, Message: message} + meta.SetStatusCondition(&plan.Status.Conditions, condition) +} + +func setSkippedCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, message string) { + condition := metav1.Condition{Type: conditionType, Status: metav1.ConditionFalse, Reason: lifecyclev1alpha1.UpgradeSkipped, Message: message} + meta.SetStatusCondition(&plan.Status.Conditions, condition) +} + // SetupWithManager sets up the controller with the Manager. func (r *UpgradePlanReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr).