From 56fa20103a851f4e48274429e7cdbde9258a9e80 Mon Sep 17 00:00:00 2001
From: Abubakr-Sadik Nii Nai Davis <dwa2pac@gmail.com>
Date: Sun, 17 Sep 2017 14:35:25 +0000
Subject: [PATCH] Add function to retrieve Kubernetes server version.

The server version is used to load the correct benchmark check
to run against the Kubernetes cluster.
---
 cmd/common.go    |  4 +++-
 cmd/federated.go |  2 +-
 cmd/master.go    |  2 +-
 cmd/node.go      |  2 +-
 cmd/util.go      | 35 +++++++++++++++++++++++++++++++++++
 cmd/util_test.go | 15 +++++++++++++++
 6 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/cmd/common.go b/cmd/common.go
index 89f45bc..1200d4e 100644
--- a/cmd/common.go
+++ b/cmd/common.go
@@ -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))
 	}
diff --git a/cmd/federated.go b/cmd/federated.go
index 1ebc251..0f9dbf3 100644
--- a/cmd/federated.go
+++ b/cmd/federated.go
@@ -33,7 +33,7 @@ func init() {
 	federatedCmd.PersistentFlags().StringVarP(&federatedFile,
 		"file",
 		"f",
-		cfgDir+"/federated.yaml",
+		"/federated.yaml",
 		"Alternative YAML file for federated checks",
 	)
 
diff --git a/cmd/master.go b/cmd/master.go
index b702615..1659d38 100644
--- a/cmd/master.go
+++ b/cmd/master.go
@@ -33,7 +33,7 @@ func init() {
 	masterCmd.PersistentFlags().StringVarP(&masterFile,
 		"file",
 		"f",
-		cfgDir+"/master.yaml",
+		"/master.yaml",
 		"Alternative YAML file for master checks",
 	)
 
diff --git a/cmd/node.go b/cmd/node.go
index 9ea68ba..b07ed7e 100644
--- a/cmd/node.go
+++ b/cmd/node.go
@@ -33,7 +33,7 @@ func init() {
 	nodeCmd.PersistentFlags().StringVarP(&nodeFile,
 		"file",
 		"f",
-		cfgDir+"/node.yaml",
+		"/node.yaml",
 		"Alternative YAML file for node checks",
 	)
 
diff --git a/cmd/util.go b/cmd/util.go
index 478ae21..fa926d8 100644
--- a/cmd/util.go
+++ b/cmd/util.go
@@ -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
+}
diff --git a/cmd/util_test.go b/cmd/util_test.go
index dbd434b..40994a1 100644
--- a/cmd/util_test.go
+++ b/cmd/util_test.go
@@ -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)
+		}
+	}
+}
-- 
GitLab