Add --interactive option to sync, delete and destroy / Remove --interactive from global options (#328)

* add interactive in sync & remove --interactive in global options

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix unittest

Signed-off-by: yxxhero <aiopsclub@163.com>

* same behave as apply when in interactive

Signed-off-by: yxxhero <aiopsclub@163.com>

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2022-08-28 09:36:07 +08:00 committed by GitHub
parent 896d16566f
commit 9284d1764e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 115 additions and 79 deletions

View File

@ -31,29 +31,30 @@ func NewApplyCmd(globalCfg *config.GlobalImpl) *cobra.Command {
} }
f := cmd.Flags() f := cmd.Flags()
f.StringArrayVar(&applyImpl.ApplyOptions.Set, "set", nil, "additional values to be merged into the command") f.StringArrayVar(&applyOptions.Set, "set", nil, "additional values to be merged into the command")
f.StringArrayVar(&applyImpl.ApplyOptions.Values, "values", nil, "additional value files to be merged into the command") f.StringArrayVar(&applyOptions.Values, "values", nil, "additional value files to be merged into the command")
f.IntVar(&applyImpl.ApplyOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited") f.IntVar(&applyOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&applyImpl.ApplyOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the list of available API versions") f.BoolVar(&applyOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the list of available API versions")
f.IntVar(&applyImpl.ApplyOptions.Context, "context", 0, "output NUM lines of context around changes") f.IntVar(&applyOptions.Context, "context", 0, "output NUM lines of context around changes")
f.StringVar(&applyImpl.ApplyOptions.Output, "output", "", "output format for diff plugin") f.StringVar(&applyOptions.Output, "output", "", "output format for diff plugin")
f.BoolVar(&applyImpl.ApplyOptions.DetailedExitcode, "detailed-exitcode", false, "return a non-zero exit code 2 instead of 0 when there were changes detected AND the changes are synced successfully") f.BoolVar(&applyOptions.DetailedExitcode, "detailed-exitcode", false, "return a non-zero exit code 2 instead of 0 when there were changes detected AND the changes are synced successfully")
f.StringVar(&applyImpl.ApplyOptions.Args, "args", "", "pass args to helm exec") f.StringVar(&applyOptions.Args, "args", "", "pass args to helm exec")
f.BoolVar(&applyImpl.ApplyOptions.RetainValuesFiles, "retain-values-files", false, "DEPRECATED: Use skip-cleanup instead") f.BoolVar(&applyOptions.RetainValuesFiles, "retain-values-files", false, "DEPRECATED: Use skip-cleanup instead")
f.BoolVar(&applyImpl.ApplyOptions.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security") f.BoolVar(&applyOptions.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security")
f.BoolVar(&applyImpl.ApplyOptions.SkipCRDs, "skip-crds", false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present") f.BoolVar(&applyOptions.SkipCRDs, "skip-crds", false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present")
f.BoolVar(&applyImpl.ApplyOptions.SkipNeeds, "skip-needs", false, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`) f.BoolVar(&applyOptions.SkipNeeds, "skip-needs", false, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&applyImpl.ApplyOptions.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when when --selector/-l flag is not provided`) f.BoolVar(&applyOptions.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when when --selector/-l flag is not provided`)
f.BoolVar(&applyImpl.ApplyOptions.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`) f.BoolVar(&applyOptions.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&applyImpl.ApplyOptions.SkipDiffOnInstall, "skip-diff-on-install", false, "Skips running helm-diff on releases being newly installed on this apply. Useful when the release manifests are too huge to be reviewed, or it's too time-consuming to diff at all0") f.BoolVar(&applyOptions.SkipDiffOnInstall, "skip-diff-on-install", false, "Skips running helm-diff on releases being newly installed on this apply. Useful when the release manifests are too huge to be reviewed, or it's too time-consuming to diff at all0")
f.BoolVar(&applyImpl.ApplyOptions.IncludeTests, "include-tests", false, "enable the diffing of the helm test hooks") f.BoolVar(&applyOptions.IncludeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.StringArrayVar(&applyImpl.ApplyOptions.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the diff output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret") f.StringArrayVar(&applyOptions.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the diff output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret")
f.BoolVar(&applyImpl.ApplyOptions.SuppressSecrets, "suppress-secrets", false, "suppress secrets in the diff output. highly recommended to specify on CI/CD use-cases") f.BoolVar(&applyOptions.SuppressSecrets, "suppress-secrets", false, "suppress secrets in the diff output. highly recommended to specify on CI/CD use-cases")
f.BoolVar(&applyImpl.ApplyOptions.ShowSecrets, "show-secrets", false, "do not redact secret values in the diff output. should be used for debug purpose only") f.BoolVar(&applyOptions.ShowSecrets, "show-secrets", false, "do not redact secret values in the diff output. should be used for debug purpose only")
f.BoolVar(&applyImpl.ApplyOptions.SuppressDiff, "suppress-diff", false, "suppress diff in the output. Usable in new installs") f.BoolVar(&applyOptions.SuppressDiff, "suppress-diff", false, "suppress diff in the output. Usable in new installs")
f.BoolVar(&applyImpl.ApplyOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`) f.BoolVar(&applyOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`)
f.Bool("wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`) f.BoolVarP(&applyOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters")
f.Bool("wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`) f.BoolVar(&applyOptions.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`)
f.BoolVar(&applyOptions.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`)
return cmd return cmd
} }

View File

@ -35,6 +35,7 @@ func NewDeleteCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f.IntVar(&deleteOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited") f.IntVar(&deleteOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&deleteOptions.Purge, "purge", false, "purge releases i.e. free release names and histories") f.BoolVar(&deleteOptions.Purge, "purge", false, "purge releases i.e. free release names and histories")
f.BoolVar(&deleteOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`) f.BoolVar(&deleteOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`)
f.BoolVarP(&deleteOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters")
return cmd return cmd
} }

View File

@ -34,6 +34,7 @@ func NewDestroyCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f.StringVar(&destroyOptions.Args, "args", "", "pass args to helm exec") f.StringVar(&destroyOptions.Args, "args", "", "pass args to helm exec")
f.IntVar(&destroyOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited") f.IntVar(&destroyOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&destroyOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`) f.BoolVar(&destroyOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`)
f.BoolVarP(&destroyOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters")
return cmd return cmd
} }

View File

@ -121,7 +121,6 @@ func setGlobalOptionsForRootCmd(fs *pflag.FlagSet, globalOptions *config.GlobalO
--selector tier=frontend,tier!=proxy --selector tier=backend. Will match all frontend, non-proxy releases AND all backend releases. --selector tier=frontend,tier!=proxy --selector tier=backend. Will match all frontend, non-proxy releases AND all backend releases.
The name of a release can be used as a label. --selector name=myrelease`) The name of a release can be used as a label. --selector name=myrelease`)
fs.BoolVar(&globalOptions.AllowNoMatchingRelease, "allow-no-matching-release", false, `Do not exit with an error code if the provided selector has no matching releases.`) fs.BoolVar(&globalOptions.AllowNoMatchingRelease, "allow-no-matching-release", false, `Do not exit with an error code if the provided selector has no matching releases.`)
fs.BoolVarP(&globalOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters")
// avoid 'pflag: help requested' error (#251) // avoid 'pflag: help requested' error (#251)
fs.BoolP("help", "h", false, "help for helmfile") fs.BoolP("help", "h", false, "help for helmfile")
} }

View File

@ -43,6 +43,7 @@ func NewSyncCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f.BoolVar(&syncOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`) f.BoolVar(&syncOptions.SkipDeps, "skip-deps", false, `skip running "helm repo update" and "helm dependency build"`)
f.BoolVar(&syncOptions.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`) f.BoolVar(&syncOptions.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`)
f.BoolVar(&syncOptions.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`) f.BoolVar(&syncOptions.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`)
f.BoolVarP(&syncOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters")
return cmd return cmd
} }

View File

@ -522,7 +522,6 @@ Flags:
-f, --file helmfile.yaml load config from file or directory. defaults to helmfile.yaml or `helmfile.d`(means `helmfile.d/*.yaml`) in this preference -f, --file helmfile.yaml load config from file or directory. defaults to helmfile.yaml or `helmfile.d`(means `helmfile.d/*.yaml`) in this preference
-b, --helm-binary string Path to the helm binary (default "helm") -b, --helm-binary string Path to the helm binary (default "helm")
-h, --help help for helmfile -h, --help help for helmfile
-i, --interactive Request confirmation before attempting to modify clusters
--kube-context string Set kubectl context. Uses current context by default --kube-context string Set kubectl context. Uses current context by default
--log-level string Set log level, default info (default "info") --log-level string Set log level, default info (default "info")
-n, --namespace string Set namespace. Uses the namespace set in the context by default, and is available in templates as {{ .Namespace }} -n, --namespace string Set namespace. Uses the namespace set in the context by default, and is available in templates as {{ .Namespace }}
@ -1355,7 +1354,7 @@ Please see #203 for more context.
## Running Helmfile interactively ## Running Helmfile interactively
`helmfile --interactive [apply|destroy]` requests confirmation from you before actually modifying your cluster. `helmfile --interactive [apply|destroy|delete|sync]` requests confirmation from you before actually modifying your cluster.
Use it when you're running `helmfile` manually on your local machine or a kind of secure administrative hosts. Use it when you're running `helmfile` manually on your local machine or a kind of secure administrative hosts.

View File

@ -1297,7 +1297,7 @@ Do you really want to apply?
a.Logger.Debug(*infoMsg) a.Logger.Debug(*infoMsg)
} }
syncErrs := []error{} applyErrs := []error{}
affectedReleases := state.AffectedReleases{} affectedReleases := state.AffectedReleases{}
@ -1325,7 +1325,7 @@ Do you really want to apply?
})) }))
if len(deletionErrs) > 0 { if len(deletionErrs) > 0 {
syncErrs = append(syncErrs, deletionErrs...) applyErrs = append(applyErrs, deletionErrs...)
} }
} }
@ -1354,13 +1354,13 @@ Do you really want to apply?
})) }))
if len(updateErrs) > 0 { if len(updateErrs) > 0 {
syncErrs = append(syncErrs, updateErrs...) applyErrs = append(applyErrs, updateErrs...)
} }
} }
} }
affectedReleases.DisplayAffectedReleases(c.Logger()) affectedReleases.DisplayAffectedReleases(c.Logger())
return true, true, syncErrs return true, true, applyErrs
} }
func (a *App) delete(r *Run, purge bool, c DestroyConfigProvider) (bool, []error) { func (a *App) delete(r *Run, purge bool, c DestroyConfigProvider) (bool, []error) {
@ -1663,7 +1663,16 @@ func (a *App) sync(r *Run, c SyncConfigProvider) (bool, []error) {
%s %s
`, strings.Join(names, "\n")) `, strings.Join(names, "\n"))
a.Logger.Info(infoMsg) confMsg := fmt.Sprintf(`%s
Do you really want to sync?
Helmfile will sync all your releases, as shown above.
`, infoMsg)
interactive := c.Interactive()
if !interactive {
a.Logger.Debug(infoMsg)
}
var errs []error var errs []error
@ -1674,6 +1683,7 @@ func (a *App) sync(r *Run, c SyncConfigProvider) (bool, []error) {
affectedReleases := state.AffectedReleases{} affectedReleases := state.AffectedReleases{}
if !interactive || interactive && r.askForConfirmation(confMsg) {
if len(releasesToDelete) > 0 { if len(releasesToDelete) > 0 {
_, deletionErrs := withDAG(st, helm, a.Logger, state.PlanOptions{Reverse: true, SelectedReleases: toDelete, SkipNeeds: true}, a.WrapWithoutSelector(func(subst *state.HelmState, helm helmexec.Interface) []error { _, deletionErrs := withDAG(st, helm, a.Logger, state.PlanOptions{Reverse: true, SelectedReleases: toDelete, SkipNeeds: true}, a.WrapWithoutSelector(func(subst *state.HelmState, helm helmexec.Interface) []error {
var rs []state.ReleaseSpec var rs []state.ReleaseSpec
@ -1721,6 +1731,7 @@ func (a *App) sync(r *Run, c SyncConfigProvider) (bool, []error) {
errs = append(errs, syncErrs...) errs = append(errs, syncErrs...)
} }
} }
}
affectedReleases.DisplayAffectedReleases(c.Logger()) affectedReleases.DisplayAffectedReleases(c.Logger())
return true, errs return true, errs
} }

View File

@ -91,6 +91,7 @@ type SyncConfigProvider interface {
DAGConfig DAGConfig
concurrencyConfig concurrencyConfig
interactive
loggingConfig loggingConfig
} }

View File

@ -50,6 +50,8 @@ type ApplyOptions struct {
Wait bool Wait bool
// WaitForJobs is true if the helm command should wait for the jobs to be completed // WaitForJobs is true if the helm command should wait for the jobs to be completed
WaitForJobs bool WaitForJobs bool
// Interactive is true if the user should be prompted for input.
Interactive bool
} }
// NewApply creates a new Apply // NewApply creates a new Apply
@ -195,3 +197,8 @@ func (a *ApplyImpl) Wait() bool {
func (a *ApplyImpl) WaitForJobs() bool { func (a *ApplyImpl) WaitForJobs() bool {
return a.ApplyOptions.WaitForJobs return a.ApplyOptions.WaitForJobs
} }
// Interactive returns the Interactive.
func (a *ApplyImpl) Interactive() bool {
return a.ApplyOptions.Interactive
}

View File

@ -10,6 +10,8 @@ type DeleteOptions struct {
Purge bool Purge bool
// SkipDeps is the skip deps flag // SkipDeps is the skip deps flag
SkipDeps bool SkipDeps bool
// Interactive is true if the user should be prompted for input.
Interactive bool
} }
// NewDeleteOptions creates a new Apply // NewDeleteOptions creates a new Apply
@ -50,3 +52,8 @@ func (c *DeleteImpl) Purge() bool {
func (c *DeleteImpl) SkipDeps() bool { func (c *DeleteImpl) SkipDeps() bool {
return c.DeleteOptions.SkipDeps return c.DeleteOptions.SkipDeps
} }
// Interactive returns the Interactive
func (c *DeleteImpl) Interactive() bool {
return c.DeleteOptions.Interactive
}

View File

@ -8,6 +8,8 @@ type DestroyOptions struct {
Concurrency int Concurrency int
// SkipDeps is the skip deps flag // SkipDeps is the skip deps flag
SkipDeps bool SkipDeps bool
// Interactive is true if the user should be prompted for input.
Interactive bool
} }
// NewDestroyOptions creates a new Apply // NewDestroyOptions creates a new Apply
@ -43,3 +45,8 @@ func (c *DestroyImpl) Args() string {
func (c *DestroyImpl) SkipDeps() bool { func (c *DestroyImpl) SkipDeps() bool {
return c.DestroyOptions.SkipDeps return c.DestroyOptions.SkipDeps
} }
// Interactive returns the Interactive
func (c *DestroyImpl) Interactive() bool {
return c.DestroyOptions.Interactive
}

View File

@ -42,8 +42,6 @@ type GlobalOptions struct {
Selector []string Selector []string
// AllowNoMatchingRelease is not exit with an error code if the provided selector has no matching releases. // AllowNoMatchingRelease is not exit with an error code if the provided selector has no matching releases.
AllowNoMatchingRelease bool AllowNoMatchingRelease bool
// Interactive is true if the user should be prompted for input.
Interactive bool
// logger is the logger to use. // logger is the logger to use.
logger *zap.SugaredLogger logger *zap.SugaredLogger
} }
@ -122,11 +120,6 @@ func (g *GlobalImpl) StateValuesFiles() []string {
return g.GlobalOptions.StateValuesFile return g.GlobalOptions.StateValuesFile
} }
// Interactive return interactive mode
func (g *GlobalImpl) Interactive() bool {
return g.GlobalOptions.Interactive
}
// Logger returns the logger // Logger returns the logger
func (g *GlobalImpl) Logger() *zap.SugaredLogger { func (g *GlobalImpl) Logger() *zap.SugaredLogger {
return g.GlobalOptions.logger return g.GlobalOptions.logger

View File

@ -26,6 +26,8 @@ type SyncOptions struct {
Wait bool Wait bool
// WaitForJobs is the wait for jobs flag // WaitForJobs is the wait for jobs flag
WaitForJobs bool WaitForJobs bool
// Interactive is true if the user should be prompted for input.
Interactive bool
} }
// NewSyncOptions creates a new Apply // NewSyncOptions creates a new Apply
@ -110,3 +112,8 @@ func (t *SyncImpl) Wait() bool {
func (t *SyncImpl) WaitForJobs() bool { func (t *SyncImpl) WaitForJobs() bool {
return t.SyncOptions.WaitForJobs return t.SyncOptions.WaitForJobs
} }
// Interactive returns the Interactive
func (t *SyncImpl) Interactive() bool {
return t.SyncOptions.Interactive
}

View File

@ -610,7 +610,7 @@ func (st *HelmState) isReleaseInstalled(context helmexec.HelmContext, helm helme
} }
func (st *HelmState) DetectReleasesToBeDeletedForSync(helm helmexec.Interface, releases []ReleaseSpec) ([]ReleaseSpec, error) { func (st *HelmState) DetectReleasesToBeDeletedForSync(helm helmexec.Interface, releases []ReleaseSpec) ([]ReleaseSpec, error) {
detected := []ReleaseSpec{} deleted := []ReleaseSpec{}
for i := range releases { for i := range releases {
release := releases[i] release := releases[i]
@ -618,14 +618,15 @@ func (st *HelmState) DetectReleasesToBeDeletedForSync(helm helmexec.Interface, r
installed, err := st.isReleaseInstalled(st.createHelmContext(&release, 0), helm, release) installed, err := st.isReleaseInstalled(st.createHelmContext(&release, 0), helm, release)
if err != nil { if err != nil {
return nil, err return nil, err
} else if installed { }
if installed {
// Otherwise `release` messed up(https://github.com/roboll/helmfile/issues/554) // Otherwise `release` messed up(https://github.com/roboll/helmfile/issues/554)
r := release r := release
detected = append(detected, r) deleted = append(deleted, r)
} }
} }
} }
return detected, nil return deleted, nil
} }
func (st *HelmState) DetectReleasesToBeDeleted(helm helmexec.Interface, releases []ReleaseSpec) ([]ReleaseSpec, error) { func (st *HelmState) DetectReleasesToBeDeleted(helm helmexec.Interface, releases []ReleaseSpec) ([]ReleaseSpec, error) {