-
Notifications
You must be signed in to change notification settings - Fork 268
/
upgrade.go
256 lines (220 loc) · 8.54 KB
/
upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package cmd
import (
"errors"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
"github.com/databus23/helm-diff/diff"
"github.com/databus23/helm-diff/manifest"
)
type diffCmd struct {
release string
chart string
chartVersion string
client helm.Interface
detailedExitCode bool
devel bool
namespace string // namespace to assume the release to be installed into. Defaults to the current kube config namespace.
valueFiles valueFiles
values []string
stringValues []string
fileValues []string
reuseValues bool
resetValues bool
allowUnreleased bool
noHooks bool
includeTests bool
suppressedKinds []string
outputContext int
showSecrets bool
}
const globalUsage = `Show a diff explaining what a helm upgrade would change.
This fetches the currently deployed version of a release
and compares it to a chart plus values.
This can be used visualize what changes a helm upgrade will
perform.
`
func newChartCommand() *cobra.Command {
diff := diffCmd{
namespace: os.Getenv("HELM_NAMESPACE"),
}
cmd := &cobra.Command{
Use: "upgrade [flags] [RELEASE] [CHART]",
Short: "Show a diff explaining what a helm upgrade would change.",
Long: globalUsage,
Example: " helm diff upgrade my-release stable/postgresql --values values.yaml",
Args: func(cmd *cobra.Command, args []string) error {
return checkArgsLength(len(args), "release name", "chart path")
},
PreRun: func(*cobra.Command, []string) {
expandTLSPaths()
},
RunE: func(cmd *cobra.Command, args []string) error {
// Suppress the command usage on error. See #77 for more info
cmd.SilenceUsage = true
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
}
diff.release = args[0]
diff.chart = args[1]
if isHelm3() {
return diff.runHelm3()
}
if diff.client == nil {
diff.client = createHelmClient()
}
return diff.run()
},
}
f := cmd.Flags()
f.StringVar(&diff.chartVersion, "version", "", "specify the exact chart version to use. If this is not specified, the latest version is used")
f.BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
f.BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
f.StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
f.StringArrayVar(&diff.stringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
f.StringArrayVar(&diff.fileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
f.BoolVar(&diff.reuseValues, "reuse-values", false, "reuse the last release's values and merge in any new values. If '--reset-values' is specified, this is ignored")
f.BoolVar(&diff.resetValues, "reset-values", false, "reset the values to the ones built into the chart and merge in any new values")
f.BoolVar(&diff.allowUnreleased, "allow-unreleased", false, "enables diffing of releases that are not yet deployed via Helm")
f.BoolVar(&diff.noHooks, "no-hooks", false, "disable diffing of hooks")
f.BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.BoolVar(&diff.devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")
f.StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
f.IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
if !isHelm3() {
f.StringVar(&diff.namespace, "namespace", "default", "namespace to assume the release to be installed into")
}
if !isHelm3() {
addCommonCmdOptions(f)
}
return cmd
}
func (d *diffCmd) runHelm3() error {
if err := compatibleHelm3Version(); err != nil {
return err
}
releaseManifest, err := getRelease(d.release, d.namespace)
var newInstall bool
if err != nil && strings.Contains(err.Error(), "release: not found") {
if d.allowUnreleased {
fmt.Printf("********************\n\n\tRelease was not present in Helm. Diff will show entire contents as new.\n\n********************\n")
newInstall = true
err = nil
} else {
fmt.Printf("********************\n\n\tRelease was not present in Helm. Include the `--allow-unreleased` to perform diff without exiting in error.\n\n********************\n")
return err
}
}
if err != nil {
return fmt.Errorf("Failed to get release %s in namespace %s: %s", d.release, d.namespace, err)
}
installManifest, err := d.template(!newInstall)
if err != nil {
return fmt.Errorf("Failed to render chart: %s", err)
}
currentSpecs := make(map[string]*manifest.MappingResult)
if !newInstall {
if !d.noHooks {
hooks, err := getHooks(d.release, d.namespace)
if err != nil {
return err
}
releaseManifest = append(releaseManifest, hooks...)
}
if d.includeTests {
currentSpecs = manifest.Parse(string(releaseManifest), d.namespace)
} else {
currentSpecs = manifest.Parse(string(releaseManifest), d.namespace, helm3TestHook, helm2TestSuccessHook)
}
}
var newSpecs map[string]*manifest.MappingResult
if d.includeTests {
newSpecs = manifest.Parse(string(installManifest), d.namespace)
} else {
newSpecs = manifest.Parse(string(installManifest), d.namespace, helm3TestHook, helm2TestSuccessHook)
}
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, os.Stdout)
if d.detailedExitCode && seenAnyChanges {
return Error{
error: errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)"),
Code: 2,
}
}
return nil
}
func (d *diffCmd) run() error {
if d.chartVersion == "" && d.devel {
d.chartVersion = ">0.0.0-0"
}
chartPath, err := locateChartPath(d.chart, d.chartVersion, false, "")
if err != nil {
return err
}
if err := d.valueFiles.Valid(); err != nil {
return err
}
rawVals, err := d.vals()
if err != nil {
return err
}
releaseResponse, err := d.client.ReleaseContent(d.release)
var newInstall bool
if err != nil && strings.Contains(err.Error(), fmt.Sprintf("release: %q not found", d.release)) {
if d.allowUnreleased {
fmt.Printf("********************\n\n\tRelease was not present in Helm. Diff will show entire contents as new.\n\n********************\n")
newInstall = true
err = nil
} else {
fmt.Printf("********************\n\n\tRelease was not present in Helm. Include the `--allow-unreleased` to perform diff without exiting in error.\n\n********************\n")
}
}
if err != nil {
return prettyError(err)
}
var currentSpecs, newSpecs map[string]*manifest.MappingResult
if newInstall {
installResponse, err := d.client.InstallRelease(
chartPath,
d.namespace,
helm.ReleaseName(d.release),
helm.ValueOverrides(rawVals),
helm.InstallDryRun(true),
)
if err != nil {
return prettyError(err)
}
currentSpecs = make(map[string]*manifest.MappingResult)
newSpecs = manifest.Parse(installResponse.Release.Manifest, installResponse.Release.Namespace)
} else {
upgradeResponse, err := d.client.UpdateRelease(
d.release,
chartPath,
helm.UpdateValueOverrides(rawVals),
helm.ReuseValues(d.reuseValues),
helm.ResetValues(d.resetValues),
helm.UpgradeDryRun(true),
)
if err != nil {
return prettyError(err)
}
if d.noHooks {
currentSpecs = manifest.Parse(releaseResponse.Release.Manifest, releaseResponse.Release.Namespace)
newSpecs = manifest.Parse(upgradeResponse.Release.Manifest, upgradeResponse.Release.Namespace)
} else {
currentSpecs = manifest.ParseRelease(releaseResponse.Release, d.includeTests)
newSpecs = manifest.ParseRelease(upgradeResponse.Release, d.includeTests)
}
}
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, os.Stdout)
if d.detailedExitCode && seenAnyChanges {
return Error{
error: errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)"),
Code: 2,
}
}
return nil
}