From 0264f8c506425fc881efc97fda7854f83f458c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=80lex=20P=C3=A9rez?= Date: Sat, 27 Jun 2020 14:28:25 +0200 Subject: [PATCH] Optimize version detection --- internal/helpers/kube_client.go | 86 +++++++++++++++++++++++---------- internal/wrapper/main.go | 1 - 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/internal/helpers/kube_client.go b/internal/helpers/kube_client.go index 12ff982..ce7faf6 100644 --- a/internal/helpers/kube_client.go +++ b/internal/helpers/kube_client.go @@ -7,47 +7,65 @@ import ( "os" "path/filepath" "strings" + "time" + + "os/exec" + "runtime" "github.com/mitchellh/go-homedir" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) func KubeGetVersion() (string, error) { - var kubeconfig *string + var kubeconfig string + var cli *string var err error var home string var version string + var config *rest.Config - home, _ = homedir.Dir() - - kubeconfigVar := os.Getenv("KUBECONFIG") + cli = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") + flag.Parse() - if kubeconfigVar != "" { // nolint: gocritic - kubeconfig = flag.String("kubeconfig", kubeconfigVar, "absolute path to the kubeconfig file") - } else if FileExists(filepath.Join(home, ".kube", "config")) { - kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "absolute path to the kubeconfig file") + // Try first --kubeconfig parameter + if *cli != "" { + kubeconfig = *cli } else { - kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") + home, _ = homedir.Dir() + + kubeconfigVar := os.Getenv("KUBECONFIG") + + // Try first KUBECONFIG env var + // If not set, try default kubeconfig path + // If not find, let it empty + if kubeconfigVar != "" { // nolint: gocritic + kubeconfig = kubeconfigVar + } else if FileExists(filepath.Join(home, ".kube", "config")) { + kubeconfig = filepath.Join(home, ".kube", "config") + } } - flag.Parse() - - config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) - if err != nil { - version = getLatestVersion() + // If no kubeconfig + if kubeconfig == "" { + version = getDefaultVersion() return version, nil } + config, _ = clientcmd.BuildConfigFromFlags("", kubeconfig) + config.Timeout = 1 * time.Second + client, err := kubernetes.NewForConfig(config) + if err != nil { - version = getLatestVersion() + version = getDefaultVersion() return version, nil } v, err := client.DiscoveryClient.ServerVersion() if err != nil { - version = getLatestVersion() + version = getDefaultVersion() return version, nil } @@ -56,19 +74,37 @@ func KubeGetVersion() (string, error) { return version, nil } -func getLatestVersion() string { - resp, err := http.Get("https://storage.googleapis.com/kubernetes-release/release/stable.txt") - CheckGenericError(err) +func getDefaultVersion() string { + var fileExt string + var version string + + if runtime.GOOS == "windows" { + fileExt = ".exe" + } - defer resp.Body.Close() + // See if there's any kubectl version installed + args := []string{"list", "local"} + cmd := exec.Command("kbenv"+fileExt, args...) // nolint: gosec + output, _ := cmd.Output() + out := string(output) - body, err := ioutil.ReadAll(resp.Body) - CheckGenericError(err) + // If there's no kubectl, get latest + // If there's at least one, use that + if out == "" { + resp, err := http.Get("https://storage.googleapis.com/kubernetes-release/release/stable.txt") + CheckGenericError(err) - bodyText := string(body) - version := bodyText[1:] + defer resp.Body.Close() - version = strings.Trim(version, "\n") + body, err := ioutil.ReadAll(resp.Body) + CheckGenericError(err) + + bodyText := string(body) + version = strings.Trim(bodyText[1:], "\n") + } else { + lines := strings.Split(out, "\n") + version = lines[0] + } return version } diff --git a/internal/wrapper/main.go b/internal/wrapper/main.go index 676f55c..e9ef4a0 100644 --- a/internal/wrapper/main.go +++ b/internal/wrapper/main.go @@ -71,7 +71,6 @@ func Wrapper(binName string) { // nolint: funlen if !helpers.FileExists(bin) { args := []string{"install", version} cmd := exec.Command("kbenv"+fileExt, args...) // nolint: gosec - cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Run()