Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add debug logs for the multi stage addon #141

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
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 @@
}

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 @@
// 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 @@ -476,7 +471,7 @@
return addonStages, nil
}

func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx context.Context, in templateAndApplyClusterAddonInput, shouldDelete bool) (bool, error) {

Check failure on line 474 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

hugeParam: in is heavy (144 bytes); consider passing it by pointer (gocritic)
clusterAddonChart := in.clusterAddonChartPath
var shouldRequeue bool

Expand All @@ -501,7 +496,9 @@
return shouldRequeue, nil
}

func (r *ClusterAddonReconciler) executeStage(ctx context.Context, stage clusteraddon.Stage, in templateAndApplyClusterAddonInput) (bool, error) {

Check failure on line 499 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

hugeParam: stage is heavy (112 bytes); consider passing it by pointer (gocritic)
logger := log.FromContext(ctx)

var (
shouldRequeue bool
err error
Expand All @@ -524,9 +521,10 @@
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 @@ -543,12 +541,14 @@
}
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 @@ -564,12 +564,14 @@

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 @@ -585,14 +587,17 @@

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 @@ -607,15 +612,17 @@
}
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) {

Check failure on line 625 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

unnamedResult: consider giving a name to these results (gocritic)
// initiate github release.
gc, err := r.GitHubClientFactory.NewClient(ctx)
if err != nil {
Expand All @@ -636,7 +643,6 @@
}

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 @@ -645,9 +651,7 @@
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 @@ -683,9 +687,7 @@
return releaseAsset.ClusterAddonChartPath(), false, nil
}

func helmTemplateAndApplyNewClusterStack(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string) (bool, error) {

Check failure on line 690 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

hugeParam: in is heavy (144 bytes); consider passing it by pointer (gocritic)
logger := log.FromContext(ctx)

var (
buildTemplate []byte
oldHelmTemplate []byte
Expand All @@ -698,15 +700,13 @@
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 @@ -716,29 +716,17 @@
return shouldRequeue, nil
}

func helmTemplateAndDeleteNewClusterStack(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string) (bool, error) {

Check failure on line 719 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

hugeParam: in is heavy (144 bytes); consider passing it by pointer (gocritic)
// 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 All @@ -748,7 +736,7 @@
return shouldRequeue, nil
}

func helmTemplateAndApply(ctx context.Context, kubeClient kube.Client, in templateAndApplyClusterAddonInput, helmChartName string, buildTemplate []byte, shouldDelete bool) ([]*csov1alpha1.Resource, bool, error) {

Check failure on line 739 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

hugeParam: in is heavy (144 bytes); consider passing it by pointer (gocritic)
subDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName)
loggger := log.FromContext(ctx)

Expand All @@ -766,7 +754,7 @@
return newResources, shouldRequeue, nil
}

func helmTemplateAndDelete(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string, buildTemplate []byte) ([]*csov1alpha1.Resource, bool, error) {

Check failure on line 757 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

hugeParam: in is heavy (144 bytes); consider passing it by pointer (gocritic)
subDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName)

helmTemplate, err := helmTemplateClusterAddon(subDirPath, buildTemplate, true)
Expand All @@ -783,7 +771,7 @@
}

func getDynamicResourceAndEvaluateCEL(ctx context.Context, dynamicClient *dynamic.DynamicClient, discoveryClient *discovery.DiscoveryClient, waitCondition clusteraddon.WaitForCondition) error {
var evalMap = make(map[string]interface{})

Check failure on line 774 in internal/controller/clusteraddon_controller.go

View workflow job for this annotation

GitHub Actions / Lint Pull Request

File is not `gofumpt`-ed with `-extra` (gofumpt)

env, err := cel.NewEnv()
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
Loading