@@ -17,7 +17,9 @@ limitations under the License.
17
17
package cmd
18
18
19
19
import (
20
+ "encoding/json"
20
21
"fmt"
22
+ "math"
21
23
"net"
22
24
"net/url"
23
25
"os"
@@ -130,6 +132,17 @@ var (
130
132
extraOptions cfg.ExtraOptionSlice
131
133
)
132
134
135
+ type kubectlversion struct {
136
+ CVersion VersionInfo `json:"clientVersion"`
137
+ SVersion VersionInfo `json:"serverVersion"`
138
+ }
139
+
140
+ type VersionInfo struct {
141
+ Major string `json:"major"`
142
+ Minor string `json:"minor"`
143
+ GitVersion string `json:"gitVersion"`
144
+ }
145
+
133
146
func init () {
134
147
initMinikubeFlags ()
135
148
initKubernetesFlags ()
@@ -477,15 +490,49 @@ func showVersionInfo(k8sVersion string, cr cruntime.Manager) {
477
490
}
478
491
}
479
492
493
+ /**
494
+ Function to check for kubectl. The checking is to compare
495
+ the version reported by both the client and server. Checking
496
+ is based on what is outlined in Kubernetes document
497
+ https://kubernetes.io/docs/setup/release/version-skew-policy/#kubectl
498
+ */
480
499
func showKubectlConnectInfo (kcs * kubeconfig.Settings ) {
500
+ var output []byte
501
+ clientVersion := kubectlversion {}
502
+
481
503
if kcs .KeepContext {
482
504
out .T (out .Kubectl , "To connect to this cluster, use: kubectl --context={{.name}}" , out.V {"name" : kcs .ClusterName })
483
505
} else {
484
506
out .T (out .Ready , `Done! kubectl is now configured to use "{{.name}}"` , out.V {"name" : cfg .GetMachineName ()})
485
507
}
486
- _ , err := exec .LookPath ("kubectl" )
508
+
509
+ path , err := exec .LookPath ("kubectl" )
510
+ // ...not found just print and return
487
511
if err != nil {
488
512
out .T (out .Tip , "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/" )
513
+ return
514
+ }
515
+
516
+ output , err = exec .Command (path , "version" , "--output=json" ).Output ()
517
+ if err != nil {
518
+ return
519
+ }
520
+ glog .Infof ("Received output from kubectl %s" , output )
521
+
522
+ // unmarshal the json
523
+ output = []byte ("Nanik" )
524
+ clientjsonErr := json .Unmarshal (output , & clientVersion )
525
+ if clientjsonErr != nil {
526
+ glog .Infof ("There was an error processing kubectl json output." )
527
+ return
528
+ }
529
+ // obtain the minor version for both client & server
530
+ serverMinor , _ := strconv .Atoi (clientVersion .SVersion .Minor )
531
+ clientMinor , _ := strconv .Atoi (clientVersion .CVersion .Minor )
532
+
533
+ if math .Abs (float64 (clientMinor - serverMinor )) > 1 {
534
+ out .T (out .Tip , "{{.path}} is version {{.clientMinor}}, and is incompatible with your specified Kubernetes version. You will need to update {{.path}} or use 'minikube kubectl' to connect with this cluster" ,
535
+ out.V {"path" : path , "clientMinor" : clientMinor })
489
536
}
490
537
}
491
538
0 commit comments