From 0639ef3d10be330a34a51929c73b034c6d379d02 Mon Sep 17 00:00:00 2001 From: Aniruddha Basak Date: Thu, 25 Apr 2024 22:27:20 +0200 Subject: [PATCH] Add debug logs for the multi stage addon Add debug log information in the addon controller Signed-off-by: Aniruddha Basak --- .../controller/clusteraddon_controller.go | 46 +++++++------------ pkg/kube/kube.go | 26 ----------- 2 files changed, 17 insertions(+), 55 deletions(-) diff --git a/internal/controller/clusteraddon_controller.go b/internal/controller/clusteraddon_controller.go index 76e02dd98..bd007e6e5 100644 --- a/internal/controller/clusteraddon_controller.go +++ b/internal/controller/clusteraddon_controller.go @@ -39,7 +39,6 @@ import ( "github.com/SovereignCloudStack/cluster-stack-operator/pkg/kube" "github.com/SovereignCloudStack/cluster-stack-operator/pkg/release" "github.com/SovereignCloudStack/cluster-stack-operator/pkg/workloadcluster" - "github.com/go-logr/logr" sprig "github.com/go-task/slim-sprig" "github.com/google/cel-go/cel" celtypes "github.com/google/cel-go/common/types" @@ -213,8 +212,6 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re return reconcile.Result{}, fmt.Errorf("failed to get cluster addon config path: %w", err) } - logger := log.FromContext(ctx) - // Check whether current Helm chart has been applied in the workload cluster. If not, then we need to apply the helm chart (again). // the spec.clusterStack is only set after a Helm chart from a ClusterStack has been applied successfully. // If it is not set, the Helm chart has never been applied. @@ -284,7 +281,7 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re } if clusterAddon.Spec.ClusterStack != "" { - oldClusterStackAddonChartPath, requeue, err := r.downloadOldClusterStackRelease(ctx, clusterAddon, logger) + oldClusterStackAddonChartPath, requeue, err := r.downloadOldClusterStackRelease(ctx, clusterAddon) if err != nil { return reconcile.Result{}, fmt.Errorf("failed to download old cluster stack releases: %w", err) } @@ -296,8 +293,6 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re // dst - /tmp/cluster-stacks/docker-ferrol-1-27-v1/docker-ferrol-1-27-cluster-addon-v1/ in.oldDestinationClusterAddonChartDir = strings.TrimSuffix(oldClusterStackAddonChartPath, ".tgz") - logger.Info("old cluster stack's cluster addon chart path", "path", in.oldDestinationClusterAddonChartDir) - if err := unTarContent(oldClusterStackAddonChartPath, in.oldDestinationClusterAddonChartDir); err != nil { return reconcile.Result{}, fmt.Errorf("failed to untar cluster addon chart: %q: %w", oldClusterStackAddonChartPath, err) } @@ -502,6 +497,8 @@ func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx conte } func (r *ClusterAddonReconciler) executeStage(ctx context.Context, stage clusteraddon.Stage, in templateAndApplyClusterAddonInput) (bool, error) { + logger := log.FromContext(ctx) + var ( shouldRequeue bool err error @@ -524,9 +521,10 @@ func (r *ClusterAddonReconciler) executeStage(ctx context.Context, stage cluster check: switch in.clusterAddon.Status.HelmChartStatus[stage.HelmChartName] { case csov1alpha1.None: - // If no WaitForPreCondition is mentioned. + // If WaitForPreCondition is mentioned. if !reflect.DeepEqual(stage.WaitForPreCondition, clusteraddon.WaitForCondition{}) { // Evaluate the condition. + logger.V(1).Info("starting to evaluate pre condition", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) if err := getDynamicResourceAndEvaluateCEL(ctx, in.dynamicClient, in.discoverClient, stage.WaitForPreCondition); err != nil { if errors.Is(err, clusteraddon.ConditionNotMatchError) { conditions.MarkFalse( @@ -543,12 +541,14 @@ check: } return false, fmt.Errorf("failed to get dynamic resource and evaluate cel expression for pre condition: %w", err) } + logger.V(1).Info("finished evaluating pre condition", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) } in.clusterAddon.Status.HelmChartStatus[stage.HelmChartName] = csov1alpha1.ApplyingOrDeleting goto check case csov1alpha1.ApplyingOrDeleting: if stage.Action == clusteraddon.Apply { + logger.V(1).Info("starting to apply helm chart", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) shouldRequeue, err = helmTemplateAndApplyNewClusterStack(ctx, in, stage.HelmChartName) if err != nil { return false, fmt.Errorf("failed to helm template and apply: %w", err) @@ -564,12 +564,14 @@ check: return true, nil } + logger.V(1).Info("finished applying helm chart", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) in.clusterAddon.Status.HelmChartStatus[stage.HelmChartName] = csov1alpha1.WaitingForPostCondition goto check } else { // Delete part + logger.V(1).Info("starting to delete helm chart", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) shouldRequeue, err = helmTemplateAndDeleteNewClusterStack(ctx, in, stage.HelmChartName) if err != nil { return false, fmt.Errorf("failed to delete helm chart: %w", err) @@ -585,14 +587,17 @@ check: return true, nil } + logger.V(1).Info("finished deleting helm chart", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) + in.clusterAddon.Status.HelmChartStatus[stage.HelmChartName] = csov1alpha1.WaitingForPostCondition goto check } case csov1alpha1.WaitingForPostCondition: - // If no WaitForPostCondition is mentioned. + // If WaitForPostCondition is mentioned. if !reflect.DeepEqual(stage.WaitForPostCondition, clusteraddon.WaitForCondition{}) { // Evaluate the condition. + logger.V(1).Info("starting to evaluate post condition", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) if err := getDynamicResourceAndEvaluateCEL(ctx, in.dynamicClient, in.discoverClient, stage.WaitForPostCondition); err != nil { if errors.Is(err, clusteraddon.ConditionNotMatchError) { conditions.MarkFalse( @@ -607,7 +612,9 @@ check: } return false, fmt.Errorf("failed to get dynamic resource and evaluate cel expression for post condition: %w", err) } + logger.V(1).Info("finished evaluating post condition", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook) } + in.clusterAddon.Status.HelmChartStatus[stage.HelmChartName] = csov1alpha1.Done } @@ -615,7 +622,7 @@ check: } // downloadOldClusterStackRelease downloads the old cluster stack if not present and returns release clusterAddon chart path if requeue and error. -func (r *ClusterAddonReconciler) downloadOldClusterStackRelease(ctx context.Context, clusterAddon *csov1alpha1.ClusterAddon, logger logr.Logger) (string, bool, error) { +func (r *ClusterAddonReconciler) downloadOldClusterStackRelease(ctx context.Context, clusterAddon *csov1alpha1.ClusterAddon) (string, bool, error) { // initiate github release. gc, err := r.GitHubClientFactory.NewClient(ctx) if err != nil { @@ -636,7 +643,6 @@ func (r *ClusterAddonReconciler) downloadOldClusterStackRelease(ctx context.Cont } conditions.MarkTrue(clusterAddon, csov1alpha1.GitAPIAvailableCondition) - logger.Info("github client initialized") // check if old cluster stack release is present or not. releaseAsset, download, err := release.New(release.ConvertFromClusterClassToClusterStackFormat(clusterAddon.Spec.ClusterStack), r.ReleaseDirectory) @@ -645,9 +651,7 @@ func (r *ClusterAddonReconciler) downloadOldClusterStackRelease(ctx context.Cont return "", true, nil } if download { - logger.Info("the old cluster stack is not present", "clusterstack", clusterAddon.Spec.ClusterStack) // if download is true, it means that the release assets have not been downloaded yet - conditions.MarkFalse(clusterAddon, csov1alpha1.ClusterStackReleaseAssetsReadyCondition, csov1alpha1.ReleaseAssetsNotDownloadedYetReason, clusterv1.ConditionSeverityInfo, "assets not downloaded yet") // this is the point where we download the release from github @@ -684,8 +688,6 @@ func (r *ClusterAddonReconciler) downloadOldClusterStackRelease(ctx context.Cont } func helmTemplateAndApplyNewClusterStack(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string) (bool, error) { - logger := log.FromContext(ctx) - var ( buildTemplate []byte oldHelmTemplate []byte @@ -698,7 +700,6 @@ func helmTemplateAndApplyNewClusterStack(ctx context.Context, in templateAndAppl if err != nil { return false, fmt.Errorf("failed to template old helm chart: %w", err) } - logger.Info("oldClusterStackAddonChartPath v1", "path", oldClusterStackSubDirPath) } newClusterStackSubDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName) @@ -706,7 +707,6 @@ func helmTemplateAndApplyNewClusterStack(ctx context.Context, in templateAndAppl if err != nil { return false, fmt.Errorf("failed to template new helm chart: %w", err) } - logger.Info("newClusterStackSubDirPath v1", "path", newClusterStackSubDirPath) shouldRequeue, err := in.kubeClient.ApplyNewClusterStack(ctx, oldHelmTemplate, newHelmTemplate) if err != nil { @@ -717,28 +717,16 @@ func helmTemplateAndApplyNewClusterStack(ctx context.Context, in templateAndAppl } func helmTemplateAndDeleteNewClusterStack(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string) (bool, error) { - // logger := log.FromContext(ctx) var ( buildTemplate []byte - // oldHelmTemplate []byte - err error + err error ) - // if in.oldDestinationClusterAddonChartDir != "" { - // oldClusterStackSubDirPath := filepath.Join(in.oldDestinationClusterAddonChartDir, helmChartName) - // oldHelmTemplate, err = helmTemplateClusterAddon(oldClusterStackSubDirPath, buildTemplate, true) - // if err != nil { - // return false, fmt.Errorf("failed to template old helm chart: %w", err) - // } - // logger.Info("oldClusterStackAddonChartPath v1", "path", oldClusterStackSubDirPath) - // } - newClusterStackSubDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName) newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, buildTemplate, true) if err != nil { return false, fmt.Errorf("failed to template new helm chart: %w", err) } - // logger.Info("newClusterStackSubDirPath v1", "path", newClusterStackSubDirPath) shouldRequeue, err := in.kubeClient.DeleteNewClusterStack(ctx, newHelmTemplate) if err != nil { diff --git a/pkg/kube/kube.go b/pkg/kube/kube.go index 19807435b..67d7799e9 100644 --- a/pkg/kube/kube.go +++ b/pkg/kube/kube.go @@ -74,22 +74,13 @@ func (k *kube) ApplyNewClusterStack(ctx context.Context, oldTemplate, newTemplat if err != nil { return false, fmt.Errorf("failed to parse old cluster stack template: %w", err) } - logger.Info("oldObjects value", "val", oldObjects) newObjects, err := parseK8sYaml(newTemplate) if err != nil { return false, fmt.Errorf("failed to parse new cluster stack template: %w", err) } - // oldObjectMap := getResourceMapOfUnstructuredObjects(oldObjects) for _, newObject := range newObjects { - // do nothing if found - // if _, found := oldObjectMap[types.NamespacedName{Name: newObject.GetName(), Namespace: newObject.GetNamespace()}]; found { - // continue - // } - - logger.Info("object to be applied", "object", newObject.GetName(), "kind", newObject.GetKind()) - if err := setLabel(newObject, ObjectLabelKeyOwned, ObjectLabelValueOwned); err != nil { return false, fmt.Errorf("error setting label: %w", err) } @@ -112,8 +103,6 @@ func (k *kube) ApplyNewClusterStack(ctx context.Context, oldTemplate, newTemplat } for _, object := range resourcesToBeDeletedFromUnstructuredObjects(oldObjects, newObjects) { - logger.Info("resource are being deleted", "kind", object.GetKind(), "name", object.GetName(), "namespace", object.GetNamespace()) - dr, err := getDynamicResourceInterface(k.Namespace, k.RestConfig, object.GroupVersionKind()) if err != nil { return false, fmt.Errorf("failed to get dynamic resource interface: %w", err) @@ -131,27 +120,12 @@ func (k *kube) ApplyNewClusterStack(ctx context.Context, oldTemplate, newTemplat } func (k *kube) DeleteNewClusterStack(ctx context.Context, template []byte) (shouldRequeue bool, err error) { - logger := log.FromContext(ctx) - - // oldObjects, err := parseK8sYaml(oldTemplate) - // if err != nil { - // return false, fmt.Errorf("failed to parse old cluster stack template: %w", err) - // } - objects, err := parseK8sYaml(template) if err != nil { return false, fmt.Errorf("failed to parse new cluster stack template: %w", err) } - // oldObjectMap := getResourceMapOfUnstructuredObjects(objects) for _, object := range objects { - // do nothing if synced - // if _, found := oldObjectMap[types.NamespacedName{Name: newObject.GetName(), Namespace: newObject.GetNamespace()}]; found { - // continue - // } - - logger.Info("object to be deleted", "object", object.GetName(), "kind", object.GetKind()) - if err := setLabel(object, ObjectLabelKeyOwned, ObjectLabelValueOwned); err != nil { return false, fmt.Errorf("error setting label: %w", err) }