Skip to content

Commit 560cea9

Browse files
committed
Include Kubernetes version in helm template
Signed-off-by: Roman Hros <[email protected]>
1 parent 22cc6d7 commit 560cea9

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

internal/controller/clusteraddon_controller.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
216216
in := &templateAndApplyClusterAddonInput{
217217
clusterAddonChartPath: releaseAsset.ClusterAddonChartPath(),
218218
clusterAddonValuesPath: releaseAsset.ClusterAddonValuesPath(),
219+
kubernetesVersion: releaseAsset.Meta.Versions.Kubernetes,
219220
clusterAddon: clusterAddon,
220221
cluster: cluster,
221222
restConfig: restConfig,
@@ -323,6 +324,7 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
323324
// src - /tmp/cluster-stacks/docker-ferrol-1-27-v1/docker-ferrol-1-27-cluster-addon-v1.tgz
324325
// dst - /tmp/cluster-stacks/docker-ferrol-1-27-v1/docker-ferrol-1-27-cluster-addon-v1/
325326
in.oldDestinationClusterAddonChartDir = strings.TrimSuffix(oldRelease.ClusterAddonChartPath(), ".tgz")
327+
in.oldKubernetesVersion = oldRelease.Meta.Versions.Kubernetes
326328

327329
if err := unTarContent(oldRelease.ClusterAddonChartPath(), in.oldDestinationClusterAddonChartDir); err != nil {
328330
return reconcile.Result{}, fmt.Errorf("failed to untar old cluster stack cluster addon chart: %q: %w", oldRelease.ClusterAddonChartPath(), err)
@@ -512,7 +514,7 @@ func (r *ClusterAddonReconciler) getNewReleaseObjects(ctx context.Context, in *t
512514
}
513515
}
514516

515-
helmTemplate, err := helmTemplateClusterAddon(filepath.Join(in.newDestinationClusterAddonChartDir, stage.Name), newBuildTemplate)
517+
helmTemplate, err := helmTemplateClusterAddon(filepath.Join(in.newDestinationClusterAddonChartDir, stage.Name), newBuildTemplate, in.kubernetesVersion)
516518
if err != nil {
517519
return nil, fmt.Errorf("failed to template new helm chart of the latest cluster stack: %w", err)
518520
}
@@ -551,7 +553,7 @@ func (r *ClusterAddonReconciler) getOldReleaseObjects(ctx context.Context, in *t
551553
return nil, fmt.Errorf("failed to build template from the old cluster stack cluster addon values: %w", err)
552554
}
553555

554-
helmTemplate, err := helmTemplateClusterAddon(oldRelease.ClusterAddonChartPath(), buildTemplate)
556+
helmTemplate, err := helmTemplateClusterAddon(oldRelease.ClusterAddonChartPath(), buildTemplate, oldRelease.Meta.Versions.Kubernetes)
555557
if err != nil {
556558
return nil, fmt.Errorf("failed to template helm chart: %w", err)
557559
}
@@ -588,7 +590,7 @@ func (r *ClusterAddonReconciler) getOldReleaseObjects(ctx context.Context, in *t
588590
}
589591
}
590592

591-
helmTemplate, err := helmTemplateClusterAddon(filepath.Join(in.oldDestinationClusterAddonChartDir, stage.Name), newBuildTemplate)
593+
helmTemplate, err := helmTemplateClusterAddon(filepath.Join(in.oldDestinationClusterAddonChartDir, stage.Name), newBuildTemplate, oldRelease.Meta.Versions.Kubernetes)
592594
if err != nil {
593595
return nil, fmt.Errorf("failed to template new helm chart: %w", err)
594596
}
@@ -664,6 +666,8 @@ type templateAndApplyClusterAddonInput struct {
664666
// clusteraddon.yaml
665667
clusterAddonConfigPath string
666668
clusterAddon *csov1alpha1.ClusterAddon
669+
kubernetesVersion string
670+
oldKubernetesVersion string
667671
cluster *clusterv1.Cluster
668672
restConfig *rest.Config
669673
addonStagesInput
@@ -727,7 +731,7 @@ func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx conte
727731
return false, fmt.Errorf("failed to build template from cluster addon values: %w", err)
728732
}
729733

