Add function to retrieve Kubernetes server version.

The server version is used to load the correct benchmark check
to run against the Kubernetes cluster.
pull/52/head
Abubakr-Sadik Nii Nai Davis 7 years ago
parent 8ea0892437
commit 56fa20103a

@ -90,7 +90,9 @@ func runChecks(t check.NodeType) {
file = federatedFile
}
in, err := ioutil.ReadFile(file)
ver := getKubeVersion()
path := fmt.Sprintf("%s/%s/%s", cfgDir, ver.Server, file)
in, err := ioutil.ReadFile(path)
if err != nil {
exitWithError(fmt.Errorf("error opening %s controls file: %v", t, err))
}

@ -33,7 +33,7 @@ func init() {
federatedCmd.PersistentFlags().StringVarP(&federatedFile,
"file",
"f",
cfgDir+"/federated.yaml",
"/federated.yaml",
"Alternative YAML file for federated checks",
)

@ -33,7 +33,7 @@ func init() {
masterCmd.PersistentFlags().StringVarP(&masterFile,
"file",
"f",
cfgDir+"/master.yaml",
"/master.yaml",
"Alternative YAML file for master checks",
)

@ -33,7 +33,7 @@ func init() {
nodeCmd.PersistentFlags().StringVarP(&nodeFile,
"file",
"f",
cfgDir+"/node.yaml",
"/node.yaml",
"Alternative YAML file for node checks",
)

@ -159,3 +159,38 @@ func multiWordReplace(s string, subname string, sub string) string {
return strings.Replace(s, subname, sub, -1)
}
type version struct {
Server string
Client string
}
func getKubeVersion() *version {
ver := new(version)
// These executables might not be on the user's path.
_, err := exec.LookPath("kubectl")
if err != nil {
s := fmt.Sprintf("Kubernetes version check skipped with error %v", err)
continueWithError(err, sprintlnWarn(s))
return nil
}
cmd := exec.Command("kubectl", "version")
out, err := cmd.Output()
if err != nil {
s := fmt.Sprintf("Kubernetes version check skipped, with error getting kubectl version")
continueWithError(err, sprintlnWarn(s))
return nil
}
clientVerRe := regexp.MustCompile(`Client.*Major:"(\d+)".*Minor:"(\d+)"`)
svrVerRe := regexp.MustCompile(`Server.*Major:"(\d+)".*Minor:"(\d+)"`)
sub := clientVerRe.FindStringSubmatch(string(out))
ver.Client = sub[1] + "." + sub[2]
sub = svrVerRe.FindStringSubmatch(string(out))
ver.Server = sub[1] + "." + sub[2]
return ver
}

@ -129,3 +129,18 @@ func TestMultiWordReplace(t *testing.T) {
})
}
}
func TestGetKubeVersion(t *testing.T) {
ver := getKubeVersion()
if ver == nil {
t.Log("Expected non nil version info.")
} else {
if ok, err := regexp.MatchString(`\d+.\d+`, ver.Client); !ok && err != nil {
t.Logf("Expected:%v got %v\n", "n.m", ver.Client)
}
if ok, err := regexp.MatchString(`\d+.\d+`, ver.Server); !ok && err != nil {
t.Logf("Expected:%v got %v\n", "n.m", ver.Server)
}
}
}

Loading…
Cancel
Save