Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optioned recursive environment variables expansion #793

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/cmd_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ This lists available CMD options in Helmsman:
`--no-env-subst`
turn off environment substitution globally.

`--no-recursive-env-expand`
disable recursive environment variables expansion.

`--subst-env-values`
turn on environment substitution in values files.

Expand Down
7 changes: 7 additions & 0 deletions internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type cli struct {
diffContext int
noEnvSubst bool
substEnvValues bool
noRecursiveEnvExpand bool
noSSMSubst bool
substSSMValues bool
detailedExitCode bool
Expand Down Expand Up @@ -138,6 +139,7 @@ func (c *cli) setup() {
flag.BoolVar(&c.detailedExitCode, "detailed-exit-code", false, "returns a detailed exit code (0 - no changes, 1 - error, 2 - changes present)")
flag.BoolVar(&c.noEnvSubst, "no-env-subst", false, "turn off environment substitution globally")
flag.BoolVar(&c.substEnvValues, "subst-env-values", false, "turn on environment substitution in values files.")
flag.BoolVar(&c.noRecursiveEnvExpand, "no-recursive-env-expand", false, "disable recursive environment values expansion")
flag.BoolVar(&c.noSSMSubst, "no-ssm-subst", false, "turn off SSM parameter substitution globally")
flag.BoolVar(&c.substSSMValues, "subst-ssm-values", false, "turn on SSM parameter substitution in values files.")
flag.BoolVar(&c.updateDeps, "update-deps", false, "run 'helm dep up' for local charts")
Expand Down Expand Up @@ -231,6 +233,11 @@ func (c *cli) parse() {
log.Verbose("Substitution of env variables in values enabled")
}
}

if !c.noRecursiveEnvExpand {
log.Verbose("Recursive environment variables expansion is enabled")
}

if !c.noSSMSubst {
log.Verbose("Substitution of SSM variables enabled")
if c.substSSMValues {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const (
helmBin = "helm"
kubectlBin = "kubectl"
appVersion = "v3.16.4"
appVersion = "v3.16.4-fix"
tempFilesDir = ".helmsman-tmp"
defaultContextName = "default"
resourcePool = 10
Expand Down
22 changes: 13 additions & 9 deletions internal/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func readFile(filepath string) string {
// recusively expanding the variable's value
func getEnv(key string) string {
value := os.Getenv(key)
for envVar.MatchString(value) {
value = os.ExpandEnv(value)
if !flags.noRecursiveEnvExpand {
for envVar.MatchString(value) {
value = os.ExpandEnv(value)
}
}
return value
}
Expand All @@ -160,13 +162,15 @@ func prepareEnv(envFiles []string) error {
return fmt.Errorf("error loading env file: %w", err)
}
}
for _, e := range os.Environ() {
if !strings.Contains(e, "$") {
continue
if !flags.noRecursiveEnvExpand {
for _, e := range os.Environ() {
if !strings.Contains(e, "$") {
continue
}
e = os.Expand(e, getEnv)
pair := strings.SplitN(e, "=", 2)
os.Setenv(pair[0], pair[1])
}
e = os.Expand(e, getEnv)
pair := strings.SplitN(e, "=", 2)
os.Setenv(pair[0], pair[1])
}
return nil
}
Expand All @@ -178,7 +182,7 @@ func substituteEnv(str string) string {
if strings.Contains(str, "$") {
// add $$ escaping for $ strings
os.Setenv("HELMSMAN_DOLLAR", "$")
return os.ExpandEnv(strings.ReplaceAll(str, "$$", "${HELMSMAN_DOLLAR}"))
return os.Expand(strings.ReplaceAll(str, "$$", "${HELMSMAN_DOLLAR}"), getEnv)
}
return str
}
Expand Down