Skip to content

Commit

Permalink
feat: Improved diff on apiVersion changes (databus23#73)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
novas0x2a authored and mumoshu committed Jul 25, 2018
1 parent fa12c4c commit 1846c74
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 5 deletions.
47 changes: 46 additions & 1 deletion diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package diff

import (
"testing"
"bytes"
"testing"

"github.com/mgutz/ansi"
"github.com/stretchr/testify/require"

"github.com/databus23/helm-diff/manifest"
)

var text1 = "" +
Expand Down Expand Up @@ -121,3 +125,44 @@ func assertDiff(t *testing.T, before, after string, context int, expected string
t.Errorf("Unexpected diff output: \nExpected:\n#%v# \nActual:\n#%v#", expected, actual)
}
}

func TestDiffManifests(t *testing.T) {
specBeta := map[string]*manifest.MappingResult{
"default, nginx, Deployment (apps)": &manifest.MappingResult{

Name: "default, nginx, Deployment (apps)",
Kind: "Deployment",
Content: `
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx
`,
}}

specRelease := map[string]*manifest.MappingResult{
"default, nginx, Deployment (apps)": &manifest.MappingResult{

Name: "default, nginx, Deployment (apps)",
Kind: "Deployment",
Content: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
`,
}}

var buf bytes.Buffer
DiffManifests(specBeta, specRelease, []string{}, 10, &buf)

require.Equal(t, `default, nginx, Deployment (apps) has changed:
- apiVersion: apps/v1beta1
+ apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
`, buf.String())
}
19 changes: 16 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ import:
version: 1.7.2
- package: golang.org/x/net
version: 1c05540f6879653db88113bc4a2b70aec4bd491f
testImport:
- package: github.com/stretchr/testify
version: ^1.2.2
8 changes: 7 additions & 1 deletion manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ type metadata struct {
}

func (m metadata) String() string {
return fmt.Sprintf("%s, %s, %s (%s)", m.Metadata.Namespace, m.Metadata.Name, m.Kind, m.ApiVersion)
apiBase := m.ApiVersion
sp := strings.Split(apiBase, "/")
if len(sp) > 1 {
apiBase = strings.Join(sp[:len(sp)-1], "/")
}

return fmt.Sprintf("%s, %s, %s (%s)", m.Metadata.Namespace, m.Metadata.Name, m.Kind, apiBase)
}

func scanYamlSpecs(data []byte, atEOF bool) (advance int, token []byte, err error) {
Expand Down
60 changes: 60 additions & 0 deletions manifest/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package manifest_test

import (
"io/ioutil"
"sort"
"testing"

"github.com/stretchr/testify/require"

. "github.com/databus23/helm-diff/manifest"
)

func foundObjects(result map[string]*MappingResult) []string {
objs := make([]string, 0, len(result))
for k, _ := range result {
objs = append(objs, k)
}
sort.Strings(objs)
return objs
}

func TestPod(t *testing.T) {
spec, err := ioutil.ReadFile("testdata/pod.yaml")
require.NoError(t, err)

require.Equal(t,
[]string{"default, nginx, Pod (v1)"},
foundObjects(Parse(string(spec), "default")),
)
}

func TestPodNamespace(t *testing.T) {
spec, err := ioutil.ReadFile("testdata/pod_namespace.yaml")
require.NoError(t, err)

require.Equal(t,
[]string{"batcave, nginx, Pod (v1)"},
foundObjects(Parse(string(spec), "default")),
)
}

func TestDeployV1(t *testing.T) {
spec, err := ioutil.ReadFile("testdata/deploy_v1.yaml")
require.NoError(t, err)

require.Equal(t,
[]string{"default, nginx, Deployment (apps)"},
foundObjects(Parse(string(spec), "default")),
)
}

func TestDeployV1Beta1(t *testing.T) {
spec, err := ioutil.ReadFile("testdata/deploy_v1beta1.yaml")
require.NoError(t, err)

require.Equal(t,
[]string{"default, nginx, Deployment (apps)"},
foundObjects(Parse(string(spec), "default")),
)
}
22 changes: 22 additions & 0 deletions manifest/testdata/deploy_v1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

---
# Source: nginx/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
22 changes: 22 additions & 0 deletions manifest/testdata/deploy_v1beta1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

---
# Source: nginx/deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
13 changes: 13 additions & 0 deletions manifest/testdata/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

---
# Source: nginx/pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
14 changes: 14 additions & 0 deletions manifest/testdata/pod_namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

---
# Source: nginx/pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: batcave
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80

0 comments on commit 1846c74

Please sign in to comment.