Skip to content

Commit 2b8f694

Browse files
authored
Merge branch 'master' into feature/revision-diff
2 parents 81d477f + 993b7db commit 2b8f694

File tree

13 files changed

+291
-271
lines changed

13 files changed

+291
-271
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
diff
21
vendor/
2+
bin/
3+
build/
34
release/
45
.envrc

Makefile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ HELM_HOME ?= $(shell helm home)
22
HAS_GLIDE := $(shell command -v glide;)
33
VERSION := $(shell sed -n -e 's/version:[ "]*\([^"]*\).*/\1/p' plugin.yaml)
44

5-
LDFLAGS := -X main.Version=$(VERSION)
6-
75
PKG:= github.com/databus23/helm-diff
6+
LDFLAGS := -X $(PKG)/cmd.Version=$(VERSION)
87

98
# Clear the "unreleased" string in BuildMetadata
109
LDFLAGS += -X $(PKG)/vendor/k8s.io/helm/pkg/version.BuildMetadata=
1110
LDFLAGS += -X $(PKG)/vendor/k8s.io/helm/pkg/version.Version=$(shell grep -A1 "package: k8s.io/helm" glide.yaml | sed -n -e 's/[ ]*version:.*\(v[.0-9]*\).*/\1/p')
1211

1312
.PHONY: install
14-
install: bootstrap build
15-
mkdir -p $(HELM_HOME)/plugins/helm-diff
16-
cp diff $(HELM_HOME)/plugins/helm-diff/
13+
install: build
14+
mkdir -p $(HELM_HOME)/plugins/helm-diff/bin
15+
cp bin/diff $(HELM_HOME)/plugins/helm-diff/bin
1716
cp plugin.yaml $(HELM_HOME)/plugins/helm-diff/
1817

1918
.PHONY: build
2019
build:
21-
go build -i -v -o diff -ldflags="$(LDFLAGS)"
20+
mkdir -p bin/
21+
go build -i -v -o bin/diff -ldflags="$(LDFLAGS)"
2222

2323
.PHONY: bootstrap
2424
bootstrap:
@@ -37,11 +37,14 @@ dist:
3737
tar -C build/ -zcvf $(CURDIR)/release/helm-diff-linux.tgz diff/
3838
GOOS=darwin GOARCH=amd64 go build -o build/diff/diff -ldflags="$(LDFLAGS)"
3939
tar -C build/ -zcvf $(CURDIR)/release/helm-diff-macos.tgz diff/
40+
rm build/diff/diff
41+
GOOS=windows GOARCH=amd64 go build -o build/diff/diff.exe -ldflags="$(LDFLAGS)"
42+
tar -C build/ -zcvf $(CURDIR)/release/helm-diff-windows.tgz diff/
4043

4144
.PHONY: release
4245
release: dist
43-
ifndef GITHUB_ACCESS_TOKEN
44-
$(error GITHUB_ACCESS_TOKEN is undefined)
46+
ifndef GITHUB_TOKEN
47+
$(error GITHUB_TOKEN is undefined)
4548
endif
4649
git push
47-
gh-release create databus23/helm-diff $(VERSION) master
50+
github-release databus23/helm-diff v$(VERSION) master "v$(VERSION)" "release/*"

helm.go renamed to cmd/helm.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
// This file contains functions that where blatantly copied from
44
// https://github.wdf.sap.corp/kubernetes/helm
@@ -14,6 +14,8 @@ import (
1414
"github.com/ghodss/yaml"
1515
"google.golang.org/grpc"
1616
"k8s.io/helm/pkg/downloader"
17+
"k8s.io/helm/pkg/getter"
18+
"k8s.io/helm/pkg/helm/environment"
1719
"k8s.io/helm/pkg/helm/helmpath"
1820
"k8s.io/helm/pkg/strvals"
1921
)
@@ -26,6 +28,22 @@ func (v *valueFiles) String() string {
2628
return fmt.Sprint(*v)
2729
}
2830

31+
// Ensures all valuesFiles exist
32+
func (v *valueFiles) Valid() error {
33+
errStr := ""
34+
for _, valuesFile := range *v {
35+
if _, err := os.Stat(valuesFile); os.IsNotExist(err) {
36+
errStr += err.Error()
37+
}
38+
}
39+
40+
if errStr == "" {
41+
return nil
42+
} else {
43+
return errors.New(errStr)
44+
}
45+
}
46+
2947
func (v *valueFiles) Type() string {
3048
return "valueFiles"
3149
}
@@ -68,12 +86,13 @@ func locateChartPath(name, version string, verify bool, keyring string) (string,
6886
HelmHome: helmpath.Home(homePath()),
6987
Out: os.Stdout,
7088
Keyring: keyring,
89+
Getters: getter.All(environment.EnvSettings{}),
7190
}
7291
if verify {
7392
dl.Verify = downloader.VerifyAlways
7493
}
7594

76-
filename, _, err := dl.DownloadTo(name, version, ".")
95+
filename, _, err := dl.DownloadTo(name, version, helmpath.Home(homePath()).Archive())
7796
if err == nil {
7897
lname, err := filepath.Abs(filename)
7998
if err != nil {
@@ -82,7 +101,7 @@ func locateChartPath(name, version string, verify bool, keyring string) (string,
82101
return lname, nil
83102
}
84103

85-
return filename, fmt.Errorf("file %q not found", name)
104+
return filename, err
86105
}
87106

88107
// Merges source and destination map, preferring values from the source map

cmd/root.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
func New() *cobra.Command {
6+
7+
cmd := &cobra.Command{
8+
Use: "diff",
9+
Short: "Show manifest differences",
10+
}
11+
12+
chartCommand := newChartCommand()
13+
cmd.AddCommand(newVersionCmd(), chartCommand)
14+
//Alias root command to chart subcommand
15+
cmd.Args = chartCommand.Args
16+
cmd.Flags().AddFlagSet(chartCommand.Flags())
17+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
18+
cmd.Println(`Command "helm diff" is deprecated, use "helm diff upgrade" instead`)
19+
return chartCommand.RunE(cmd, args)
20+
}
21+
cmd.SetHelpCommand(&cobra.Command{}) // Disable the help command
22+
23+
return cmd
24+
25+
}

cmd/upgrade.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

cmd/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Version identifier populated via the CI/CD process.
10+
var Version = "HEAD"
11+
12+
func newVersionCmd() *cobra.Command {
13+
return &cobra.Command{
14+
Use: "version",
15+
Short: "Show version of the helm diff plugin",
16+
Run: func(*cobra.Command, []string) {
17+
fmt.Println(Version)
18+
},
19+
}
20+
}

diff.go renamed to diff/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package diff
22

33
import (
44
"fmt"
@@ -11,7 +11,7 @@ import (
1111
"github.com/databus23/helm-diff/manifest"
1212
)
1313

14-
func diffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, to io.Writer) {
14+
func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, to io.Writer) {
1515
for key, oldContent := range oldIndex {
1616
if newContent, ok := newIndex[key]; ok {
1717
if oldContent.Content != newContent.Content {

0 commit comments

Comments
 (0)