diff --git a/cmd/root.go b/cmd/root.go index 08be2b3..3c80f5d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,16 +25,17 @@ import ( ) var ( - envVarsPrefix = "KUBE_BENCH" - cfgDir = "./cfg" - cfgFile string - jsonFmt bool - pgSql bool - checkList string - groupList string - masterFile string - nodeFile string - federatedFile string + envVarsPrefix = "KUBE_BENCH" + cfgDir = "./cfg" + defaultKubeVersion = "1.6" + cfgFile string + jsonFmt bool + pgSql bool + checkList string + groupList string + masterFile string + nodeFile string + federatedFile string ) // RootCmd represents the base command when called without any subcommands diff --git a/cmd/util.go b/cmd/util.go index 97b5544..6addcfc 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -214,25 +214,35 @@ func multiWordReplace(s string, subname string, sub string) string { } func getKubeVersion() string { + var ver string + var matched bool + failmsg := "kubernetes version check failed" // These executables might not be on the user's path. _, err := exec.LookPath("kubectl") if err != nil { - exitWithError(fmt.Errorf("%s: %s", failmsg, err)) + continueWithError(err, failmsg) } cmd := exec.Command("kubectl", "version", "--short") out, err := cmd.CombinedOutput() if err != nil { - exitWithError(fmt.Errorf("%s, %s", failmsg, out)) + continueWithError(fmt.Errorf("%s", out), "") } - validVersionPttn := `\d.\d` serverVersionRe := regexp.MustCompile(`Server Version: v(\d+.\d+)`) - ver := serverVersionRe.FindStringSubmatch(string(out))[1] + subs := serverVersionRe.FindStringSubmatch(string(out)) + if len(subs) > 2 { + ver = string(subs[1]) + validVersionPttn := `\d.\d` + if matched, _ = regexp.MatchString(validVersionPttn, ver); !matched { + continueWithError(fmt.Errorf("%s: invalid server version ", ver), failmsg) + } + } - if matched, _ := regexp.MatchString(validVersionPttn, ver); !matched { - exitWithError(fmt.Errorf("%s: invalid server version ", failmsg, ver)) + if ver == "" || !matched { + printlnWarn(fmt.Sprintf("Unable to get kubectl version, using default version: %s", defaultKubeVersion)) + ver = defaultKubeVersion } return ver