From c498af3f525f7b8d1ecc0d4b71bd23db262311f5 Mon Sep 17 00:00:00 2001 From: xiaomudk Date: Wed, 24 May 2023 07:29:19 +0800 Subject: [PATCH] Fix set flag are ignored when used in conjunction with jsonPatches (#867) * Fix set flag are ignored when used in conjunction with jsonPatches Signed-off-by: xiaomudk --- pkg/app/app.go | 1 + pkg/state/state.go | 6 ++ test/integration/run.sh | 1 + test/integration/test-cases/chartify.sh | 27 ++++++++ .../test-cases/chartify/input/helmfile.yaml | 14 +++++ .../test-cases/chartify/output/template | 61 +++++++++++++++++++ .../test-cases/chartify/output/template-set | 61 +++++++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 test/integration/test-cases/chartify.sh create mode 100644 test/integration/test-cases/chartify/input/helmfile.yaml create mode 100644 test/integration/test-cases/chartify/output/template create mode 100644 test/integration/test-cases/chartify/output/template-set diff --git a/pkg/app/app.go b/pkg/app/app.go index d1e1f6c5..26b8fcd6 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -251,6 +251,7 @@ func (a *App) Template(c TemplateConfigProvider) error { Validate: c.Validate(), Concurrency: c.Concurrency(), IncludeTransitiveNeeds: c.IncludeNeeds(), + Set: c.Set(), }, func() { ok, errs = a.template(run, c) }) diff --git a/pkg/state/state.go b/pkg/state/state.go index 0cbaf1b6..8da1755c 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1052,6 +1052,7 @@ type ChartPrepareOptions struct { IncludeTransitiveNeeds bool Concurrency int KubeVersion string + Set []string } type chartPrepareResult struct { @@ -1225,6 +1226,11 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre chartifyOpts.KubeVersion = release.KubeVersion chartifyOpts.ApiVersions = release.ApiVersions + var flags []string + for _, s := range opts.Set { + flags = append(flags, "--set", s) + } + chartifyOpts.SetFlags = flags out, err := c.Chartify(release.Name, chartPath, chartify.WithChartifyOpts(chartifyOpts)) if err != nil { diff --git a/test/integration/run.sh b/test/integration/run.sh index 5c32e3ab..9949a4b7 100755 --- a/test/integration/run.sh +++ b/test/integration/run.sh @@ -84,6 +84,7 @@ ${kubectl} create namespace ${test_ns} || fail "Could not create namespace ${tes . ${dir}/test-cases/yaml-overwrite.sh . ${dir}/test-cases/chart-needs.sh . ${dir}/test-cases/postrender.sh +. ${dir}/test-cases/chartify.sh # ALL DONE ----------------------------------------------------------------------------------------------------------- diff --git a/test/integration/test-cases/chartify.sh b/test/integration/test-cases/chartify.sh new file mode 100644 index 00000000..581e2d00 --- /dev/null +++ b/test/integration/test-cases/chartify.sh @@ -0,0 +1,27 @@ +chartify_case_input_dir="${cases_dir}/chartify/input" +chartify_case_output_dir="${cases_dir}/chartify/output" + +config_file="helmfile.yaml" +if [[ ${HELMFILE_V1MODE} = true ]]; then + pushd "${chartify_case_input_dir}" + mv "${config_file}" "${config_file}.gotmpl" + config_file="${config_file}.gotmpl" + popd +fi + +chartify_tmp=$(mktemp -d) +chartify_template_reverse=${chartify_tmp}/chartify.template.log + + +test_start "helmfile template with chartify" + +info "Comparing template/chartify" +${helmfile} -f ${chartify_case_input_dir}/${config_file} template > ${chartify_template_reverse} || fail "\"helmfile template\" shouldn't fail" +./dyff between -bs ${chartify_case_output_dir}/template ${chartify_template_reverse} || fail "\"helmfile template\" should be consistent" + +info "Comparing template/chartify with set" +${helmfile} -f ${chartify_case_input_dir}/${config_file} template --set image.tag=v2 > ${chartify_template_reverse} || fail "\"helmfile template\" shouldn't fail" +./dyff between -bs ${chartify_case_output_dir}/template-set ${chartify_template_reverse} || fail "\"helmfile template\" should be consistent" + + +test_pass "helmfile template with chartify" \ No newline at end of file diff --git a/test/integration/test-cases/chartify/input/helmfile.yaml b/test/integration/test-cases/chartify/input/helmfile.yaml new file mode 100644 index 00000000..d2285856 --- /dev/null +++ b/test/integration/test-cases/chartify/input/helmfile.yaml @@ -0,0 +1,14 @@ +releases: + - name: httpbin + chart: ../../../charts/httpbin + jsonPatches: + - target: + group: apps + version: v1 + kind: Deployment + name: httpbin-httpbin + patch: + - path: "/spec/strategy/type" + op: replace + value: Recreate + diff --git a/test/integration/test-cases/chartify/output/template b/test/integration/test-cases/chartify/output/template new file mode 100644 index 00000000..3424a8d0 --- /dev/null +++ b/test/integration/test-cases/chartify/output/template @@ -0,0 +1,61 @@ +--- +# Source: httpbin/templates/patched_resources.yaml +apiVersion: v1 +kind: Service +metadata: + labels: + app: httpbin + chart: httpbin-0.1.0 + heritage: Helm + release: httpbin + name: httpbin-httpbin +spec: + ports: + - name: httpbin + port: 8000 + protocol: TCP + targetPort: 8000 + selector: + app: httpbin + release: httpbin + type: LoadBalancer +--- +# Source: httpbin/templates/patched_resources.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: httpbin + chart: httpbin-0.1.0 + heritage: Helm + release: httpbin + name: httpbin-httpbin +spec: + replicas: 1 + selector: + matchLabels: + app: httpbin + strategy: + type: Recreate + template: + metadata: + labels: + app: httpbin + release: httpbin + spec: + containers: + - image: docker.io/citizenstig/httpbin:latest + imagePullPolicy: IfNotPresent + livenessProbe: + httpGet: + path: / + port: 8000 + name: httpbin + ports: + - containerPort: 8000 + readinessProbe: + httpGet: + path: / + port: 8000 + resources: {} +status: {} \ No newline at end of file diff --git a/test/integration/test-cases/chartify/output/template-set b/test/integration/test-cases/chartify/output/template-set new file mode 100644 index 00000000..7e226a7c --- /dev/null +++ b/test/integration/test-cases/chartify/output/template-set @@ -0,0 +1,61 @@ +--- +# Source: httpbin/templates/patched_resources.yaml +apiVersion: v1 +kind: Service +metadata: + labels: + app: httpbin + chart: httpbin-0.1.0 + heritage: Helm + release: httpbin + name: httpbin-httpbin +spec: + ports: + - name: httpbin + port: 8000 + protocol: TCP + targetPort: 8000 + selector: + app: httpbin + release: httpbin + type: LoadBalancer +--- +# Source: httpbin/templates/patched_resources.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: httpbin + chart: httpbin-0.1.0 + heritage: Helm + release: httpbin + name: httpbin-httpbin +spec: + replicas: 1 + selector: + matchLabels: + app: httpbin + strategy: + type: Recreate + template: + metadata: + labels: + app: httpbin + release: httpbin + spec: + containers: + - image: docker.io/citizenstig/httpbin:v2 + imagePullPolicy: IfNotPresent + livenessProbe: + httpGet: + path: / + port: 8000 + name: httpbin + ports: + - containerPort: 8000 + readinessProbe: + httpGet: + path: / + port: 8000 + resources: {} +status: {} \ No newline at end of file