feat: inject cli state values (--state-values-set) into environment templating context (#1917)
* feat: inject cli state values (--state-values-set) into environment templating context Signed-off-by: Vincent Chenal <vincent.chenal@protonmail.com> * test: added envvals_loader unit test for environment values Signed-off-by: Vincent Chenal <vincent.chenal@protonmail.com> * test: added 'state values set cli args in environments' integration test Signed-off-by: Vincent Chenal <vincent.chenal@protonmail.com> * fix: merge environments before loadValuesEntries Signed-off-by: Vincent Chenal <vincent.chenal@protonmail.com> * fix: 'state values set cli args in environments' integration test Signed-off-by: Vincent Chenal <vincent.chenal@protonmail.com> --------- Signed-off-by: Vincent Chenal <vincent.chenal@protonmail.com>
This commit is contained in:
		
							parent
							
								
									0d863b3c05
								
							
						
					
					
						commit
						e33b4725ea
					
				| 
						 | 
					@ -278,7 +278,11 @@ func (c *StateCreator) loadEnvValues(st *HelmState, name string, failOnMissingEn
 | 
				
			||||||
			valuesFiles = append(valuesFiles, f)
 | 
								valuesFiles = append(valuesFiles, f)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		envValuesEntries := append(valuesFiles, envSpec.Values...)
 | 
							envValuesEntries := append(valuesFiles, envSpec.Values...)
 | 
				
			||||||
		valuesVals, err = st.loadValuesEntries(envSpec.MissingFileHandler, envValuesEntries, c.remote, ctxEnv, name)
 | 
							loadValuesEntriesEnv, err := ctxEnv.Merge(overrode)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							valuesVals, err = st.loadValuesEntries(envSpec.MissingFileHandler, envValuesEntries, c.remote, loadValuesEntriesEnv, name)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,7 +65,7 @@ func (ld *EnvironmentValuesLoader) LoadEnvironmentValues(missingFileHandler *str
 | 
				
			||||||
				if strings.HasSuffix(f, ".hcl") {
 | 
									if strings.HasSuffix(f, ".hcl") {
 | 
				
			||||||
					hclLoader.AddFile(f)
 | 
										hclLoader.AddFile(f)
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					tmplData := NewEnvironmentTemplateData(env, "", map[string]any{})
 | 
										tmplData := NewEnvironmentTemplateData(env, "", env.Values)
 | 
				
			||||||
					r := tmpl.NewFileRenderer(ld.fs, filepath.Dir(f), tmplData)
 | 
										r := tmpl.NewFileRenderer(ld.fs, filepath.Dir(f), tmplData)
 | 
				
			||||||
					bytes, err := r.RenderToBytes(f)
 | 
										bytes, err := r.RenderToBytes(f)
 | 
				
			||||||
					if err != nil {
 | 
										if err != nil {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -228,3 +228,22 @@ func TestEnvValsLoad_MultiHCL(t *testing.T) {
 | 
				
			||||||
		t.Error(diff)
 | 
							t.Error(diff)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestEnvValsLoad_EnvironmentValues(t *testing.T) {
 | 
				
			||||||
 | 
						l := newLoader()
 | 
				
			||||||
 | 
						env := environment.New("test")
 | 
				
			||||||
 | 
						env.Values["foo"] = "bar"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						actual, err := l.LoadEnvironmentValues(nil, []any{"testdata/values.9.yaml.gotmpl"}, env, "")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := map[string]any{
 | 
				
			||||||
 | 
							"foo": "bar",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if diff := cmp.Diff(expected, actual); diff != "" {
 | 
				
			||||||
 | 
							t.Error(diff)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					foo: {{ .Values.foo }}
 | 
				
			||||||
| 
						 | 
					@ -101,6 +101,7 @@ ${kubectl} create namespace ${test_ns} || fail "Could not create namespace ${tes
 | 
				
			||||||
. ${dir}/test-cases/hcl-secrets.sh
 | 
					. ${dir}/test-cases/hcl-secrets.sh
 | 
				
			||||||
. ${dir}/test-cases/issue-1749.sh
 | 
					. ${dir}/test-cases/issue-1749.sh
 | 
				
			||||||
. ${dir}/test-cases/issue-1893.sh
 | 
					. ${dir}/test-cases/issue-1893.sh
 | 
				
			||||||
 | 
					. ${dir}/test-cases/state-values-set-cli-args-in-environments.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# ALL DONE -----------------------------------------------------------------------------------------------------------
 | 
					# ALL DONE -----------------------------------------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,14 @@
 | 
				
			||||||
 | 
					state_values_set_cli_args_in_environments_input_dir="${cases_dir}/state-values-set-cli-args-in-environments/input"
 | 
				
			||||||
 | 
					state_values_set_cli_args_in_environments_output_dir="${cases_dir}/state-values-set-cli-args-in-environments/output"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					state_values_set_cli_args_in_environments_tmp=$(mktemp -d)
 | 
				
			||||||
 | 
					state_values_set_cli_args_in_environments_reverse=${state_values_set_cli_args_in_environments_tmp}/state.values.set.cli.args.build.yaml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_start "state values set cli args in environments"
 | 
				
			||||||
 | 
					info "Comparing state values set cli args environments output ${state_values_set_cli_args_in_environments_reverse} with ${state_values_set_cli_args_in_environments_output_dir}/output.yaml"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					${helmfile} -f ${state_values_set_cli_args_in_environments_input_dir}/helmfile.yaml.gotmpl template  $(cat "$state_values_set_cli_args_in_environments_input_dir/helmfile-extra-args") --skip-deps > "${state_values_set_cli_args_in_environments_reverse}" || fail "\"helmfile template\" shouldn't fail"
 | 
				
			||||||
 | 
					./dyff between -bs "${state_values_set_cli_args_in_environments_output_dir}/output.yaml" "${state_values_set_cli_args_in_environments_reverse}" || fail "\"helmfile template\" should be consistent"
 | 
				
			||||||
 | 
					echo code=$?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_pass "state values set cli args in environments"
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					---
 | 
				
			||||||
 | 
					foo: {{ .Values.foo }}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					--state-values-set foo=bar
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,17 @@
 | 
				
			||||||
 | 
					environments:
 | 
				
			||||||
 | 
					  default:
 | 
				
			||||||
 | 
					    values:
 | 
				
			||||||
 | 
					      - environments.yaml.gotmpl
 | 
				
			||||||
 | 
					---
 | 
				
			||||||
 | 
					releases:
 | 
				
			||||||
 | 
					- name: state-value-set-args-environments
 | 
				
			||||||
 | 
					  chart: ../../../charts/raw
 | 
				
			||||||
 | 
					  values:
 | 
				
			||||||
 | 
					  - templates:
 | 
				
			||||||
 | 
					    - |
 | 
				
			||||||
 | 
					      apiVersion: v1
 | 
				
			||||||
 | 
					      kind: ConfigMap
 | 
				
			||||||
 | 
					      metadata:
 | 
				
			||||||
 | 
					        name: test
 | 
				
			||||||
 | 
					      data:
 | 
				
			||||||
 | 
					        foo: {{ .Values.foo }}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,8 @@
 | 
				
			||||||
 | 
					---
 | 
				
			||||||
 | 
					# Source: raw/templates/resources.yaml
 | 
				
			||||||
 | 
					apiVersion: v1
 | 
				
			||||||
 | 
					kind: ConfigMap
 | 
				
			||||||
 | 
					metadata:
 | 
				
			||||||
 | 
					  name: test
 | 
				
			||||||
 | 
					data:
 | 
				
			||||||
 | 
					  foo: bar
 | 
				
			||||||
		Loading…
	
		Reference in New Issue