Skip to content

Commit b44a8ec

Browse files
authored
feat: HELM_DIFF_USE_UPGRADE_DRY_RUN (#330)
Resolves #253
1 parent 19a5207 commit b44a8ec

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,14 @@ Examples:
120120
121121
# Set HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true to ignore unknown flags
122122
# It's useful when you're using `helm-diff` in a `helm upgrade` wrapper.
123+
# See https://github.com/databus23/helm-diff/issues/278 for more information.
123124
HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true helm diff upgrade my-release stable/postgres --wait
124125
126+
# Set HELM_DIFF_USE_UPGRADE_DRY_RUN=true to
127+
# use `helm upgrade --dry-run` instead of `helm template` to render manifests from the chart.
128+
# See https://github.com/databus23/helm-diff/issues/253 for more information.
129+
HELM_DIFF_USE_UPGRADE_DRY_RUN=true helm diff upgarde my-release datadog/datadog
130+
125131
Flags:
126132
--allow-unreleased enables diffing of releases that are not yet deployed via Helm
127133
-a, --api-versions stringArray Kubernetes api versions used for Capabilities.APIVersions

cmd/helm3.go

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -129,26 +130,63 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
129130
flags = append(flags, "--set-file", fileValue)
130131
}
131132

132-
if !d.disableValidation && !d.dryRun {
133-
flags = append(flags, "--validate")
134-
}
135-
136-
if isUpgrade {
137-
flags = append(flags, "--is-upgrade")
138-
}
139-
140133
if d.disableOpenAPIValidation {
141134
flags = append(flags, "--disable-openapi-validation")
142135
}
143136

144-
for _, a := range d.extraAPIs {
145-
flags = append(flags, "--api-versions", a)
137+
var (
138+
subcmd string
139+
filter func([]byte) []byte
140+
)
141+
142+
if d.useUpgradeDryRun {
143+
if d.dryRun {
144+
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")
145+
}
146+
147+
flags = append(flags, "--dry-run")
148+
subcmd = "upgrade"
149+
filter = func(s []byte) []byte {
150+
if len(s) == 0 {
151+
return s
152+
}
153+
154+
i := bytes.Index(s, []byte("MANIFEST:"))
155+
s = s[i:]
156+
i = bytes.Index(s, []byte("---"))
157+
s = s[i:]
158+
i = bytes.Index(s, []byte("\nNOTES:"))
159+
if i != -1 {
160+
s = s[:i+1]
161+
}
162+
return s
163+
}
164+
} else {
165+
if !d.disableValidation && !d.dryRun {
166+
flags = append(flags, "--validate")
167+
}
168+
169+
if isUpgrade {
170+
flags = append(flags, "--is-upgrade")
171+
}
172+
173+
for _, a := range d.extraAPIs {
174+
flags = append(flags, "--api-versions", a)
175+
}
176+
177+
subcmd = "template"
178+
179+
filter = func(s []byte) []byte {
180+
return s
181+
}
146182
}
147183

148-
args := []string{"template", d.release, d.chart}
184+
args := []string{subcmd, d.release, d.chart}
149185
args = append(args, flags...)
186+
150187
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
151-
return outputWithRichError(cmd)
188+
out, err := outputWithRichError(cmd)
189+
return filter(out), err
152190
}
153191

154192
func (d *diffCmd) writeExistingValues(f *os.File) error {

cmd/upgrade.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type diffCmd struct {
6161
normalizeManifests bool
6262
threeWayMerge bool
6363
extraAPIs []string
64+
useUpgradeDryRun bool
6465
}
6566

6667
func (d *diffCmd) isAllowUnreleased() bool {
@@ -95,7 +96,13 @@ func newChartCommand() *cobra.Command {
9596
"",
9697
" # Set HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true to ignore unknown flags",
9798
" # It's useful when you're using `helm-diff` in a `helm upgrade` wrapper.",
99+
" # See https://github.com/databus23/helm-diff/issues/278 for more information.",
98100
" HELM_DIFF_IGNORE_UNKNOWN_FLAGS=true helm diff upgrade my-release stable/postgres --wait",
101+
"",
102+
" # Set HELM_DIFF_USE_UPGRADE_DRY_RUN=true to ",
103+
" # use `helm upgrade --dry-run` instead of `helm template` to render manifests from the chart.",
104+
" # See https://github.com/databus23/helm-diff/issues/253 for more information.",
105+
" HELM_DIFF_USE_UPGRADE_DRY_RUN=true helm diff upgarde my-release datadog/datadog",
99106
}, "\n"),
100107
Args: func(cmd *cobra.Command, args []string) error {
101108
return checkArgsLength(len(args), "release name", "chart path")
@@ -107,6 +114,9 @@ func newChartCommand() *cobra.Command {
107114
// Suppress the command usage on error. See #77 for more info
108115
cmd.SilenceUsage = true
109116

117+
// See https://github.com/databus23/helm-diff/issues/253
118+
diff.useUpgradeDryRun = os.Getenv("HELM_DIFF_USE_UPGRADE_DRY_RUN") == "true"
119+
110120
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
111121
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
112122
}

0 commit comments

Comments
 (0)