forked from databus23/helm-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm3.go
390 lines (346 loc) · 11.3 KB
/
helm3.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package cmd
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/Masterminds/semver/v3"
)
var (
helmVersionRE = regexp.MustCompile(`Version:\s*"([^"]+)"`)
minHelmVersion = semver.MustParse("v3.1.0-rc.1")
// See https://github.com/helm/helm/pull/9426.
minHelmVersionWithDryRunLookupSupport = semver.MustParse("v3.13.0")
// The --reset-then-reuse-values flag for `helm upgrade` was added in
// https://github.com/helm/helm/pull/9653 and released as part of Helm v3.14.0.
minHelmVersionWithResetThenReuseValues = semver.MustParse("v3.14.0")
)
func getHelmVersion() (*semver.Version, error) {
cmd := exec.Command(os.Getenv("HELM_BIN"), "version")
debugPrint("Executing %s", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("Failed to run `%s version`: %w", os.Getenv("HELM_BIN"), err)
}
versionOutput := string(output)
matches := helmVersionRE.FindStringSubmatch(versionOutput)
if matches == nil {
return nil, fmt.Errorf("Failed to find version in output %#v", versionOutput)
}
helmVersion, err := semver.NewVersion(matches[1])
if err != nil {
return nil, fmt.Errorf("Failed to parse version %#v: %w", matches[1], err)
}
return helmVersion, nil
}
func isHelmVersionAtLeast(versionToCompareTo *semver.Version) (bool, error) {
helmVersion, err := getHelmVersion()
if err != nil {
return false, err
}
if helmVersion.LessThan(versionToCompareTo) {
return false, nil
}
return true, nil
}
func compatibleHelm3Version() error {
if isCompatible, err := isHelmVersionAtLeast(minHelmVersion); err != nil {
return err
} else if !isCompatible {
return fmt.Errorf("helm diff upgrade requires at least helm version %s", minHelmVersion.String())
}
return nil
}
func getRelease(release, namespace string) ([]byte, error) {
args := []string{"get", "manifest", release}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
return outputWithRichError(cmd)
}
func getHooks(release, namespace string) ([]byte, error) {
args := []string{"get", "hooks", release}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
return outputWithRichError(cmd)
}
func getRevision(release string, revision int, namespace string) ([]byte, error) {
args := []string{"get", "manifest", release, "--revision", strconv.Itoa(revision)}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
return outputWithRichError(cmd)
}
func getChart(release, namespace string) (string, error) {
args := []string{"get", "all", release, "--template", "{{.Release.Chart.Name}}"}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
out, err := outputWithRichError(cmd)
if err != nil {
return "", err
}
return string(out), nil
}
func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
flags := []string{}
if d.devel {
flags = append(flags, "--devel")
}
if d.noHooks && !d.useUpgradeDryRun {
flags = append(flags, "--no-hooks")
}
if d.chartVersion != "" {
flags = append(flags, "--version", d.chartVersion)
}
if d.chartRepo != "" {
flags = append(flags, "--repo", d.chartRepo)
}
if d.namespace != "" {
flags = append(flags, "--namespace", d.namespace)
}
if d.postRenderer != "" {
flags = append(flags, "--post-renderer", d.postRenderer)
}
for _, arg := range d.postRendererArgs {
flags = append(flags, "--post-renderer-args", arg)
}
if d.insecureSkipTLSVerify {
flags = append(flags, "--insecure-skip-tls-verify")
}
// Helm automatically enable --reuse-values when there's no --set, --set-string, --set-json, --set-values, --set-file present.
// Let's simulate that in helm-diff.
// See https://medium.com/@kcatstack/understand-helm-upgrade-flags-reset-values-reuse-values-6e58ac8f127e
shouldDefaultReusingValues := isUpgrade && len(d.values) == 0 && len(d.stringValues) == 0 && len(d.stringLiteralValues) == 0 && len(d.jsonValues) == 0 && len(d.valueFiles) == 0 && len(d.fileValues) == 0
if (d.reuseValues || d.resetThenReuseValues || shouldDefaultReusingValues) && !d.resetValues && d.clusterAccessAllowed() {
tmpfile, err := os.CreateTemp("", "existing-values")
if err != nil {
return nil, err
}
defer func() {
_ = os.Remove(tmpfile.Name())
}()
// In the presence of --reuse-values (or --reset-values), --reset-then-reuse-values is ignored.
if d.resetThenReuseValues && !d.reuseValues {
var supported bool
supported, err = isHelmVersionAtLeast(minHelmVersionWithResetThenReuseValues)
if err != nil {
return nil, err
}
if !supported {
return nil, fmt.Errorf("Using --reset-then-reuse-values requires at least helm version %s", minHelmVersionWithResetThenReuseValues.String())
}
err = d.writeExistingValues(tmpfile, false)
} else {
err = d.writeExistingValues(tmpfile, true)
}
if err != nil {
return nil, err
}
flags = append(flags, "--values", tmpfile.Name())
}
for _, value := range d.values {
flags = append(flags, "--set", value)
}
for _, stringValue := range d.stringValues {
flags = append(flags, "--set-string", stringValue)
}
for _, stringLiteralValue := range d.stringLiteralValues {
flags = append(flags, "--set-literal", stringLiteralValue)
}
for _, jsonValue := range d.jsonValues {
flags = append(flags, "--set-json", jsonValue)
}
for _, valueFile := range d.valueFiles {
if strings.TrimSpace(valueFile) == "-" {
bytes, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
tmpfile, err := os.CreateTemp("", "helm-diff-stdin-values")
if err != nil {
return nil, err
}
defer func() {
_ = os.Remove(tmpfile.Name())
}()
if _, err := tmpfile.Write(bytes); err != nil {
_ = tmpfile.Close()
return nil, err
}
if err := tmpfile.Close(); err != nil {
return nil, err
}
flags = append(flags, "--values", tmpfile.Name())
} else {
flags = append(flags, "--values", valueFile)
}
}
for _, fileValue := range d.fileValues {
flags = append(flags, "--set-file", fileValue)
}
if d.disableOpenAPIValidation {
flags = append(flags, "--disable-openapi-validation")
}
if d.enableDNS {
flags = append(flags, "--enable-dns")
}
if d.SkipSchemaValidation {
flags = append(flags, "--skip-schema-validation")
}
if d.takeOwnership {
flags = append(flags, "--take-ownership")
}
var (
subcmd string
filter func([]byte) []byte
)
// `--dry-run=client` or `--dry-run=server`?
//
// Or what's the relationoship between helm-diff's --dry-run flag,
// HELM_DIFF_UPGRADE_DRY_RUN env var and the helm upgrade --dry-run flag?
//
// Read on to find out.
if d.useUpgradeDryRun {
if d.isAllowUnreleased() {
// Otherwise you get the following error when this is a diff for a new install
// Error: UPGRADE FAILED: "$RELEASE_NAME" has no deployed releases
flags = append(flags, "--install")
}
// If the program reaches here,
// we are sure that the user wants to use the `helm upgrade --dry-run` command
// for generating the manifests to be diffed.
//
// So the question is only whether to use `--dry-run=client` or `--dry-run=server`.
//
// As HELM_DIFF_UPGRADE_DRY_RUN is there for producing more complete and correct diff results,
// we use --dry-run=server if the version of helm supports it.
// Otherwise, we use --dry-run=client, as that's the best we can do.
if useDryRunService, err := isHelmVersionAtLeast(minHelmVersionWithDryRunLookupSupport); err == nil && useDryRunService {
flags = append(flags, "--dry-run=server")
} else {
flags = append(flags, "--dry-run")
}
subcmd = "upgrade"
filter = func(s []byte) []byte {
return extractManifestFromHelmUpgradeDryRunOutput(s, d.noHooks)
}
} else {
if !d.disableValidation && d.clusterAccessAllowed() {
flags = append(flags, "--validate")
}
if isUpgrade {
flags = append(flags, "--is-upgrade")
}
for _, a := range d.extraAPIs {
flags = append(flags, "--api-versions", a)
}
if d.kubeVersion != "" {
flags = append(flags, "--kube-version", d.kubeVersion)
}
// To keep the full compatibility with older helm-diff versions,
// we pass --dry-run to `helm template` only if Helm is greater than v3.13.0.
if useDryRunService, err := isHelmVersionAtLeast(minHelmVersionWithDryRunLookupSupport); err == nil && useDryRunService {
// However, which dry-run mode to use is still not clear.
//
// For compatibility with the old and new helm-diff options,
// old and new helm, we assume that the user wants to use the older `helm template --dry-run=client` mode
// if helm-diff has been invoked with any of the following flags:
//
// * no dry-run flags (to be consistent with helm-template)
// * --dry-run
// * --dry-run=""
// * --dry-run=client
//
// and the newer `helm template --dry-run=server` mode when invoked with:
//
// * --dry-run=server
//
// Any other values should result in errors.
//
// See the fllowing link for more details:
// - https://github.com/databus23/helm-diff/pull/458
// - https://github.com/helm/helm/pull/9426#issuecomment-1501005666
if d.dryRunMode == "server" {
// This is for security reasons!
//
// We give helm-template the additional cluster access for the helm `lookup` function
// only if the user has explicitly requested it by --dry-run=server,
//
// In other words, although helm-diff-upgrade implies limited cluster access by default,
// helm-diff-upgrade without a --dry-run flag does NOT imply
// full cluster-access via helm-template --dry-run=server!
flags = append(flags, "--dry-run=server")
} else {
// Since helm-diff 3.9.0 and helm 3.13.0, we pass --dry-run=client to `helm template` by default.
// This doesn't make any difference for helm-diff itself,
// because helm-template w/o flags is equivalent to helm-template --dry-run=client.
// See https://github.com/helm/helm/pull/9426#discussion_r1181397259
flags = append(flags, "--dry-run=client")
}
}
subcmd = "template"
filter = func(s []byte) []byte {
return s
}
}
args := []string{subcmd, d.release, d.chart}
args = append(args, flags...)
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
out, err := outputWithRichError(cmd)
return filter(out), err
}
func (d *diffCmd) writeExistingValues(f *os.File, all bool) error {
args := []string{"get", "values", d.release, "--output", "yaml"}
if all {
args = append(args, "--all")
}
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
debugPrint("Executing %s", strings.Join(cmd.Args, " "))
defer func() {
_ = f.Close()
}()
cmd.Stdout = f
return cmd.Run()
}
func extractManifestFromHelmUpgradeDryRunOutput(s []byte, noHooks bool) []byte {
if len(s) == 0 {
return s
}
i := bytes.Index(s, []byte("HOOKS:"))
hooks := s[i:]
j := bytes.Index(hooks, []byte("MANIFEST:"))
manifest := hooks[j:]
hooks = hooks[:j]
k := bytes.Index(manifest, []byte("\nNOTES:"))
if k > -1 {
manifest = manifest[:k+1]
}
if noHooks {
hooks = nil
} else {
a := bytes.Index(hooks, []byte("---"))
if a > -1 {
hooks = hooks[a:]
} else {
hooks = nil
}
}
a := bytes.Index(manifest, []byte("---"))
if a > -1 {
manifest = manifest[a:]
}
r := []byte{}
r = append(r, manifest...)
r = append(r, hooks...)
return r
}