Skip to content

Commit 166614c

Browse files
committed
feat(kubernetes): check for version in diff mode
1 parent bc16b2a commit 166614c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/sh0rez/tanka
33
go 1.12
44

55
require (
6+
github.com/Masterminds/semver v1.4.2
67
github.com/alecthomas/chroma v0.6.6
78
github.com/google/go-jsonnet v0.13.0
89
github.com/mitchellh/mapstructure v1.1.2

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
44
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
55
github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0=
6+
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
7+
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
68
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
79
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
810
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
@@ -103,6 +105,7 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
103105
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
104106
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
105107
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
108+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
106109
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
107110
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
108111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

pkg/provider/kubernetes/kubectl.go

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99

10+
"github.com/Masterminds/semver"
1011
"github.com/stretchr/objx"
1112
funk "github.com/thoas/go-funk"
1213
)
@@ -17,6 +18,24 @@ type Kubectl struct {
1718
APIServer string
1819
}
1920

21+
// Version returns the version of kubectl and the Kubernetes api server
22+
func (k Kubectl) Version() (client, server semver.Version, err error) {
23+
zero := *semver.MustParse("0.0.0")
24+
cmd := exec.Command("kubectl", "version",
25+
"-o", "json",
26+
"--context", k.context,
27+
)
28+
var buf bytes.Buffer
29+
cmd.Stdout = &buf
30+
if err := cmd.Run(); err != nil {
31+
return zero, zero, err
32+
}
33+
vs := objx.MustFromJSON(buf.String())
34+
client = *semver.MustParse(vs.Get("clientVersion.gitVersion").MustStr())
35+
server = *semver.MustParse(vs.Get("serverVersion.gitVersion").MustStr())
36+
return client, server, nil
37+
}
38+
2039
// setupContext uses `kubectl config view` to obtain the KUBECONFIG and extracts the correct context from it
2140
func (k Kubectl) setupContext() error {
2241
cmd := exec.Command("kubectl", "config", "view", "-o", "json")
@@ -94,6 +113,15 @@ func (k Kubectl) Diff(yaml string) (string, error) {
94113
if err := k.setupContext(); err != nil {
95114
return "", err
96115
}
116+
117+
client, server, err := k.Version()
118+
if !client.GreaterThan(semver.MustParse("1.13.0")) || !server.GreaterThan(semver.MustParse("1.13.0")) {
119+
return "", fmt.Errorf("The kubernetes diff feature requires at least version 1.13 on both, kubectl (is `%s`) and server (is `%s`)", client.String(), server.String())
120+
}
121+
if err != nil {
122+
return "", err
123+
}
124+
97125
argv := []string{"diff",
98126
"--context", k.context,
99127
"-f", "-",

0 commit comments

Comments
 (0)