Skip to content

Commit

Permalink
feat(helm): add ability for --dry-run to do lookup functions
Browse files Browse the repository at this point in the history
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 <tapaskapadia10@gmail.com>
  • Loading branch information
tapaskapadia committed Jan 16, 2023
1 parent 51281c1 commit 4d67dfa
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cmd/helm/install.go
Expand Up @@ -263,7 +263,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
client.Namespace = settings.Namespace()

// validate dry-run flag value is one of the allowed values
if err := validateDryRunFlag(client); err != nil {
if err := validateDryRunFlag(client.DryRun); err != nil {
return nil, err
}

Expand Down Expand Up @@ -308,12 +308,12 @@ func compInstall(args []string, toComplete string, client *action.Install) ([]st
return nil, cobra.ShellCompDirectiveNoFileComp
}

func validateDryRunFlag(client *action.Install) error {
func validateDryRunFlag(dryRunFlagValue string) error {
// validate dry-run flag value with set of allowed value
allowedDryRunValues := []string{"false", "true", "none", "client", "server"}
isAllowed := false
for _, v := range allowedDryRunValues {
if client.DryRun == v {
if dryRunFlagValue == v {
isAllowed = true
break
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/helm/upgrade.go
Expand Up @@ -120,11 +120,6 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
instClient.Description = client.Description
instClient.DependencyUpdate = client.DependencyUpdate

// validate dry-run flag value is one of the allowed values
if err := validateDryRunFlag(instClient); err != nil {
return err
}

rel, err := runInstall(args, instClient, valueOpts, out)
if err != nil {
return err
Expand All @@ -144,6 +139,10 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
// validate dry-run flag value is one of the allowed values
if err := validateDryRunFlag(client.DryRun); err != nil {
return err
}

p := getter.All(settings)
vals, err := valueOpts.MergeValues(p)
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/action.go
Expand Up @@ -125,7 +125,7 @@ func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Valu
// This enables the ability to render 'lookup' functions.
// It may break in interesting and exotic ways because other data (e.g. discovery)
// is mocked.
if (dryRun == "server" || dryRun == "none" || dryRun == "false") && cfg.RESTClientGetter != nil {
if (dryRun == "server" || dryRun == "none" || dryRun == "false") && cfg.RESTClientGetter != nil {
restConfig, err := cfg.RESTClientGetter.ToRESTConfig()
if err != nil {
return hs, b, "", err
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/install.go
Expand Up @@ -243,7 +243,7 @@ func (i *Install) RunWithContext(ctx context.Context, chrt *chart.Chart, vals ma
}

// special case for helm template --is-upgrade
isUpgrade := i.IsUpgrade && (i.DryRun != "none" && i.DryRun != "false")
isUpgrade := i.IsUpgrade && (i.DryRun != "none" && i.DryRun != "false")
options := chartutil.ReleaseOptions{
Name: i.ReleaseName,
Namespace: i.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/upgrade.go
Expand Up @@ -154,7 +154,7 @@ func (u *Upgrade) RunWithContext(ctx context.Context, name string, chart *chart.
return res, err
}
// Do not update for dry runs
if u.DryRun == "none" || u.DryRun == "false" {
if u.DryRun == "none" || u.DryRun == "false" {
u.cfg.Log("updating status for upgraded release for %s", name)
if err := u.cfg.Releases.Update(upgradedRelease); err != nil {
return res, err
Expand Down

0 comments on commit 4d67dfa

Please sign in to comment.