7
7
"os"
8
8
"os/exec"
9
9
10
+ "github.com/Masterminds/semver"
10
11
"github.com/stretchr/objx"
11
12
funk "github.com/thoas/go-funk"
12
13
)
@@ -17,6 +18,24 @@ type Kubectl struct {
17
18
APIServer string
18
19
}
19
20
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
+
20
39
// setupContext uses `kubectl config view` to obtain the KUBECONFIG and extracts the correct context from it
21
40
func (k Kubectl ) setupContext () error {
22
41
cmd := exec .Command ("kubectl" , "config" , "view" , "-o" , "json" )
@@ -94,6 +113,15 @@ func (k Kubectl) Diff(yaml string) (string, error) {
94
113
if err := k .setupContext (); err != nil {
95
114
return "" , err
96
115
}
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
+
97
125
argv := []string {"diff" ,
98
126
"--context" , k .context ,
99
127
"-f" , "-" ,
0 commit comments