From 9a0025f96388bcb45f91b2206e0e9d24b498d339 Mon Sep 17 00:00:00 2001 From: Tapas Kapadia Date: Mon, 23 Jan 2023 14:28:29 -0600 Subject: [PATCH] feat(helm): add ability for --dry-run to do lookup functions When a helm command is run with the --dry-run flag, it will try to connect to the cluster if the value is 'server' to be able to render lookup functions. Closes #8137 Signed-off-by: Tapas Kapadia --- cmd/helm/install.go | 7 ++----- cmd/helm/upgrade.go | 7 ++----- pkg/action/install.go | 3 ++- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index 25e52810c..410505ef1 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -154,9 +154,8 @@ func newInstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { func addInstallFlags(cmd *cobra.Command, f *pflag.FlagSet, client *action.Install, valueOpts *values.Options) { f.BoolVar(&client.CreateNamespace, "create-namespace", false, "create the release namespace if not present") - f.BoolVar(&client.DryRun, "dry-run", false, "simulate an install") - f.StringVar(&client.DryRunOption, "dry-run-option", "none", "simulate an install. If --dry-run is set with no option being specified or as 'client', it will not attempt cluster connections. Setting option as 'server' allows attempting cluster connections.") - f.Lookup("dry-run-option").NoOptDefVal = "client" + f.StringVar(&client.DryRunOption, "dry-run", "none", "simulate an install. If --dry-run is set with no option being specified or as 'client', it will not attempt cluster connections. Setting option as 'server' allows attempting cluster connections.") + f.Lookup("dry-run").NoOptDefVal = "client" f.BoolVar(&client.Force, "force", false, "force resource updates through a replacement strategy") f.BoolVar(&client.DisableHooks, "no-hooks", false, "prevent hooks from running during install") f.BoolVar(&client.Replace, "replace", false, "re-use the given name, only if that name is a deleted release which remains in the history. This is unsafe in production") @@ -175,8 +174,6 @@ func addInstallFlags(cmd *cobra.Command, f *pflag.FlagSet, client *action.Instal addValueOptionsFlags(f, valueOpts) addChartPathOptionsFlags(f, &client.ChartPathOptions) - cmd.MarkFlagsMutuallyExclusive("dry-run", "dry-run-option") - err := cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { requiredArgs := 2 if client.GenerateName { diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index 34d8ab24b..355b181e3 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -218,9 +218,8 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.BoolVar(&createNamespace, "create-namespace", false, "if --install is set, create the release namespace if not present") f.BoolVarP(&client.Install, "install", "i", false, "if a release by this name doesn't already exist, run an install") f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored") - f.BoolVar(&client.DryRun, "dry-run", false, "simulate an upgrade") - f.StringVar(&client.DryRunOption, "dry-run-option", "none", "simulate an install. If --dry-run is set with no option being specified or as 'client', it will not attempt cluster connections. Setting option as 'server' allows attempting cluster connections.") - f.Lookup("dry-run-option").NoOptDefVal = "client" + f.StringVar(&client.DryRunOption, "dry-run", "none", "simulate an install. If --dry-run is set with no option being specified or as 'client', it will not attempt cluster connections. Setting option as 'server' allows attempting cluster connections.") + f.Lookup("dry-run").NoOptDefVal = "client" f.BoolVar(&client.Recreate, "recreate-pods", false, "performs pods restart for the resource if applicable") f.MarkDeprecated("recreate-pods", "functionality will no longer be updated. Consult the documentation for other methods to recreate pods") f.BoolVar(&client.Force, "force", false, "force resource updates through a replacement strategy") @@ -243,8 +242,6 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { bindOutputFlag(cmd, &outfmt) bindPostRenderFlag(cmd, &client.PostRenderer) - cmd.MarkFlagsMutuallyExclusive("dry-run", "dry-run-option") - err := cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) != 2 { return nil, cobra.ShellCompDirectiveNoFileComp diff --git a/pkg/action/install.go b/pkg/action/install.go index 3abe102a4..013cf7e94 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -210,7 +210,8 @@ func (i *Install) RunWithContext(ctx context.Context, chrt *chart.Chart, vals ma } var interactWithRemote bool - if !i.DryRun || i.DryRunOption == "server" { + // `helm template` is the only command that Install.APIVersions field will not be nil. + if (!i.DryRun || i.DryRunOption == "server") && i.APIVersions == nil { interactWithRemote = true }