mirror of
https://github.com/aquasecurity/kube-bench.git
synced 2024-11-22 16:18:07 +00:00
Add default version if version check fails.
This commit is contained in:
parent
f90dd925b8
commit
42a1068964
21
cmd/root.go
21
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
|
||||
|
22
cmd/util.go
22
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
|
||||
|
Loading…
Reference in New Issue
Block a user