|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "os" |
6 | 5 |
|
7 | | - "github.com/mgutz/ansi" |
8 | | - "github.com/spf13/cobra" |
9 | | - "k8s.io/helm/pkg/helm" |
10 | | - |
11 | | - "github.com/databus23/helm-diff/manifest" |
| 6 | + "github.com/databus23/helm-diff/cmd" |
12 | 7 | ) |
13 | 8 |
|
14 | | -const globalUsage = ` |
15 | | -Show a diff explaining what a helm upgrade would change. |
16 | | -
|
17 | | -This fetches the currently deployed version of a release |
18 | | -and compares it to a local chart plus values. |
19 | | -This can be used visualize what changes a helm upgrade will |
20 | | -perform. |
21 | | -` |
22 | | - |
23 | | -// Version identifier populated via the CI/CD process. |
24 | | -var Version = "HEAD" |
25 | | - |
26 | | -type diffCmd struct { |
27 | | - release string |
28 | | - chart string |
29 | | - client helm.Interface |
30 | | - valueFiles valueFiles |
31 | | - values []string |
32 | | - reuseValues bool |
33 | | - resetValues bool |
34 | | - suppressedKinds []string |
35 | | -} |
36 | | - |
37 | 9 | func main() { |
38 | | - diff := diffCmd{} |
39 | | - |
40 | | - cmd := &cobra.Command{ |
41 | | - Use: "diff [flags] [RELEASE] [CHART]", |
42 | | - Short: "Show manifest differences", |
43 | | - Long: globalUsage, |
44 | | - RunE: func(cmd *cobra.Command, args []string) error { |
45 | | - if v, _ := cmd.Flags().GetBool("version"); v { |
46 | | - fmt.Println(Version) |
47 | | - return nil |
48 | | - } |
49 | 10 |
|
50 | | - if err := checkArgsLength(len(args), "release name", "chart path"); err != nil { |
51 | | - return err |
52 | | - } |
53 | | - |
54 | | - if q, _ := cmd.Flags().GetBool("suppress-secrets"); q { |
55 | | - diff.suppressedKinds = append(diff.suppressedKinds, "Secret") |
56 | | - } |
57 | | - |
58 | | - if nc, _ := cmd.Flags().GetBool("no-color"); nc { |
59 | | - ansi.DisableColors(true) |
60 | | - } |
61 | | - |
62 | | - diff.release = args[0] |
63 | | - diff.chart = args[1] |
64 | | - if diff.client == nil { |
65 | | - diff.client = helm.NewClient(helm.Host(os.Getenv("TILLER_HOST")), helm.ConnectTimeout(int64(30))) |
66 | | - } |
67 | | - return diff.run() |
68 | | - }, |
69 | | - } |
70 | | - |
71 | | - f := cmd.Flags() |
72 | | - f.BoolP("version", "v", false, "show version") |
73 | | - f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output") |
74 | | - f.Bool("no-color", false, "remove colors from the output") |
75 | | - f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)") |
76 | | - f.StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)") |
77 | | - f.BoolVar(&diff.reuseValues, "reuse-values", false, "reuse the last release's values and merge in any new values") |
78 | | - f.BoolVar(&diff.resetValues, "reset-values", false, "reset the values to the ones built into the chart and merge in any new values") |
79 | | - f.StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output") |
80 | | - |
81 | | - if err := cmd.Execute(); err != nil { |
| 11 | + if err := cmd.New().Execute(); err != nil { |
82 | 12 | os.Exit(1) |
83 | 13 | } |
84 | 14 | } |
85 | | - |
86 | | -func (d *diffCmd) run() error { |
87 | | - chartPath, err := locateChartPath(d.chart, "", false, "") |
88 | | - if err != nil { |
89 | | - return err |
90 | | - } |
91 | | - |
92 | | - if err := d.valueFiles.Valid(); err != nil { |
93 | | - return err |
94 | | - } |
95 | | - |
96 | | - rawVals, err := d.vals() |
97 | | - if err != nil { |
98 | | - return err |
99 | | - } |
100 | | - |
101 | | - releaseResponse, err := d.client.ReleaseContent(d.release) |
102 | | - |
103 | | - if err != nil { |
104 | | - return prettyError(err) |
105 | | - } |
106 | | - |
107 | | - upgradeResponse, err := d.client.UpdateRelease( |
108 | | - d.release, |
109 | | - chartPath, |
110 | | - helm.UpdateValueOverrides(rawVals), |
111 | | - helm.ReuseValues(d.reuseValues), |
112 | | - helm.ResetValues(d.resetValues), |
113 | | - helm.UpgradeDryRun(true), |
114 | | - ) |
115 | | - if err != nil { |
116 | | - return prettyError(err) |
117 | | - } |
118 | | - |
119 | | - currentSpecs := manifest.Parse(releaseResponse.Release.Manifest) |
120 | | - newSpecs := manifest.Parse(upgradeResponse.Release.Manifest) |
121 | | - |
122 | | - diffManifests(currentSpecs, newSpecs, d.suppressedKinds, os.Stdout) |
123 | | - |
124 | | - return nil |
125 | | -} |
0 commit comments