Skip to content

Commit

Permalink
Warn if incompatible kubectl version is in use
Browse files Browse the repository at this point in the history
Fixes: kubernetes#3329

Modification is done inside start.go where additional checking on the
version returned via the kubectl CLI is checked.

Running 'kubectl version --output=json' will return both client
and server information.
  • Loading branch information
nanikjava committed Oct 11, 2019
1 parent 0a67493 commit 9bf9041
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -130,6 +131,17 @@ var (
extraOptions cfg.ExtraOptionSlice
)

type kubectlversion struct {
CVersion ClientVersion `json:"clientVersion"`
SVersion ClientVersion `json:"serverVersion"`
}

type ClientVersion struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
}

func init() {
initMinikubeFlags()
initKubernetesFlags()
Expand Down Expand Up @@ -477,16 +489,57 @@ func showVersionInfo(k8sVersion string, cr cruntime.Manager) {
}
}

/**
Function to check for kubectl. The checking is to compare
the version reported by both the client and server. It is
that kubectl will be of the same version or 1 lower than
the kubernetes version.
*/
func showKubectlConnectInfo(kcs *kubeconfig.Settings) {
var output []byte
clientVersion := kubectlversion{}
serverVersion := kubectlversion{}

path, err := exec.LookPath("kubectl")

if err != nil {
out.T(out.Tip, "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/")
} else {
glog.Infof("Kubectl found at the following location %s. Let's execute and get the result", path)
output, err = exec.Command(path, "version", "--output=json").Output()

// ...once we get something back do some version checking
if err == nil {
glog.Infof("Received output from kubectl %s", output)

// unmarshal both the {client}
clientjsonErr := json.Unmarshal(output, &clientVersion)

// ....... and {server} json
serverjsonErr := json.Unmarshal(output, &serverVersion)

if (clientjsonErr != nil) || (serverjsonErr != nil) {
glog.Infof("There was an error processing the json output")
} else {
// obtain the minor version for both
serverMinor, _ := strconv.Atoi(serverVersion.SVersion.Minor)
clientMinor, _ := strconv.Atoi(serverVersion.CVersion.Minor)

if clientMinor < serverMinor {
out.T(out.Tip, "The version of kubectl {{.kubectl}} installed is incompatible with your Kubernetes {{.kubernetes}} deployment. Please upgrade/downgrade as necessary, or use minikube kubectlto connect to your cluster",
out.V{"kubectl": clientVersion.CVersion.GitVersion, "kubernetes": serverVersion.SVersion.GitVersion})
}
}
} else {
out.T(out.Tip, "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/")
}
}

if kcs.KeepContext {
out.T(out.Kubectl, "To connect to this cluster, use: kubectl --context={{.name}}", out.V{"name": kcs.ClusterName})
} else {
out.T(out.Ready, `Done! kubectl is now configured to use "{{.name}}"`, out.V{"name": cfg.GetMachineName()})
}
_, err := exec.LookPath("kubectl")
if err != nil {
out.T(out.Tip, "For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/")
}
}

func selectDriver(oldConfig *cfg.Config) string {
Expand Down

0 comments on commit 9bf9041

Please sign in to comment.