Skip to content

Commit

Permalink
Optimize version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
alexppg committed Jun 27, 2020
1 parent a03f486 commit 0264f8c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
86 changes: 61 additions & 25 deletions internal/helpers/kube_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
1 change: 0 additions & 1 deletion internal/wrapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 0264f8c

Please sign in to comment.