Skip to content

Commit

Permalink
Add function to retrieve Kubernetes server version.
Browse files Browse the repository at this point in the history
The server version is used to load the correct benchmark check
to run against the Kubernetes cluster.
  • Loading branch information
ttousai committed Sep 17, 2017
1 parent 8ea0892 commit 56fa201
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/federated.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
federatedCmd.PersistentFlags().StringVarP(&federatedFile,
"file",
"f",
cfgDir+"/federated.yaml",
"/federated.yaml",
"Alternative YAML file for federated checks",
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
masterCmd.PersistentFlags().StringVarP(&masterFile,
"file",
"f",
cfgDir+"/master.yaml",
"/master.yaml",
"Alternative YAML file for master checks",
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
nodeCmd.PersistentFlags().StringVarP(&nodeFile,
"file",
"f",
cfgDir+"/node.yaml",
"/node.yaml",
"Alternative YAML file for node checks",
)

Expand Down
35 changes: 35 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 56fa201

Please sign in to comment.