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 <ykuoka@gmail.com>
This commit is contained in:
Yusuke Kuoka 2023-07-23 18:23:41 +09:00 committed by GitHub
parent b6581ee4bc
commit 858c233272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 7 deletions

View File

@ -241,15 +241,33 @@ func (ld *desiredStateLoader) load(env, overrodeEnv *environment.Environment, ba
env = &finalState.Env env = &finalState.Env
ld.logger.Debugf("merged environment: %v", 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 // If environments are not defined in the helmfile at all although the env is specified,
// all the helmfile parts are loaded and merged. // it's a missing env situation. Let's fail.
// Otherwise, any single helmfile part missing the env would fail the whole helmfile run. if len(finalState.Environments) == 0 && evaluateBases && env.Name != state.DefaultEnv {
// 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{ return nil, &state.StateLoadError{
Msg: fmt.Sprintf("failed to read %s", finalState.FilePath), Msg: fmt.Sprintf("failed to read %s", finalState.FilePath),
Cause: &state.UndefinedEnvError{Env: env.Name}, Cause: &state.UndefinedEnvError{Env: env.Name},

View File

@ -0,0 +1,5 @@
chartifyTempDir: environment_missing_in_subhelmfile
helmfileArgs:
- --environment
- test
- template

View File

@ -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 }}

View File

@ -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 }}

View File

@ -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 }}

View File

@ -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

View File

@ -0,0 +1 @@
envName: {{ .Environment.Name }}