From 1dde3eccc80b70558fe6b468e471ee562048e24a Mon Sep 17 00:00:00 2001 From: Abirdcfly Date: Thu, 19 Oct 2023 11:38:54 +0800 Subject: [PATCH] fix: cpl cant uninstall when component not found Signed-off-by: Abirdcfly --- config/samples/example-test.sh | 1 + controllers/componentplan_controller.go | 90 ++++++++++++------------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/config/samples/example-test.sh b/config/samples/example-test.sh index 8b728b0d..540bb616 100755 --- a/config/samples/example-test.sh +++ b/config/samples/example-test.sh @@ -209,6 +209,7 @@ function waitPodReady() { error "Timeout reached" kubectl describe po -n${namespace} -l ${podLabel} kubectl get po -n${namespace} --show-labels + helm list -A -a exit 1 fi sleep 5 diff --git a/controllers/componentplan_controller.go b/controllers/componentplan_controller.go index bd2854dc..70d725a7 100644 --- a/controllers/componentplan_controller.go +++ b/controllers/componentplan_controller.go @@ -88,47 +88,6 @@ func (r *ComponentPlanReconciler) Reconcile(ctx context.Context, req ctrl.Reques logger = logger.WithValues("Generation", plan.GetGeneration(), "ObservedGeneration", plan.Status.ObservedGeneration, "creator", plan.Spec.Creator) logger.V(1).Info("Get ComponentPlan instance") - if plan.Spec.ComponentRef == nil || plan.Spec.ComponentRef.Namespace == "" || plan.Spec.ComponentRef.Name == "" { - logger.Info("Failed to get Componentplan's Component ref, stop") - return reconcile.Result{}, nil - } - logger = logger.WithValues("Component.Namespace", plan.Spec.ComponentRef.Namespace, "Component.Name", plan.Spec.ComponentRef.Name) - - // Get related component - component := &corev1alpha1.Component{} - err = r.Get(ctx, types.NamespacedName{Namespace: plan.Spec.ComponentRef.Namespace, Name: plan.Spec.ComponentRef.Name}, component) - if err != nil { - msg := fmt.Sprintf("Failed to get Component, wait %s for Component to be found", waitLonger) - if apierrors.IsNotFound(err) { - logger.Info(msg, "Component.Namespace", plan.Spec.ComponentRef.Namespace, "Component.Name", plan.Spec.ComponentRef.Name) - } else { - logger.Error(err, msg, "Component.Namespace", plan.Spec.ComponentRef.Namespace, "Component.Name", plan.Spec.ComponentRef.Name) - } - return reconcile.Result{RequeueAfter: waitLonger}, utils.IgnoreNotFound(err) - } - if component.Status.RepositoryRef == nil || component.Status.RepositoryRef.Name == "" || component.Status.RepositoryRef.Namespace == "" { - logger.Info(fmt.Sprintf("Failed to get Component.Status.RepositoryRef, wait %s to retry", waitLonger), "obj", klog.KObj(component)) - return reconcile.Result{RequeueAfter: waitLonger}, nil - } - logger.V(1).Info("Get Component instance", "Component", klog.KObj(component)) - - if plan.Labels[corev1alpha1.ComponentPlanReleaseNameLabel] != plan.Spec.Name { - if plan.GetLabels() == nil { - plan.Labels = make(map[string]string) - } - plan.Labels[corev1alpha1.ComponentPlanReleaseNameLabel] = plan.Spec.Name - err = r.Update(ctx, plan) - if err != nil { - logger.Error(err, "Failed to update ComponentPlan release label") - } - return ctrl.Result{}, err - } - - // Set the status as Unknown when no status are available. - if len(plan.Status.Conditions) == 0 { - return ctrl.Result{}, r.PatchCondition(ctx, plan, logger, revisionNoExist, false, false, plan.InitCondition()...) - } - // Add a finalizer. Then, we can define some operations which should // occur before the ComponentPlan to be deleted. // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/finalizers @@ -142,9 +101,6 @@ func (r *ComponentPlanReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{}, nil } - // updateLatest try to update all componentplan's status.Latest - go r.updateLatest(ctx, logger, plan) - // Check if the ComponentPlan instance is marked to be deleted, which is // indicated by the deletion timestamp being set. isPlanMarkedToBeDeleted := plan.GetDeletionTimestamp() != nil @@ -178,13 +134,57 @@ func (r *ComponentPlanReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{}, r.PatchCondition(ctx, plan, logger, revisionNoExist, true, false, corev1alpha1.ComponentPlanUninstallSuccess()) } + if plan.Spec.ComponentRef == nil || plan.Spec.ComponentRef.Namespace == "" || plan.Spec.ComponentRef.Name == "" { + logger.Info("Failed to get Componentplan's Component ref, stop") + return reconcile.Result{}, nil + } + logger = logger.WithValues("Component.Namespace", plan.Spec.ComponentRef.Namespace, "Component.Name", plan.Spec.ComponentRef.Name) + + // Get related component + component := &corev1alpha1.Component{} + err = r.Get(ctx, types.NamespacedName{Namespace: plan.Spec.ComponentRef.Namespace, Name: plan.Spec.ComponentRef.Name}, component) + if err != nil { + msg := fmt.Sprintf("Failed to get Component, wait %s for Component to be found", waitLonger) + if apierrors.IsNotFound(err) { + logger.Info(msg, "Component.Namespace", plan.Spec.ComponentRef.Namespace, "Component.Name", plan.Spec.ComponentRef.Name) + } else { + logger.Error(err, msg, "Component.Namespace", plan.Spec.ComponentRef.Namespace, "Component.Name", plan.Spec.ComponentRef.Name) + } + return reconcile.Result{RequeueAfter: waitLonger}, utils.IgnoreNotFound(err) + } + if component.Status.RepositoryRef == nil || component.Status.RepositoryRef.Name == "" || component.Status.RepositoryRef.Namespace == "" { + logger.Info(fmt.Sprintf("Failed to get Component.Status.RepositoryRef, wait %s to retry", waitLonger), "obj", klog.KObj(component)) + return reconcile.Result{RequeueAfter: waitLonger}, nil + } + logger.V(1).Info("Get Component instance", "Component", klog.KObj(component)) + + if plan.Labels[corev1alpha1.ComponentPlanReleaseNameLabel] != plan.Spec.Name { + if plan.GetLabels() == nil { + plan.Labels = make(map[string]string) + } + plan.Labels[corev1alpha1.ComponentPlanReleaseNameLabel] = plan.Spec.Name + err = r.Update(ctx, plan) + if err != nil { + logger.Error(err, "Failed to update ComponentPlan release label") + } + return ctrl.Result{}, err + } + + // Set the status as Unknown when no status are available. + if len(plan.Status.Conditions) == 0 { + return ctrl.Result{}, r.PatchCondition(ctx, plan, logger, revisionNoExist, false, false, plan.InitCondition()...) + } + + // updateLatest try to update all componentplan's status.Latest + go r.updateLatest(ctx, logger, plan) + if plan.Status.GetCondition(corev1alpha1.ComponentPlanTypeSucceeded).Status == corev1.ConditionTrue { switch { case r.isGenerationUpdate(plan): logger.Info("ComponentPlan.Spec is changed, need to install or upgrade ...") return ctrl.Result{}, r.PatchCondition(ctx, plan, logger, revisionNoExist, false, false, plan.InitCondition()...) case r.needRetry(plan): - logger.Info("ComponentPlan need to rollback...") + logger.Info("ComponentPlan need to retry...") default: logger.Info("ComponentPlan is unchanged and has been successful, no need to reconcile") return ctrl.Result{}, nil