Skip to content

Commit 01eba5c

Browse files
cmeurymumoshu
authored andcommitted
return non-zero exitcode when encountering any change on upgrade (#65)
An attempt at resolving #54, maybe someone can pick it up and build on it?
1 parent f75b83f commit 01eba5c

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Examples:
7676
7777
Flags:
7878
-h, --help help for upgrade
79+
--detailed-exitcode return a non-zero exit code when there are changes
7980
--reset-values reset the values to the ones built into the chart and merge in any new values
8081
--reuse-values reuse the last release's values and merge in any new values
8182
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)

cmd/upgrade.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ import (
99
"github.com/databus23/helm-diff/manifest"
1010
"github.com/spf13/cobra"
1111
"k8s.io/helm/pkg/helm"
12+
"errors"
1213
)
1314

1415
type diffCmd struct {
15-
release string
16-
chart string
17-
chartVersion string
18-
client helm.Interface
19-
valueFiles valueFiles
20-
values []string
21-
reuseValues bool
22-
resetValues bool
23-
allowUnreleased bool
24-
suppressedKinds []string
25-
outputContext int
16+
release string
17+
chart string
18+
chartVersion string
19+
client helm.Interface
20+
detailedExitCode bool
21+
valueFiles valueFiles
22+
values []string
23+
reuseValues bool
24+
resetValues bool
25+
allowUnreleased bool
26+
suppressedKinds []string
27+
outputContext int
2628
}
2729

2830
const globalUsage = `Show a diff explaining what a helm upgrade would change.
@@ -61,6 +63,7 @@ func newChartCommand() *cobra.Command {
6163

6264
f := cmd.Flags()
6365
f.StringVar(&diff.chartVersion, "version", "", "specify the exact chart version to use. If this is not specified, the latest version is used")
66+
f.BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
6467
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
6568
f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
6669
f.StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
@@ -139,7 +142,11 @@ func (d *diffCmd) run() error {
139142
newSpecs = manifest.Parse(upgradeResponse.Release.Manifest, upgradeResponse.Release.Namespace)
140143
}
141144

142-
diff.DiffManifests(currentSpecs, newSpecs, d.suppressedKinds, d.outputContext, os.Stdout)
145+
seenAnyChanges := diff.DiffManifests(currentSpecs, newSpecs, d.suppressedKinds, d.outputContext, os.Stdout)
146+
147+
if d.detailedExitCode && seenAnyChanges {
148+
return errors.New("identified at least one change, exiting with non-zero exit code (detailed-exitcode parameter enabled)")
149+
}
143150

144151
return nil
145152
}

diff/diff.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,51 @@ import (
1212
"github.com/databus23/helm-diff/manifest"
1313
)
1414

15-
func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, context int, to io.Writer) {
15+
func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, context int, to io.Writer) bool {
16+
seenAnyChanges := false
17+
emptyMapping := &manifest.MappingResult{}
1618
for key, oldContent := range oldIndex {
1719
if newContent, ok := newIndex[key]; ok {
1820
if oldContent.Content != newContent.Content {
1921
// modified
2022
fmt.Fprintf(to, ansi.Color("%s has changed:", "yellow")+"\n", key)
21-
printDiff(suppressedKinds, oldContent.Kind, context, oldContent.Content, newContent.Content, to)
23+
diffs := generateDiff(oldContent, newContent)
24+
if len(diffs) > 0 {
25+
seenAnyChanges = true
26+
}
27+
printDiff(suppressedKinds, oldContent.Kind, context, diffs, to)
2228
}
2329
} else {
2430
// removed
2531
fmt.Fprintf(to, ansi.Color("%s has been removed:", "yellow")+"\n", key)
26-
printDiff(suppressedKinds, oldContent.Kind, context, oldContent.Content, "", to)
32+
diffs := generateDiff(oldContent, emptyMapping)
33+
if len(diffs) > 0 {
34+
seenAnyChanges = true
35+
}
36+
printDiff(suppressedKinds, oldContent.Kind, context, diffs, to)
2737
}
2838
}
2939

3040
for key, newContent := range newIndex {
3141
if _, ok := oldIndex[key]; !ok {
3242
// added
3343
fmt.Fprintf(to, ansi.Color("%s has been added:", "yellow")+"\n", key)
34-
printDiff(suppressedKinds, newContent.Kind, context, "", newContent.Content, to)
44+
diffs := generateDiff(emptyMapping, newContent)
45+
if len(diffs) > 0 {
46+
seenAnyChanges = true
47+
}
48+
printDiff(suppressedKinds, newContent.Kind, context, diffs, to)
3549
}
3650
}
51+
return seenAnyChanges
52+
}
53+
54+
func generateDiff(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
55+
const sep = "\n"
56+
return difflib.Diff(strings.Split(oldContent.Content, sep), strings.Split(newContent.Content, sep))
3757
}
3858

39-
func printDiff(suppressedKinds []string, kind string, context int, before, after string, to io.Writer) {
59+
func printDiff(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {
4060
diffs := difflib.Diff(strings.Split(before, "\n"), strings.Split(after, "\n"))
4161

4262
for _, ckind := range suppressedKinds {

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
import (
44
"os"
5+
"fmt"
56

67
"github.com/databus23/helm-diff/cmd"
78
)
89

910
func main() {
1011
if err := cmd.New().Execute(); err != nil {
12+
fmt.Println(err)
1113
os.Exit(1)
1214
}
1315
}

0 commit comments

Comments
 (0)