Skip to content

Commit a84b3a9

Browse files
authored
Merge pull request #5596 from nanikjava/f-fix-3329
Warn if incompatible kubectl version is in use
2 parents 3f86651 + 789cc7a commit a84b3a9

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

cmd/minikube/cmd/start.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
22+
"math"
2123
"net"
2224
"net/url"
2325
"os"
@@ -130,6 +132,17 @@ var (
130132
extraOptions cfg.ExtraOptionSlice
131133
)
132134

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+
133146
func init() {
134147
initMinikubeFlags()
135148
initKubernetesFlags()
@@ -477,15 +490,49 @@ func showVersionInfo(k8sVersion string, cr cruntime.Manager) {
477490
}
478491
}
479492

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+
*/
480499
func showKubectlConnectInfo(kcs *kubeconfig.Settings) {
500+
var output []byte
501+
clientVersion := kubectlversion{}
502+
481503
if kcs.KeepContext {
482504
out.T(out.Kubectl, "To connect to this cluster, use: kubectl --context={{.name}}", out.V{"name": kcs.ClusterName})
483505
} else {
484506
out.T(out.Ready, `Done! kubectl is now configured to use "{{.name}}"`, out.V{"name": cfg.GetMachineName()})
485507
}
486-
_, err := exec.LookPath("kubectl")
508+
509+
path, err := exec.LookPath("kubectl")
510+
// ...not found just print and return
487511
if err != nil {
488512
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})
489536
}
490537
}
491538

0 commit comments

Comments
 (0)