Add diffArgs to helmDefaults (#1019)

* Add diffArgs to helmDefaults

Signed-off-by: Yuuki Takahashi <20282867+yktakaha4@users.noreply.github.com>
This commit is contained in:
Yuuki Takahashi 2023-09-14 11:23:41 +09:00 committed by GitHub
parent c6721de6a5
commit 430a825b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 27 deletions

View File

@ -182,6 +182,8 @@ helmDefaults:
# additional and global args passed to helm (default "") # additional and global args passed to helm (default "")
args: args:
- "--set k=v" - "--set k=v"
diffArgs:
- "--suppress-secrets"
# verify the chart before upgrading (only works with packaged charts not directories) (default false) # verify the chart before upgrading (only works with packaged charts not directories) (default false)
verify: true verify: true
keyring: path/to/keyring.gpg keyring: path/to/keyring.gpg

View File

@ -1323,7 +1323,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) {
st := r.state st := r.state
helm := r.helm helm := r.helm
helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, nil)...)
selectedReleases, selectedAndNeededReleases, err := a.getSelectedReleases(r, c.IncludeTransitiveNeeds()) selectedReleases, selectedAndNeededReleases, err := a.getSelectedReleases(r, c.IncludeTransitiveNeeds())
if err != nil { if err != nil {
@ -1374,7 +1374,8 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) {
// join --args and --diff-args together to one string. // join --args and --diff-args together to one string.
args := strings.Join([]string{c.Args(), c.DiffArgs()}, " ") args := strings.Join([]string{c.Args(), c.DiffArgs()}, " ")
helm.SetExtraArgs(argparser.GetArgs(args, r.state)...) argsOpts := &argparser.GetArgsOptions{WithDiffArgs: true}
helm.SetExtraArgs(argparser.GetArgs(args, r.state, argsOpts)...)
infoMsg, releasesToBeUpdated, releasesToBeDeleted, errs := r.diff(false, detailedExitCode, c, diffOpts) infoMsg, releasesToBeUpdated, releasesToBeDeleted, errs := r.diff(false, detailedExitCode, c, diffOpts)
if len(errs) > 0 { if len(errs) > 0 {
@ -1570,7 +1571,7 @@ Do you really want to delete?
`, strings.Join(names, "\n")) `, strings.Join(names, "\n"))
interactive := c.Interactive() interactive := c.Interactive()
if !interactive || interactive && r.askForConfirmation(msg) { if !interactive || interactive && r.askForConfirmation(msg) {
r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, nil)...)
if len(releasesToDelete) > 0 { if len(releasesToDelete) > 0 {
_, deletionErrs := withDAG(st, helm, a.Logger, state.PlanOptions{SelectedReleases: toDelete, Reverse: true, SkipNeeds: true}, a.WrapWithoutSelector(func(subst *state.HelmState, helm helmexec.Interface) []error { _, deletionErrs := withDAG(st, helm, a.Logger, state.PlanOptions{SelectedReleases: toDelete, Reverse: true, SkipNeeds: true}, a.WrapWithoutSelector(func(subst *state.HelmState, helm helmexec.Interface) []error {
@ -1596,7 +1597,8 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error)
helm := r.helm helm := r.helm
args := strings.Join([]string{c.Args(), c.DiffArgs()}, " ") args := strings.Join([]string{c.Args(), c.DiffArgs()}, " ")
helm.SetExtraArgs(argparser.GetArgs(args, r.state)...) argsOpts := &argparser.GetArgsOptions{WithDiffArgs: true}
helm.SetExtraArgs(argparser.GetArgs(args, r.state, argsOpts)...)
var errs []error var errs []error
@ -1620,7 +1622,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error)
} }
infoMsg, updated, deleted, errs = filtered.diff(true, c.DetailedExitcode(), c, opts) infoMsg, updated, deleted, errs = filtered.diff(true, c.DetailedExitcode(), c, opts)
helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, argsOpts)...)
return errs return errs
}) })
@ -1633,7 +1635,7 @@ func (a *App) lint(r *Run, c LintConfigProvider) (bool, []error, []error) {
ok, errs := a.withNeeds(r, c, false, func(st *state.HelmState) []error { ok, errs := a.withNeeds(r, c, false, func(st *state.HelmState) []error {
helm := r.helm helm := r.helm
args := argparser.GetArgs(c.Args(), st) args := argparser.GetArgs(c.Args(), st, nil)
// Reset the extra args if already set, not to break `helm fetch` by adding the args intended for `lint` // Reset the extra args if already set, not to break `helm fetch` by adding the args intended for `lint`
helm.SetExtraArgs() helm.SetExtraArgs()
@ -1694,7 +1696,7 @@ func (a *App) status(r *Run, c StatusesConfigProvider) (bool, []error) {
// Traverse DAG of all the releases so that we don't suffer from false-positive missing dependencies // Traverse DAG of all the releases so that we don't suffer from false-positive missing dependencies
st.Releases = allReleases st.Releases = allReleases
args := argparser.GetArgs(c.Args(), st) args := argparser.GetArgs(c.Args(), st, nil)
// Reset the extra args if already set, not to break `helm fetch` by adding the args intended for `lint` // Reset the extra args if already set, not to break `helm fetch` by adding the args intended for `lint`
helm.SetExtraArgs() helm.SetExtraArgs()
@ -1827,7 +1829,7 @@ Do you really want to sync?
var errs []error var errs []error
r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, nil)...)
// Traverse DAG of all the releases so that we don't suffer from false-positive missing dependencies // Traverse DAG of all the releases so that we don't suffer from false-positive missing dependencies
st.Releases = selectedAndNeededReleases st.Releases = selectedAndNeededReleases
@ -1894,7 +1896,7 @@ func (a *App) template(r *Run, c TemplateConfigProvider) (bool, []error) {
return a.withNeeds(r, c, false, func(st *state.HelmState) []error { return a.withNeeds(r, c, false, func(st *state.HelmState) []error {
helm := r.helm helm := r.helm
args := argparser.GetArgs(c.Args(), st) args := argparser.GetArgs(c.Args(), st, nil)
// Reset the extra args if already set, not to break `helm fetch` by adding the args intended for `lint` // Reset the extra args if already set, not to break `helm fetch` by adding the args intended for `lint`
helm.SetExtraArgs() helm.SetExtraArgs()
@ -2017,7 +2019,7 @@ func (a *App) test(r *Run, c TestConfigProvider) []error {
// with conditions and selectors // with conditions and selectors
st.Releases = toTest st.Releases = toTest
r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, nil)...)
return st.TestReleases(r.helm, cleanup, timeout, concurrency, state.Logs(c.Logs())) return st.TestReleases(r.helm, cleanup, timeout, concurrency, state.Logs(c.Logs()))
} }

View File

@ -105,13 +105,13 @@ func (r *Run) withPreparedCharts(helmfileCommand string, opts state.ChartPrepare
} }
func (r *Run) Deps(c DepsConfigProvider) []error { func (r *Run) Deps(c DepsConfigProvider) []error {
r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, nil)...)
return r.state.UpdateDeps(r.helm, c.IncludeTransitiveNeeds()) return r.state.UpdateDeps(r.helm, c.IncludeTransitiveNeeds())
} }
func (r *Run) Repos(c ReposConfigProvider) error { func (r *Run) Repos(c ReposConfigProvider) error {
r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state)...) r.helm.SetExtraArgs(argparser.GetArgs(c.Args(), r.state, nil)...)
return r.ctx.SyncReposOnce(r.state, r.helm) return r.ctx.SyncReposOnce(r.state, r.helm)
} }

View File

@ -16,6 +16,9 @@ type argMap struct {
m map[string][]*keyVal m map[string][]*keyVal
flags []string flags []string
} }
type GetArgsOptions struct {
WithDiffArgs bool
}
// isNewFlag checks if the given arg is a new flag // isNewFlag checks if the given arg is a new flag
func isNewFlag(flag string) bool { func isNewFlag(flag string) bool {
@ -83,7 +86,7 @@ func analyzeArgs(am *argMap, args string) {
} }
} }
func GetArgs(args string, state *state.HelmState) []string { func GetArgs(args string, state *state.HelmState, opts *GetArgsOptions) []string {
argsMap := newArgMap() argsMap := newArgMap()
if len(args) > 0 { if len(args) > 0 {
@ -94,6 +97,10 @@ func GetArgs(args string, state *state.HelmState) []string {
analyzeArgs(argsMap, strings.Join(state.HelmDefaults.Args, " ")) analyzeArgs(argsMap, strings.Join(state.HelmDefaults.Args, " "))
} }
if len(state.HelmDefaults.DiffArgs) > 0 && opts != nil && opts.WithDiffArgs {
analyzeArgs(argsMap, strings.Join(state.HelmDefaults.DiffArgs, " "))
}
var argArr []string var argArr []string
for _, flag := range argsMap.flags { for _, flag := range argsMap.flags {

View File

@ -12,38 +12,45 @@ import (
// TestGetArgs tests the GetArgs function // TestGetArgs tests the GetArgs function
func TestGetArgs(t *testing.T) { func TestGetArgs(t *testing.T) {
tests := []struct { tests := []struct {
args string args string
expected string expected string
defaultArgs []string defaultArgs []string
defaultDiffArgs []string
opts *GetArgsOptions
}{ }{
{ {
args: "-f a.yaml -f b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false", args: "-f a.yaml -f b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false",
defaultArgs: []string{"--recreate-pods", "--force"}, defaultArgs: []string{"--recreate-pods", "--force"},
expected: "-f a.yaml -f b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false --recreate-pods --force", defaultDiffArgs: []string{"--suppress", "Deployment"},
opts: &GetArgsOptions{WithDiffArgs: true},
expected: "-f a.yaml -f b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false --recreate-pods --force --suppress Deployment",
}, },
{ {
args: "-e a.yaml -d b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false", args: "-e a.yaml -d b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false",
defaultArgs: []string{"-q www", "-w"}, defaultArgs: []string{"-q www", "-w"},
expected: "-e a.yaml -d b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false -q www -w", defaultDiffArgs: []string{},
expected: "-e a.yaml -d b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false -q www -w",
}, },
{ {
args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false", args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false",
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false", expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false",
}, },
{ {
args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true", args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true",
defaultArgs: []string{"--recreate-pods", "--force"}, defaultArgs: []string{"--recreate-pods", "--force"},
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --recreate-pods --force", defaultDiffArgs: []string{"--suppress", "Deployment"},
opts: &GetArgsOptions{},
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --recreate-pods --force",
}, },
} }
for _, test := range tests { for _, test := range tests {
Helmdefaults := state.HelmSpec{KubeContext: "test", Args: test.defaultArgs} Helmdefaults := state.HelmSpec{KubeContext: "test", Args: test.defaultArgs, DiffArgs: test.defaultDiffArgs}
testState := &state.HelmState{ testState := &state.HelmState{
ReleaseSetSpec: state.ReleaseSetSpec{ ReleaseSetSpec: state.ReleaseSetSpec{
HelmDefaults: Helmdefaults, HelmDefaults: Helmdefaults,
}, },
} }
receivedArgs := GetArgs(test.args, testState) receivedArgs := GetArgs(test.args, testState, test.opts)
require.Equalf(t, test.expected, strings.Join(receivedArgs, " "), "expected args %s, received args %s", test.expected, strings.Join(receivedArgs, " ")) require.Equalf(t, test.expected, strings.Join(receivedArgs, " "), "expected args %s, received args %s", test.expected, strings.Join(receivedArgs, " "))
} }

View File

@ -149,6 +149,7 @@ type SubhelmfileEnvironmentSpec struct {
type HelmSpec struct { type HelmSpec struct {
KubeContext string `yaml:"kubeContext,omitempty"` KubeContext string `yaml:"kubeContext,omitempty"`
Args []string `yaml:"args,omitempty"` Args []string `yaml:"args,omitempty"`
DiffArgs []string `yaml:"diffArgs,omitempty"`
Verify bool `yaml:"verify"` Verify bool `yaml:"verify"`
Keyring string `yaml:"keyring,omitempty"` Keyring string `yaml:"keyring,omitempty"`
// EnableDNS, when set to true, enable DNS lookups when rendering templates // EnableDNS, when set to true, enable DNS lookups when rendering templates