From 227fec14ae1115cdad7359a142c6407a75540ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Wed, 1 Apr 2020 17:19:14 +0200 Subject: [PATCH] Use kubectl version --short if --output=json fails Really old kubernetes clients did not have the --output parameter Show a friendly reminder to upgrade, instead of an error message. --- cmd/minikube/cmd/start.go | 41 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 780e2ec766..0c3b233412 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -433,22 +433,12 @@ func showKubectlInfo(kcs *kubeconfig.Settings, k8sVersion string, machineName st return nil } - j, err := exec.Command(path, "version", "--client", "--output=json").Output() + gitVersion, err := kubectlVersion(path) if err != nil { - return errors.Wrap(err, "exec") + return err } - cv := struct { - ClientVersion struct { - GitVersion string `json:"gitVersion"` - } `json:"clientVersion"` - }{} - err = json.Unmarshal(j, &cv) - if err != nil { - return errors.Wrap(err, "unmarshal") - } - - client, err := semver.Make(strings.TrimPrefix(cv.ClientVersion.GitVersion, version.VersionPrefix)) + client, err := semver.Make(strings.TrimPrefix(gitVersion, version.VersionPrefix)) if err != nil { return errors.Wrap(err, "client semver") } @@ -467,6 +457,31 @@ func showKubectlInfo(kcs *kubeconfig.Settings, k8sVersion string, machineName st return nil } +func kubectlVersion(path string) (string, error) { + j, err := exec.Command(path, "version", "--client", "--output=json").Output() + if err != nil { + // really old kubernetes clients did not have the --output parameter + b, err := exec.Command(path, "version", "--client", "--short").Output() + if err != nil { + return "", errors.Wrap(err, "exec") + } + s := strings.TrimSpace(string(b)) + return strings.Replace(s, "Client Version: ", "", 1), nil + } + + cv := struct { + ClientVersion struct { + GitVersion string `json:"gitVersion"` + } `json:"clientVersion"` + }{} + err = json.Unmarshal(j, &cv) + if err != nil { + return "", errors.Wrap(err, "unmarshal") + } + + return cv.ClientVersion.GitVersion, nil +} + func selectDriver(existing *config.ClusterConfig) registry.DriverState { // Technically unrelated, but important to perform before detection driver.SetLibvirtURI(viper.GetString(kvmQemuURI))