-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
618 lines (510 loc) · 15.1 KB
/
main.go
File metadata and controls
618 lines (510 loc) · 15.1 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pmezard/go-difflib/difflib"
)
const (
defaultBase = "origin/main"
)
type multiFlag []string
func (m *multiFlag) String() string {
return strings.Join(*m, ",")
}
func (m *multiFlag) Set(value string) error {
*m = append(*m, value)
return nil
}
type Config struct {
Base string
Current string
Charts []string
ChartDir string
ValuesFiles string
SetValues []string
FailOnDiff bool
NoColor bool
SkipDependencyBuild bool
hasDifferences bool
useColor bool
}
func main() {
config := parseFlags()
if err := checkGitRepo(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if err := run(config); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func checkGitRepo() error {
cmd := exec.Command("git", "rev-parse", "--git-dir")
if err := cmd.Run(); err != nil {
return fmt.Errorf("not a git repository (or any of the parent directories)")
}
return nil
}
func parseFlags() *Config {
config := &Config{}
var setValues multiFlag
flag.StringVar(&config.Base, "base", defaultBase, "Base git reference to compare from")
flag.StringVar(&config.Current, "current", "HEAD", "Current git reference to compare to")
flag.StringVar(&config.ChartDir, "chart-dir", ".", "Directory containing Helm charts")
flag.StringVar(&config.ValuesFiles, "values", "", "Comma-separated list of values files to use")
flag.Var(&setValues, "set", "Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
flag.BoolVar(&config.FailOnDiff, "fail-on-diff", false, "Exit with code 1 if differences are found")
flag.BoolVar(&config.NoColor, "no-color", false, "Disable colored output")
flag.BoolVar(&config.SkipDependencyBuild, "skip-dependency-build", false, "Skip building chart dependencies (use if dependencies are already up to date)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: helm git-diff [flags] [CHART...]\n\n")
fmt.Fprintf(os.Stderr, "Show Kubernetes resource differences between git commits for Helm charts.\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
flag.Parse()
config.Charts = flag.Args()
config.SetValues = setValues
if err := detectChartContext(config); err != nil {
fmt.Fprintf(os.Stderr, "Warning: %v\n", err)
}
config.useColor = shouldUseColor(config.NoColor)
return config
}
func shouldUseColor(noColor bool) bool {
if noColor {
return false
}
if os.Getenv("NO_COLOR") != "" {
return false
}
return isTerminal(os.Stdout)
}
func isTerminal(f *os.File) bool {
fileInfo, err := f.Stat()
if err != nil {
return false
}
return (fileInfo.Mode() & os.ModeCharDevice) != 0
}
func detectChartContext(config *Config) error {
if len(config.Charts) > 0 {
return nil
}
if _, err := os.Stat("Chart.yaml"); err == nil {
cwd, err := os.Getwd()
if err != nil {
return err
}
gitRoot, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return err
}
gitRootPath := strings.TrimSpace(string(gitRoot))
relPath, err := filepath.Rel(gitRootPath, cwd)
if err != nil {
return err
}
parentPath := filepath.Dir(relPath)
chartName := filepath.Base(relPath)
config.ChartDir = parentPath
config.Charts = []string{chartName}
}
return nil
}
func run(config *Config) error {
if len(config.Charts) == 0 {
changedCharts, err := detectChangedCharts(config)
if err != nil {
return fmt.Errorf("detecting changed charts: %w", err)
}
config.Charts = changedCharts
if len(config.Charts) == 0 {
fmt.Println("No chart changes detected")
return nil
}
fmt.Printf("Detected changed charts: %s\n\n", strings.Join(config.Charts, ", "))
}
for _, chart := range config.Charts {
if err := diffChart(config, chart); err != nil {
return fmt.Errorf("diffing chart %s: %w", chart, err)
}
}
if config.FailOnDiff && config.hasDifferences {
os.Exit(1)
}
return nil
}
func detectChangedCharts(config *Config) ([]string, error) {
cmd := exec.Command("git", "diff", "--name-only", config.Base, config.Current)
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("running git diff: %w", err)
}
changedFiles := strings.Split(strings.TrimSpace(string(output)), "\n")
chartSet := make(map[string]bool)
for _, file := range changedFiles {
if file == "" {
continue
}
if strings.HasPrefix(file, config.ChartDir+"/") {
parts := strings.Split(file, "/")
if len(parts) >= 2 {
chartName := parts[1]
chartSet[chartName] = true
}
}
}
charts := make([]string, 0, len(chartSet))
for chart := range chartSet {
charts = append(charts, chart)
}
return charts, nil
}
func diffChart(config *Config, chartName string) error {
chartPath := filepath.Join(config.ChartDir, chartName)
workdirPath, err := getWorkdirChartPath(chartPath)
if err != nil {
return fmt.Errorf("getting workdir chart path: %w", err)
}
chartYaml := filepath.Join(workdirPath, "Chart.yaml")
if _, err := os.Stat(chartYaml); os.IsNotExist(err) {
return fmt.Errorf("no Chart.yaml found in %s - not a valid Helm chart", chartPath)
}
isLibrary, err := isLibraryChart(chartYaml)
if err != nil {
return fmt.Errorf("checking chart type: %w", err)
}
if isLibrary {
fmt.Printf("%s: skipped (library chart)\n", chartName)
return nil
}
baseManifest, err := renderChartAtRef(chartPath, config.Base, config.ValuesFiles, config.SetValues, config.SkipDependencyBuild)
if err != nil {
return fmt.Errorf("rendering base manifest: %w", err)
}
var currentManifest string
if config.Current == "HEAD" {
currentManifest, err = renderChartFromWorkdir(workdirPath, config.ValuesFiles, config.SetValues, config.SkipDependencyBuild)
if err != nil {
return fmt.Errorf("rendering current manifest: %w", err)
}
} else {
currentManifest, err = renderChartAtRef(chartPath, config.Current, config.ValuesFiles, config.SetValues, config.SkipDependencyBuild)
if err != nil {
return fmt.Errorf("rendering current manifest: %w", err)
}
}
if baseManifest == currentManifest {
fmt.Printf("%s: no changes\n", chartName)
return nil
}
config.hasDifferences = true
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(baseManifest),
B: difflib.SplitLines(currentManifest),
FromFile: fmt.Sprintf("%s (%s)", chartName, config.Base),
ToFile: fmt.Sprintf("%s (%s)", chartName, config.Current),
Context: 3,
}
diffText, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
return fmt.Errorf("generating diff: %w", err)
}
if config.useColor {
fmt.Print(colorizeDiff(diffText))
} else {
fmt.Print(diffText)
}
return nil
}
func colorizeDiff(diff string) string {
const (
red = "\033[31m"
green = "\033[32m"
cyan = "\033[36m"
reset = "\033[0m"
)
lines := strings.Split(diff, "\n")
for i, line := range lines {
if len(line) == 0 {
continue
}
switch line[0] {
case '-':
if strings.HasPrefix(line, "---") {
lines[i] = cyan + line + reset
} else {
lines[i] = red + line + reset
}
case '+':
if strings.HasPrefix(line, "+++") {
lines[i] = cyan + line + reset
} else {
lines[i] = green + line + reset
}
case '@':
lines[i] = cyan + line + reset
}
}
return strings.Join(lines, "\n")
}
func getWorkdirChartPath(gitRelativePath string) (string, error) {
gitRoot, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return "", err
}
gitRootPath := strings.TrimSpace(string(gitRoot))
if filepath.IsAbs(gitRelativePath) {
return gitRelativePath, nil
}
cwd, err := os.Getwd()
if err != nil {
return "", err
}
if strings.HasPrefix(gitRelativePath, ".") {
cwdRelativeToGit, err := filepath.Rel(gitRootPath, cwd)
if err != nil {
return "", err
}
fullRelativePath := filepath.Join(cwdRelativeToGit, gitRelativePath)
return filepath.Join(gitRootPath, fullRelativePath), nil
}
return filepath.Join(gitRootPath, gitRelativePath), nil
}
func renderChartFromWorkdir(chartPath, valuesFiles string, setValues []string, skipDependencyBuild bool) (string, error) {
if err := buildDependencies(chartPath, skipDependencyBuild); err != nil {
return "", fmt.Errorf("building dependencies: %w", err)
}
releaseName, err := getChartName(chartPath)
if err != nil {
return "", fmt.Errorf("getting chart name: %w", err)
}
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("getting current directory: %w", err)
}
args := []string{"template", releaseName, chartPath}
if valuesFiles != "" {
for _, vf := range strings.Split(valuesFiles, ",") {
valuesPath := strings.TrimSpace(vf)
if !filepath.IsAbs(valuesPath) {
valuesPath = filepath.Join(cwd, valuesPath)
}
args = append(args, "-f", valuesPath)
}
}
for _, sv := range setValues {
args = append(args, "--set", sv)
}
helmCmd := exec.Command("helm", args...)
output, err := helmCmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return "", fmt.Errorf("helm template failed: %s", string(exitErr.Stderr))
}
return "", fmt.Errorf("running helm template: %w", err)
}
return string(output), nil
}
func renderChartAtRef(chartPath, ref, valuesFiles string, setValues []string, skipDependencyBuild bool) (string, error) {
tmpDir, err := os.MkdirTemp("", "helm-git-diff-*")
if err != nil {
return "", fmt.Errorf("creating temp dir: %w", err)
}
defer func() {
_ = os.RemoveAll(tmpDir)
}()
gitRoot, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
return "", fmt.Errorf("getting git root: %w", err)
}
gitRootPath := strings.TrimSpace(string(gitRoot))
pathsToExtract, err := getChartPathsToExtract(gitRootPath, ref, chartPath)
if err != nil {
return "", fmt.Errorf("determining paths to extract: %w", err)
}
args := []string{"archive", ref}
args = append(args, pathsToExtract...)
cmd := exec.Command("git", args...)
cmd.Dir = gitRootPath
archive, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return "", fmt.Errorf("archiving chart paths at %s (stderr: %s): %w", ref, string(exitErr.Stderr), err)
}
return "", fmt.Errorf("archiving chart paths at %s: %w", ref, err)
}
if len(archive) == 0 {
return "", nil
}
extractCmd := exec.Command("tar", "x", "-C", tmpDir)
extractCmd.Stdin = strings.NewReader(string(archive))
if err := extractCmd.Run(); err != nil {
return "", fmt.Errorf("extracting archive: %w", err)
}
extractedChartPath := filepath.Join(tmpDir, chartPath)
if err := buildDependencies(extractedChartPath, skipDependencyBuild); err != nil {
return "", fmt.Errorf("building dependencies: %w", err)
}
releaseName, err := getChartName(extractedChartPath)
if err != nil {
return "", fmt.Errorf("getting chart name: %w", err)
}
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("getting current directory: %w", err)
}
helmArgs := []string{"template", releaseName, extractedChartPath}
if valuesFiles != "" {
for _, vf := range strings.Split(valuesFiles, ",") {
valuesPath := strings.TrimSpace(vf)
if !filepath.IsAbs(valuesPath) {
valuesPath = filepath.Join(cwd, valuesPath)
}
helmArgs = append(helmArgs, "-f", valuesPath)
}
}
for _, sv := range setValues {
helmArgs = append(helmArgs, "--set", sv)
}
helmCmd := exec.Command("helm", helmArgs...)
output, err := helmCmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return "", fmt.Errorf("helm template failed: %s", string(exitErr.Stderr))
}
return "", fmt.Errorf("running helm template: %w", err)
}
return string(output), nil
}
func isLibraryChart(chartYamlPath string) (bool, error) {
content, err := os.ReadFile(chartYamlPath)
if err != nil {
return false, err
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "type:") {
typeValue := strings.TrimSpace(strings.TrimPrefix(line, "type:"))
return typeValue == "library", nil
}
}
return false, nil
}
func getChartName(chartPath string) (string, error) {
chartYamlPath := filepath.Join(chartPath, "Chart.yaml")
content, err := os.ReadFile(chartYamlPath)
if err != nil {
return "", fmt.Errorf("reading Chart.yaml: %w", err)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "name:") {
name := strings.TrimSpace(strings.TrimPrefix(line, "name:"))
name = strings.Trim(name, "\"'")
if name != "" {
return name, nil
}
}
}
return "", fmt.Errorf("chart name not found in Chart.yaml")
}
func getChartPathsToExtract(gitRoot, ref, chartPath string) ([]string, error) {
paths := []string{chartPath}
cmd := exec.Command("git", "show", fmt.Sprintf("%s:%s/Chart.yaml", ref, chartPath))
cmd.Dir = gitRoot
output, err := cmd.Output()
if err != nil {
return paths, nil
}
lines := strings.Split(string(output), "\n")
inDependencies := false
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed == "dependencies:" {
inDependencies = true
continue
}
if inDependencies {
if len(trimmed) > 0 && !strings.HasPrefix(trimmed, "-") && !strings.HasPrefix(trimmed, "name:") && !strings.HasPrefix(trimmed, "version:") && !strings.HasPrefix(trimmed, "repository:") {
break
}
if strings.HasPrefix(trimmed, "repository:") {
repo := strings.TrimSpace(strings.TrimPrefix(trimmed, "repository:"))
repo = strings.Trim(repo, "\"'")
if strings.HasPrefix(repo, "file://") {
depPath := strings.TrimPrefix(repo, "file://")
fullPath := filepath.Join(chartPath, depPath)
cleanedPath := filepath.Clean(fullPath)
paths = append(paths, cleanedPath)
}
}
}
}
return paths, nil
}
func buildDependencies(chartPath string, skipBuild bool) error {
chartYaml := filepath.Join(chartPath, "Chart.yaml")
if _, err := os.Stat(chartYaml); os.IsNotExist(err) {
return nil
}
if skipBuild {
return nil
}
if areDependenciesUpToDate(chartPath) {
return nil
}
cmd := exec.Command("helm", "dependency", "build", chartPath)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("helm dependency build failed: %s", string(output))
}
return nil
}
func areDependenciesUpToDate(chartPath string) bool {
chartYaml := filepath.Join(chartPath, "Chart.yaml")
chartLock := filepath.Join(chartPath, "Chart.lock")
chartsDir := filepath.Join(chartPath, "charts")
chartYamlInfo, err := os.Stat(chartYaml)
if err != nil {
return false
}
chartLockInfo, err := os.Stat(chartLock)
if err != nil {
return false
}
if _, err := os.Stat(chartsDir); err != nil {
return false
}
if chartYamlInfo.ModTime().After(chartLockInfo.ModTime()) {
return false
}
content, err := os.ReadFile(chartYaml)
if err != nil {
return false
}
hasDependencies := false
lines := strings.Split(string(content), "\n")
for _, line := range lines {
if strings.TrimSpace(line) == "dependencies:" {
hasDependencies = true
break
}
}
if !hasDependencies {
return true
}
entries, err := os.ReadDir(chartsDir)
if err != nil || len(entries) == 0 {
return false
}
return true
}