From 858c23327213b4654b166e972b010e0451ec39db Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Sun, 23 Jul 2023 18:23:41 +0900 Subject: [PATCH] Cancel rendering helmfile parts once the missing env is detected in the previous part (#941) * Cancel rendering helmfile parts once the missing env is detected in the previous part Fixes #913 Signed-off-by: Yusuke Kuoka --- pkg/app/desired_state_file_loader.go | 32 +++++++++++++++---- .../config.yaml | 5 +++ .../helmfiles/prod.yaml | 15 +++++++++ .../helmfiles/test.yaml | 15 +++++++++ .../input.yaml | 12 +++++++ .../output.yaml | 7 ++++ .../test.yaml.gotmpl | 1 + 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/config.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/prod.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/test.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/input.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/output.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/test.yaml.gotmpl diff --git a/pkg/app/desired_state_file_loader.go b/pkg/app/desired_state_file_loader.go index c62a260c..e0bc94f9 100644 --- a/pkg/app/desired_state_file_loader.go +++ b/pkg/app/desired_state_file_loader.go @@ -241,15 +241,33 @@ func (ld *desiredStateLoader) load(env, overrodeEnv *environment.Environment, ba env = &finalState.Env ld.logger.Debugf("merged environment: %v", env) + + if len(finalState.Environments) == 0 { + continue + } + + // At this point, we are sure that the env has been + // read from the vanilla or rendered YAML document. + // We can now check if the env is defined in it and fail accordingly. + // See https://github.com/helmfile/helmfile/issues/913 + + // We defer the missing env detection and failure until + // all the helmfile parts are loaded and merged. + // Otherwise, any single helmfile part missing the env would fail the whole helmfile run. + // That's problematic, because each helmfile part is supposed to be incomplete, and + // they become complete only after merging all the parts. + // See https://github.com/helmfile/helmfile/issues/807 for the rationale of this. + if _, ok := finalState.Environments[env.Name]; evaluateBases && env.Name != state.DefaultEnv && !ok { + return nil, &state.StateLoadError{ + Msg: fmt.Sprintf("failed to read %s", finalState.FilePath), + Cause: &state.UndefinedEnvError{Env: env.Name}, + } + } } - // We defer the missing env detection and failure until - // all the helmfile parts are loaded and merged. - // Otherwise, any single helmfile part missing the env would fail the whole helmfile run. - // That's problematic, because each helmfile part is supposed to be incomplete, and - // they become complete only after merging all the parts. - // See https://github.com/helmfile/helmfile/issues/807 for the rationale of this. - if _, ok := finalState.Environments[env.Name]; evaluateBases && env.Name != state.DefaultEnv && !ok { + // If environments are not defined in the helmfile at all although the env is specified, + // it's a missing env situation. Let's fail. + if len(finalState.Environments) == 0 && evaluateBases && env.Name != state.DefaultEnv { return nil, &state.StateLoadError{ Msg: fmt.Sprintf("failed to read %s", finalState.FilePath), Cause: &state.UndefinedEnvError{Env: env.Name}, diff --git a/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/config.yaml b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/config.yaml new file mode 100644 index 00000000..233d4ed9 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/config.yaml @@ -0,0 +1,5 @@ +chartifyTempDir: environment_missing_in_subhelmfile +helmfileArgs: +- --environment +- test +- template diff --git a/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/prod.yaml b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/prod.yaml new file mode 100644 index 00000000..4d13d6a6 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/prod.yaml @@ -0,0 +1,15 @@ +environments: + prod: + values: + - foo: prod + +--- + +releases: +- name: raw + chart: ../../../charts/raw-0.0.1 + values: + - templates: + - | + subhelmfile: {{ .Values.foo }} + envName: {{ .Values.envName }} diff --git a/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/test.yaml b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/test.yaml new file mode 100644 index 00000000..11bc0301 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/helmfiles/test.yaml @@ -0,0 +1,15 @@ +environments: + test: + values: + - foo: test + +--- + +releases: +- name: raw + chart: ../../../charts/raw-0.0.1 + values: + - templates: + - | + subhelmfile: {{ .Values.foo }} + envName: {{ .Values.envName }} diff --git a/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/input.yaml b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/input.yaml new file mode 100644 index 00000000..0bcf6499 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/input.yaml @@ -0,0 +1,12 @@ +environments: + test: + values: + - test.yaml.gotmpl +--- +helmfiles: +- path: helmfiles/test.yaml + values: + - envName: {{ .Values.envName }} +- path: helmfiles/prod.yaml + values: + - envName: {{ .Values.envName }} diff --git a/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/output.yaml b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/output.yaml new file mode 100644 index 00000000..b3a62542 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/output.yaml @@ -0,0 +1,7 @@ +Building dependency release=raw, chart=../../../charts/raw-0.0.1 +Templating release=raw, chart=../../../charts/raw-0.0.1 +--- +# Source: raw/templates/resources.yaml +subhelmfile: test +envName: test + diff --git a/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/test.yaml.gotmpl b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/test.yaml.gotmpl new file mode 100644 index 00000000..03c9afe7 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/environment_missing_in_subhelmfile/test.yaml.gotmpl @@ -0,0 +1 @@ +envName: {{ .Environment.Name }} \ No newline at end of file