Skip to content

Commit 1846c74

Browse files
novas0x2amumoshu
authored andcommitted
feat: Improved diff on apiVersion changes (databus23#73)
Resolves databus23#70 When the apiVersion of a Kind changes, helm-diff detects this as a remove/readd of the whole manifest; this makes it difficult to see the actual changes. This change enables a small heuristic so apiVersion changes like `authentication.k8s.io/v1beta1` -> `authentication.k8s.io/v1` show up as a one-line diff instead of a full-manifest diff, while hopefully not meddling with unexpected apiVersions too badly.
1 parent fa12c4c commit 1846c74

File tree

9 files changed

+203
-5
lines changed

9 files changed

+203
-5
lines changed

diff/diff_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package diff
22

33
import (
4-
"testing"
54
"bytes"
5+
"testing"
6+
67
"github.com/mgutz/ansi"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/databus23/helm-diff/manifest"
711
)
812

913
var text1 = "" +
@@ -121,3 +125,44 @@ func assertDiff(t *testing.T, before, after string, context int, expected string
121125
t.Errorf("Unexpected diff output: \nExpected:\n#%v# \nActual:\n#%v#", expected, actual)
122126
}
123127
}
128+
129+
func TestDiffManifests(t *testing.T) {
130+
specBeta := map[string]*manifest.MappingResult{
131+
"default, nginx, Deployment (apps)": &manifest.MappingResult{
132+
133+
Name: "default, nginx, Deployment (apps)",
134+
Kind: "Deployment",
135+
Content: `
136+
apiVersion: apps/v1beta1
137+
kind: Deployment
138+
metadata:
139+
name: nginx
140+
`,
141+
}}
142+
143+
specRelease := map[string]*manifest.MappingResult{
144+
"default, nginx, Deployment (apps)": &manifest.MappingResult{
145+
146+
Name: "default, nginx, Deployment (apps)",
147+
Kind: "Deployment",
148+
Content: `
149+
apiVersion: apps/v1
150+
kind: Deployment
151+
metadata:
152+
name: nginx
153+
`,
154+
}}
155+
156+
var buf bytes.Buffer
157+
DiffManifests(specBeta, specRelease, []string{}, 10, &buf)
158+
159+
require.Equal(t, `default, nginx, Deployment (apps) has changed:
160+
161+
- apiVersion: apps/v1beta1
162+
+ apiVersion: apps/v1
163+
kind: Deployment
164+
metadata:
165+
name: nginx
166+
167+
`, buf.String())
168+
}

glide.lock

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ import:
1515
version: 1.7.2
1616
- package: golang.org/x/net
1717
version: 1c05540f6879653db88113bc4a2b70aec4bd491f
18+
testImport:
19+
- package: github.com/stretchr/testify
20+
version: ^1.2.2

manifest/parse.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ type metadata struct {
2929
}
3030

3131
func (m metadata) String() string {
32-
return fmt.Sprintf("%s, %s, %s (%s)", m.Metadata.Namespace, m.Metadata.Name, m.Kind, m.ApiVersion)
32+
apiBase := m.ApiVersion
33+
sp := strings.Split(apiBase, "/")
34+
if len(sp) > 1 {
35+
apiBase = strings.Join(sp[:len(sp)-1], "/")
36+
}
37+
38+
return fmt.Sprintf("%s, %s, %s (%s)", m.Metadata.Namespace, m.Metadata.Name, m.Kind, apiBase)
3339
}
3440

3541
func scanYamlSpecs(data []byte, atEOF bool) (advance int, token []byte, err error) {

manifest/parse_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package manifest_test
2+
3+
import (
4+
"io/ioutil"
5+
"sort"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
. "github.com/databus23/helm-diff/manifest"
11+
)
12+
13+
func foundObjects(result map[string]*MappingResult) []string {
14+
objs := make([]string, 0, len(result))
15+
for k, _ := range result {
16+
objs = append(objs, k)
17+
}
18+
sort.Strings(objs)
19+
return objs
20+
}
21+
22+
func TestPod(t *testing.T) {
23+
spec, err := ioutil.ReadFile("testdata/pod.yaml")
24+
require.NoError(t, err)
25+
26+
require.Equal(t,
27+
[]string{"default, nginx, Pod (v1)"},
28+
foundObjects(Parse(string(spec), "default")),
29+
)
30+
}
31+
32+
func TestPodNamespace(t *testing.T) {
33+
spec, err := ioutil.ReadFile("testdata/pod_namespace.yaml")
34+
require.NoError(t, err)
35+
36+
require.Equal(t,
37+
[]string{"batcave, nginx, Pod (v1)"},
38+
foundObjects(Parse(string(spec), "default")),
39+
)
40+
}
41+
42+
func TestDeployV1(t *testing.T) {
43+
spec, err := ioutil.ReadFile("testdata/deploy_v1.yaml")
44+
require.NoError(t, err)
45+
46+
require.Equal(t,
47+
[]string{"default, nginx, Deployment (apps)"},
48+
foundObjects(Parse(string(spec), "default")),
49+
)
50+
}
51+
52+
func TestDeployV1Beta1(t *testing.T) {
53+
spec, err := ioutil.ReadFile("testdata/deploy_v1beta1.yaml")
54+
require.NoError(t, err)
55+
56+
require.Equal(t,
57+
[]string{"default, nginx, Deployment (apps)"},
58+
foundObjects(Parse(string(spec), "default")),
59+
)
60+
}

manifest/testdata/deploy_v1.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
---
3+
# Source: nginx/deployment.yaml
4+
apiVersion: apps/v1
5+
kind: Deployment
6+
metadata:
7+
name: nginx
8+
spec:
9+
replicas: 3
10+
selector:
11+
matchLabels:
12+
app: nginx
13+
template:
14+
metadata:
15+
labels:
16+
app: nginx
17+
spec:
18+
containers:
19+
- name: nginx
20+
image: nginx:1.7.9
21+
ports:
22+
- containerPort: 80

manifest/testdata/deploy_v1beta1.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
---
3+
# Source: nginx/deployment.yaml
4+
apiVersion: apps/v1beta1
5+
kind: Deployment
6+
metadata:
7+
name: nginx
8+
spec:
9+
replicas: 3
10+
selector:
11+
matchLabels:
12+
app: nginx
13+
template:
14+
metadata:
15+
labels:
16+
app: nginx
17+
spec:
18+
containers:
19+
- name: nginx
20+
image: nginx:1.7.9
21+
ports:
22+
- containerPort: 80

manifest/testdata/pod.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
---
3+
# Source: nginx/pod.yaml
4+
apiVersion: v1
5+
kind: Pod
6+
metadata:
7+
name: nginx
8+
spec:
9+
containers:
10+
- name: nginx
11+
image: nginx:1.7.9
12+
ports:
13+
- containerPort: 80

manifest/testdata/pod_namespace.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
---
3+
# Source: nginx/pod.yaml
4+
apiVersion: v1
5+
kind: Pod
6+
metadata:
7+
name: nginx
8+
namespace: batcave
9+
spec:
10+
containers:
11+
- name: nginx
12+
image: nginx:1.7.9
13+
ports:
14+
- containerPort: 80

0 commit comments

Comments
 (0)