Skip to content

Commit 3b7c444

Browse files
committed
🐛 Fix overwrite yaml support
Fix overwrite yaml support Signed-off-by: janiskemper <[email protected]>
1 parent 2b9f050 commit 3b7c444

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

internal/controller/clusteraddon_controller.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
370370
for _, stage := range clusterAddonConfig.AddonStages[clusterAddon.Spec.Hook] {
371371
shouldRequeue, err := r.executeStage(ctx, stage, in)
372372
if err != nil {
373-
return reconcile.Result{}, fmt.Errorf("failed to execute stage: %w", err)
373+
return reconcile.Result{}, fmt.Errorf("failed to execute stage: %q: %w", stage.HelmChartName, err)
374374
}
375375
if shouldRequeue {
376376
return reconcile.Result{RequeueAfter: 20 * time.Second}, nil
@@ -480,12 +480,12 @@ func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx conte
480480
clusterAddonChart := in.clusterAddonChartPath
481481
var shouldRequeue bool
482482

483-
buildTemplate, err := buildTemplateFromClusterAddonValues(ctx, in.clusterAddonValuesPath, in.cluster, r.Client)
483+
buildTemplate, err := buildTemplateFromClusterAddonValues(ctx, in.clusterAddonValuesPath, in.cluster, r.Client, false)
484484
if err != nil {
485485
return false, fmt.Errorf("failed to build template from cluster addon values: %w", err)
486486
}
487487

488-
helmTemplate, err := helmTemplateClusterAddon(clusterAddonChart, buildTemplate, false)
488+
helmTemplate, err := helmTemplateClusterAddon(clusterAddonChart, buildTemplate)
489489
if err != nil {
490490
return false, fmt.Errorf("failed to template helm chart: %w", err)
491491
}
@@ -554,7 +554,7 @@ check:
554554
case csov1alpha1.ApplyingOrDeleting:
555555
if stage.Action == clusteraddon.Apply {
556556
logger.V(1).Info("starting to apply helm chart", "clusterStack", in.clusterAddon.Spec.ClusterStack, "helm chart", stage.HelmChartName, "hook", in.clusterAddon.Spec.Hook)
557-
shouldRequeue, err = helmTemplateAndApplyNewClusterStack(ctx, in, stage.HelmChartName)
557+
shouldRequeue, err = r.templateAndApplyNewClusterStackAddonHelmChart(ctx, in, stage.HelmChartName)
558558
if err != nil {
559559
return false, fmt.Errorf("failed to helm template and apply: %w", err)
560560
}
@@ -698,23 +698,40 @@ func (r *ClusterAddonReconciler) downloadOldClusterStackRelease(ctx context.Cont
698698
return releaseAsset.ClusterAddonChartPath(), false, nil
699699
}
700700

701-
func helmTemplateAndApplyNewClusterStack(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string) (bool, error) {
701+
func (r *ClusterAddonReconciler) templateAndApplyNewClusterStackAddonHelmChart(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string) (bool, error) {
702702
var (
703-
buildTemplate []byte
704-
oldHelmTemplate []byte
705-
err error
703+
oldHelmTemplate []byte
704+
oldBuildTemplate []byte
705+
newBuildTemplate []byte
706+
err error
706707
)
707708

708709
if in.oldDestinationClusterAddonChartDir != "" {
709710
oldClusterStackSubDirPath := filepath.Join(in.oldDestinationClusterAddonChartDir, helmChartName)
710-
oldHelmTemplate, err = helmTemplateClusterAddon(oldClusterStackSubDirPath, buildTemplate, true)
711+
712+
if _, err := os.Stat(filepath.Join(oldClusterStackSubDirPath, release.OverwriteYaml)); err == nil {
713+
oldBuildTemplate, err = buildTemplateFromClusterAddonValues(ctx, filepath.Join(oldClusterStackSubDirPath, release.OverwriteYaml), in.cluster, r.Client, true)
714+
if err != nil {
715+
return false, fmt.Errorf("failed to build template from old cluster addon values: %w", err)
716+
}
717+
}
718+
719+
oldHelmTemplate, err = helmTemplateClusterAddon(oldClusterStackSubDirPath, oldBuildTemplate)
711720
if err != nil {
712721
return false, fmt.Errorf("failed to template old helm chart: %w", err)
713722
}
714723
}
715724

716725
newClusterStackSubDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName)
717-
newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, buildTemplate, true)
726+
727+
if _, err := os.Stat(filepath.Join(newClusterStackSubDirPath, release.OverwriteYaml)); err == nil {
728+
newBuildTemplate, err = buildTemplateFromClusterAddonValues(ctx, filepath.Join(newClusterStackSubDirPath, release.OverwriteYaml), in.cluster, r.Client, true)
729+
if err != nil {
730+
return false, fmt.Errorf("failed to build template from new cluster addon values: %w", err)
731+
}
732+
}
733+
734+
newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, newBuildTemplate)
718735
if err != nil {
719736
return false, fmt.Errorf("failed to template new helm chart: %w", err)
720737
}
@@ -736,7 +753,7 @@ func helmTemplateAndDeleteNewClusterStack(ctx context.Context, in templateAndApp
736753
)
737754

738755
newClusterStackSubDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName)
739-
newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, buildTemplate, true)
756+
newHelmTemplate, err := helmTemplateClusterAddon(newClusterStackSubDirPath, buildTemplate)
740757
if err != nil {
741758
return false, fmt.Errorf("failed to template new helm chart: %w", err)
742759
}
@@ -756,7 +773,7 @@ func helmTemplateAndApply(ctx context.Context, kubeClient kube.Client, in templa
756773
loggger := log.FromContext(ctx)
757774

758775
loggger.Info("path of the subdir", "path", subDirPath)
759-
helmTemplate, err := helmTemplateClusterAddon(subDirPath, buildTemplate, true)
776+
helmTemplate, err := helmTemplateClusterAddon(subDirPath, buildTemplate)
760777
if err != nil {
761778
return nil, false, fmt.Errorf("failed to template helm chart: %w", err)
762779
}
@@ -772,7 +789,7 @@ func helmTemplateAndApply(ctx context.Context, kubeClient kube.Client, in templa
772789
func helmTemplateAndDelete(ctx context.Context, in templateAndApplyClusterAddonInput, helmChartName string, buildTemplate []byte) ([]*csov1alpha1.Resource, bool, error) {
773790
subDirPath := filepath.Join(in.newDestinationClusterAddonChartDir, helmChartName)
774791

775-
helmTemplate, err := helmTemplateClusterAddon(subDirPath, buildTemplate, true)
792+
helmTemplate, err := helmTemplateClusterAddon(subDirPath, buildTemplate)
776793
if err != nil {
777794
return nil, false, fmt.Errorf("failed to template helm chart: %w", err)
778795
}
@@ -854,7 +871,7 @@ func getDynamicResourceAndEvaluateCEL(ctx context.Context, dynamicClient *dynami
854871
return nil
855872
}
856873

857-
func buildTemplateFromClusterAddonValues(ctx context.Context, addonValuePath string, cluster *clusterv1.Cluster, c client.Client) ([]byte, error) {
874+
func buildTemplateFromClusterAddonValues(ctx context.Context, addonValuePath string, cluster *clusterv1.Cluster, c client.Client, newWay bool) ([]byte, error) {
858875
data, err := os.ReadFile(filepath.Clean(addonValuePath))
859876
if err != nil {
860877
return nil, fmt.Errorf("failed to read the file %s: %w", addonValuePath, err)
@@ -895,14 +912,13 @@ func buildTemplateFromClusterAddonValues(ctx context.Context, addonValuePath str
895912
return nil, fmt.Errorf("failed to execute template string %q on cluster %q: %w", string(data), cluster.GetName(), err)
896913
}
897914

898-
expandedTemplate := buffer.String()
899-
var unmarhallData interface{}
900-
err = yaml.Unmarshal([]byte(expandedTemplate), &unmarhallData)
915+
var unmarshalData interface{}
916+
err = yaml.Unmarshal(buffer.Bytes(), &unmarshalData)
901917
if err != nil {
902918
return nil, fmt.Errorf("unmarshal expanded template: %w", err)
903919
}
904920

905-
values, ok := unmarhallData.(map[interface{}]interface{})["values"].(string)
921+
values, ok := unmarshalData.(map[string]interface{})["values"].(string)
906922
if !ok {
907923
return nil, fmt.Errorf("key 'values' not found in template of cluster addon helm chart")
908924
}
@@ -915,7 +931,7 @@ func buildTemplateFromClusterAddonValues(ctx context.Context, addonValuePath str
915931
// Then it returns the path of the generated yaml file.
916932
// Example: helm template /tmp/downloads/cluster-stacks/myprovider-myclusterstack-1-26-v2/myprovider-myclusterstack-1-26-v2.tgz
917933
// The return yaml file path will be /tmp/downloads/cluster-stacks/myprovider-myclusterstack-1-26-v2/myprovider-myclusterstack-1-26-v2.tgz.yaml.
918-
func helmTemplateClusterAddon(chartPath string, helmTemplate []byte, newWay bool) ([]byte, error) {
934+
func helmTemplateClusterAddon(chartPath string, helmTemplate []byte) ([]byte, error) {
919935
helmCommand := "helm"
920936
helmArgs := []string{"template", "--include-crds"}
921937

@@ -928,9 +944,7 @@ func helmTemplateClusterAddon(chartPath string, helmTemplate []byte, newWay bool
928944
helmTemplateCmd.Stderr = os.Stderr
929945
helmTemplateCmd.Dir = filepath.Dir(chartPath)
930946
helmTemplateCmd.Stdout = &cmdOutput
931-
if !newWay {
932-
helmTemplateCmd.Stdin = input
933-
}
947+
helmTemplateCmd.Stdin = input
934948

935949
if err := helmTemplateCmd.Run(); err != nil {
936950
return nil, fmt.Errorf("failed to run helm template for %q: %w", chartPath, err)

pkg/release/release.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ const (
5454

5555
metadataFileName = "metadata.yaml"
5656
clusterAddonValuesName = "cluster-addon-values.yaml"
57+
58+
// OverwriteYaml is the new cluster stack overwrite yaml.
59+
OverwriteYaml = "overwrite.yaml"
5760
)
5861

5962
// New returns a new release.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
metrics-server:
2-
commonLabels:
3-
domain: "{{ .Cluster.spec.controlPlaneEndpoint.host }}"
4-
clusterAddonVersion: "v2"
1+
values: |
2+
metrics-server:
3+
commonLabels:
4+
domain: "{{ .Cluster.spec.controlPlaneEndpoint.host }}"
5+
clusterAddonVersion: "v2"
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
metrics-server:
2-
commonLabels:
3-
domain: "{{ .Cluster.spec.controlPlaneEndpoint.host }}"
4-
clusterAddonVersion: "v2"
1+
values: |
2+
metrics-server:
3+
commonLabels:
4+
domain: "{{ .Cluster.spec.controlPlaneEndpoint.host }}"
5+
clusterAddonVersion: "v2"

0 commit comments

Comments
 (0)