From ac30f1f18224145fc7406fcdbc3b1378252b07ca Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Sun, 9 Jan 2022 05:19:43 +0000 Subject: [PATCH] feat: HELM_DIFF_USE_UPGRADE_DRY_RUN Resolves #253 --- README.md | 6 +++++ cmd/helm3.go | 62 ++++++++++++++++++++++++++++++++++++++++---------- cmd/upgrade.go | 10 ++++++++ 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ab24fbaa..5cba84fb 100644 --- a/README.md +++ b/README.md @@ -120,8 +120,14 @@ Examples: # Set HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true to ignore unknown flags # It's useful when you're using `helm-diff` in a `helm upgrade` wrapper. + # See https://github.com/databus23/helm-diff/issues/278 for more information. HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true helm diff upgrade my-release stable/postgres --wait + # Set HELM_DIFF_USE_UPGRADE_DRY_RUN=true to + # use `helm upgrade --dry-run` instead of `helm template` to render manifests from the chart. + # See https://github.com/databus23/helm-diff/issues/253 for more information. + HELM_DIFF_USE_UPGRADE_DRY_RUN=true helm diff upgarde my-release datadog/datadog + Flags: --allow-unreleased enables diffing of releases that are not yet deployed via Helm -a, --api-versions stringArray Kubernetes api versions used for Capabilities.APIVersions diff --git a/cmd/helm3.go b/cmd/helm3.go index 2adb7386..0a5d7f6c 100644 --- a/cmd/helm3.go +++ b/cmd/helm3.go @@ -1,6 +1,7 @@ package cmd import ( + "bytes" "fmt" "io/ioutil" "os" @@ -129,26 +130,63 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) { flags = append(flags, "--set-file", fileValue) } - if !d.disableValidation && !d.dryRun { - flags = append(flags, "--validate") - } - - if isUpgrade { - flags = append(flags, "--is-upgrade") - } - if d.disableOpenAPIValidation { flags = append(flags, "--disable-openapi-validation") } - for _, a := range d.extraAPIs { - flags = append(flags, "--api-versions", a) + var ( + subcmd string + filter func([]byte) []byte + ) + + if d.useUpgradeDryRun { + if d.dryRun { + return nil, fmt.Errorf("`diff upgrade --dry-run` conflicts with HELM_DIFF_USE_UPGRADE_DRY_RUN_AS_TEMPLATE. Either remove --dry-run to enable cluster access, or unset HELM_DIFF_USE_UPGRADE_DRY_RUN_AS_TEMPLATE to make cluster access unnecessary") + } + + flags = append(flags, "--dry-run") + subcmd = "upgrade" + filter = func(s []byte) []byte { + if len(s) == 0 { + return s + } + + i := bytes.Index(s, []byte("MANIFEST:")) + s = s[i:] + i = bytes.Index(s, []byte("---")) + s = s[i:] + i = bytes.Index(s, []byte("\nNOTES:")) + if i != -1 { + s = s[:i+1] + } + return s + } + } else { + if !d.disableValidation && !d.dryRun { + flags = append(flags, "--validate") + } + + if isUpgrade { + flags = append(flags, "--is-upgrade") + } + + for _, a := range d.extraAPIs { + flags = append(flags, "--api-versions", a) + } + + subcmd = "template" + + filter = func(s []byte) []byte { + return s + } } - args := []string{"template", d.release, d.chart} + args := []string{subcmd, d.release, d.chart} args = append(args, flags...) + cmd := exec.Command(os.Getenv("HELM_BIN"), args...) - return outputWithRichError(cmd) + out, err := outputWithRichError(cmd) + return filter(out), err } func (d *diffCmd) writeExistingValues(f *os.File) error { diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 98e1303a..036f5b99 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -61,6 +61,7 @@ type diffCmd struct { normalizeManifests bool threeWayMerge bool extraAPIs []string + useUpgradeDryRun bool } func (d *diffCmd) isAllowUnreleased() bool { @@ -95,7 +96,13 @@ func newChartCommand() *cobra.Command { "", " # Set HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true to ignore unknown flags", " # It's useful when you're using `helm-diff` in a `helm upgrade` wrapper.", + " # See https://github.com/databus23/helm-diff/issues/278 for more information.", " HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true helm diff upgrade my-release stable/postgres --wait", + "", + " # Set HELM_DIFF_USE_UPGRADE_DRY_RUN=true to ", + " # use `helm upgrade --dry-run` instead of `helm template` to render manifests from the chart.", + " # See https://github.com/databus23/helm-diff/issues/253 for more information.", + " HELM_DIFF_USE_UPGRADE_DRY_RUN=true helm diff upgarde my-release datadog/datadog", }, "\n"), Args: func(cmd *cobra.Command, args []string) error { return checkArgsLength(len(args), "release name", "chart path") @@ -107,6 +114,9 @@ func newChartCommand() *cobra.Command { // Suppress the command usage on error. See #77 for more info cmd.SilenceUsage = true + // See https://github.com/databus23/helm-diff/issues/253 + diff.useUpgradeDryRun = os.Getenv("HELM_DIFF_USE_UPGRADE_DRY_RUN") == "true" + if q, _ := cmd.Flags().GetBool("suppress-secrets"); q { diff.suppressedKinds = append(diff.suppressedKinds, "Secret") }