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