730-
helmTemplate, err := helmTemplateClusterAddon(clusterAddonChart, buildTemplate)
734+
helmTemplate, err := helmTemplateClusterAddon(clusterAddonChart, buildTemplate, in.kubernetesVersion)
731735
if err != nil {
732736
return false, fmt.Errorf("failed to template helm chart: %w", err)
733737
}
@@ -1017,7 +1021,7 @@ func (r *ClusterAddonReconciler) templateNewClusterStackAddonHelmChart(ctx conte
10171021
return true, nil, nil, nil
10181022
}
10191023

1020-
oldHelmTemplate, err = helmTemplateClusterAddon(oldClusterStackSubDirPath, oldBuildTemplate)
1024+
oldHelmTemplate, err = helmTemplateClusterAddon(oldClusterStackSubDirPath, oldBuildTemplate, in.oldKubernetesVersion)
10211025
if err != nil {
10221026
conditions.MarkFalse(
10231027
in.clusterAddon,
@@ -1061,7 +1065,7 @@ func (r *ClusterAddonReconciler) templateNewClusterStackAddonHelmChart(ctx conte
10611065
}
10621066
}
10631067

1064-
newHelmTemplate, err = helmTemplateClusterAddon(newClusterStackSubDirPath, newBuildTemplate)
1068+
newHelmTemplate, err = helmTemplateClusterAddon(newClusterStackSubDirPath, newBuildTemplate, in.kubernetesVersion)
10651069
if err != nil {
10661070
conditions.MarkFalse(
10671071
in.clusterAddon,
@@ -1088,7 +1092,7 @@ func helmTemplateNewClusterStack(in *templateAndApplyClusterAddonInput, name str
10881092
var buildTemplate []byte
10891093

10901094
newClusterStackSubDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, name)
1091-
newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, buildTemplate)
1095+
newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, buildTemplate, in.kubernetesVersion)
10921096
if err != nil {
10931097
return nil, fmt.Errorf("failed to template new helm chart: %w", err)
10941098
}
@@ -1231,15 +1235,15 @@ func buildTemplateFromClusterAddonValues(ctx context.Context, addonValuePath str
12311235
// Then it returns the path of the generated yaml file.
12321236
// Example: helm template /tmp/downloads/cluster-stacks/myprovider-myclusterstack-1-26-v2/myprovider-myclusterstack-1-26-v2.tgz
12331237
// The return yaml file path will be /tmp/downloads/cluster-stacks/myprovider-myclusterstack-1-26-v2/myprovider-myclusterstack-1-26-v2.tgz.yaml.
1234-
func helmTemplateClusterAddon(chartPath string, helmTemplate []byte) ([]byte, error) {
1238+
func helmTemplateClusterAddon(chartPath string, helmTemplate []byte, kubernetesVersion string) ([]byte, error) {
12351239
helmCommand := "helm"
12361240
helmArgs := []string{"template", "--include-crds"}
12371241

12381242
input := bytes.NewBuffer(helmTemplate)
12391243

12401244
var cmdOutput bytes.Buffer
12411245

1242-
helmArgs = append(helmArgs, "cluster-addon", filepath.Base(chartPath), "--namespace", clusterAddonNamespace, "-f", "-")
1246+
helmArgs = append(helmArgs, "--kube-version", kubernetesVersion, "cluster-addon", filepath.Base(chartPath), "--namespace", clusterAddonNamespace, "-f", "-")
12431247
helmTemplateCmd := exec.Command(helmCommand, helmArgs...)
12441248
helmTemplateCmd.Stderr = os.Stderr
12451249
helmTemplateCmd.Dir = filepath.Dir(chartPath)

0 commit comments

Comments
 (0)