-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathlocal.go
More file actions
246 lines (198 loc) · 7.62 KB
/
local.go
File metadata and controls
246 lines (198 loc) · 7.62 KB
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
package cmd
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/spf13/cobra"
"github.com/databus23/helm-diff/v3/diff"
"github.com/databus23/helm-diff/v3/manifest"
)
type local struct {
chart1 string
chart2 string
release string
namespace string
detailedExitCode bool
includeTests bool
includeCRDs bool
normalizeManifests bool
enableDNS bool
valueFiles valueFiles
values []string
stringValues []string
stringLiteralValues []string
jsonValues []string
fileValues []string
postRenderer string
postRendererArgs []string
extraAPIs []string
kubeVersion string
diff.Options
}
const localCmdLongUsage = `
This command compares the manifests of two local chart directories.
It renders both charts using 'helm template' and shows the differences
between the resulting manifests.
This is useful for:
- Comparing different versions of a chart
- Previewing changes before committing
- Validating chart modifications
`
func localCmd() *cobra.Command {
diff := local{
release: "release",
}
localCmd := &cobra.Command{
Use: "local [flags] CHART1 CHART2",
Short: "Shows diff between two local chart directories",
Long: localCmdLongUsage,
Example: strings.Join([]string{
" helm diff local ./chart-v1 ./chart-v2",
" helm diff local ./chart-v1 ./chart-v2 -f values.yaml",
" helm diff local /path/to/chart-a /path/to/chart-b --set replicas=3",
}, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
// Suppress the command usage on error. See #77 for more info
cmd.SilenceUsage = true
if v, _ := cmd.Flags().GetBool("version"); v {
fmt.Println(Version)
return nil
}
if err := checkArgsLength(len(args), "chart1 path", "chart2 path"); err != nil {
return err
}
ProcessDiffOptions(cmd.Flags(), &diff.Options)
diff.chart1 = args[0]
diff.chart2 = args[1]
if diff.namespace == "" {
diff.namespace = os.Getenv("HELM_NAMESPACE")
}
return diff.run()
},
}
localCmd.Flags().StringVar(&diff.release, "release", "release", "release name to use for template rendering")
localCmd.Flags().StringVar(&diff.namespace, "namespace", "", "namespace to use for template rendering")
localCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
localCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
localCmd.Flags().BoolVar(&diff.includeCRDs, "include-crds", false, "include CRDs in the diffing")
localCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
localCmd.Flags().BoolVar(&diff.enableDNS, "enable-dns", false, "enable DNS lookups when rendering templates")
localCmd.Flags().VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
localCmd.Flags().StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
localCmd.Flags().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)")
localCmd.Flags().StringArrayVar(&diff.stringLiteralValues, "set-literal", []string{}, "set STRING literal values on the command line")
localCmd.Flags().StringArrayVar(&diff.jsonValues, "set-json", []string{}, "set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)")
localCmd.Flags().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)")
localCmd.Flags().StringVar(&diff.postRenderer, "post-renderer", "", "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path")
localCmd.Flags().StringArrayVar(&diff.postRendererArgs, "post-renderer-args", []string{}, "an argument to the post-renderer (can specify multiple)")
localCmd.Flags().StringArrayVarP(&diff.extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions")
localCmd.Flags().StringVar(&diff.kubeVersion, "kube-version", "", "Kubernetes version used for Capabilities.KubeVersion")
AddDiffOptions(localCmd.Flags(), &diff.Options)
localCmd.SuggestionsMinimumDistance = 1
return localCmd
}
func (l *local) run() error {
if err := l.prepareStdinValues(); err != nil {
return err
}
manifest1, err := l.renderChart(l.chart1)
if err != nil {
return fmt.Errorf("failed to render chart %s: %w", l.chart1, err)
}
manifest2, err := l.renderChart(l.chart2)
if err != nil {
return fmt.Errorf("failed to render chart %s: %w", l.chart2, err)
}
excludes := []string{manifest.Helm3TestHook, manifest.Helm2TestSuccessHook}
if l.includeTests {
excludes = []string{}
}
specs1 := manifest.Parse(manifest1, l.namespace, l.normalizeManifests, excludes...)
specs2 := manifest.Parse(manifest2, l.namespace, l.normalizeManifests, excludes...)
seenAnyChanges := diff.Manifests(specs1, specs2, &l.Options, os.Stdout)
if l.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 (l *local) prepareStdinValues() error {
for i, valueFile := range l.valueFiles {
if strings.TrimSpace(valueFile) == "-" {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
tmpfile, err := os.CreateTemp("", "helm-diff-stdin-values")
if err != nil {
return err
}
defer func() { _ = os.Remove(tmpfile.Name()) }()
if _, err := tmpfile.Write(data); err != nil {
_ = tmpfile.Close()
return err
}
if err := tmpfile.Close(); err != nil {
return err
}
l.valueFiles[i] = tmpfile.Name()
break
}
}
return nil
}
func (l *local) renderChart(chartPath string) ([]byte, error) {
flags := []string{}
if l.includeCRDs {
flags = append(flags, "--include-crds")
}
if l.namespace != "" {
flags = append(flags, "--namespace", l.namespace)
}
if l.postRenderer != "" {
flags = append(flags, "--post-renderer", l.postRenderer)
}
for _, arg := range l.postRendererArgs {
flags = append(flags, "--post-renderer-args", arg)
}
for _, valueFile := range l.valueFiles {
flags = append(flags, "--values", valueFile)
}
for _, value := range l.values {
flags = append(flags, "--set", value)
}
for _, stringValue := range l.stringValues {
flags = append(flags, "--set-string", stringValue)
}
for _, stringLiteralValue := range l.stringLiteralValues {
flags = append(flags, "--set-literal", stringLiteralValue)
}
for _, jsonValue := range l.jsonValues {
flags = append(flags, "--set-json", jsonValue)
}
for _, fileValue := range l.fileValues {
flags = append(flags, "--set-file", fileValue)
}
if l.enableDNS {
flags = append(flags, "--enable-dns")
}
for _, a := range l.extraAPIs {
flags = append(flags, "--api-versions", a)
}
if l.kubeVersion != "" {
flags = append(flags, "--kube-version", l.kubeVersion)
}
args := []string{"template", l.release, chartPath}
args = append(args, flags...)
helmBin := os.Getenv("HELM_BIN")
if helmBin == "" {
helmBin = "helm"
}
cmd := exec.Command(helmBin, args...)
return outputWithRichError(cmd)
}