Skip to content

Commit

Permalink
Add debug logs for the multi stage addon
Browse files Browse the repository at this point in the history
Add debug log information in the addon controller

Signed-off-by: Aniruddha Basak <aniruddha.basak@syself.com>
  • Loading branch information
aniruddha2000 authored and janiskemper committed Apr 30, 2024
1 parent d52242c commit 5fb5539
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 55 deletions.
46 changes: 17 additions & 29 deletions internal/controller/clusteraddon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -496,6 +491,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
Expand All @@ -518,9 +515,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(
Expand All @@ -537,12 +535,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)
Expand All @@ -558,12 +558,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)
Expand All @@ -579,14 +581,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(
Expand All @@ -601,15 +606,17 @@ 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
}

return false, nil
}

// 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 {
Expand All @@ -630,7 +637,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)
Expand All @@ -639,9 +645,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
Expand Down Expand Up @@ -678,8 +682,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
Expand All @@ -692,15 +694,13 @@ 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)
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.ApplyNewClusterStack(ctx, oldHelmTemplate, newHelmTemplate)
if err != nil {
Expand All @@ -711,28 +711,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 {
Expand Down
26 changes: 0 additions & 26 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit 5fb5539

Please sign in to comment.