aboutsummaryrefslogtreecommitdiffstats
path: root/test/security/k8s/src/check/config/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'test/security/k8s/src/check/config/config.go')
-rw-r--r--test/security/k8s/src/check/config/config.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/security/k8s/src/check/config/config.go b/test/security/k8s/src/check/config/config.go
new file mode 100644
index 000000000..dade6a67a
--- /dev/null
+++ b/test/security/k8s/src/check/config/config.go
@@ -0,0 +1,59 @@
+// Package config reads relevant SSH access information from cluster config declaration.
+package config
+
+import (
+ "io/ioutil"
+
+ v3 "github.com/rancher/types/apis/management.cattle.io/v3"
+ "gopkg.in/yaml.v2"
+)
+
+const (
+ defaultConfigFile = "cluster.yml"
+)
+
+// NodeInfo contains role and SSH access information for a single cluster node.
+type NodeInfo struct {
+ Role []string
+ User string
+ Address string
+ Port string
+ SSHKeyPath string
+}
+
+// GetNodesInfo returns nodes' roles and SSH access information for a whole cluster.
+func GetNodesInfo() ([]NodeInfo, error) {
+ config, err := readConfig(defaultConfigFile)
+ if err != nil {
+ return []NodeInfo{}, err
+ }
+
+ cluster, err := parseConfig(config)
+ if err != nil {
+ return []NodeInfo{}, err
+ }
+
+ var nodes []NodeInfo
+ for _, node := range cluster.Nodes {
+ nodes = append(nodes, NodeInfo{
+ node.Role, node.User, node.Address, node.Port, node.SSHKeyPath,
+ })
+ }
+ return nodes, nil
+}
+
+func readConfig(configFile string) (string, error) {
+ config, err := ioutil.ReadFile(configFile)
+ if err != nil {
+ return "", err
+ }
+ return string(config), nil
+}
+
+func parseConfig(config string) (*v3.RancherKubernetesEngineConfig, error) {
+ var rkeConfig v3.RancherKubernetesEngineConfig
+ if err := yaml.Unmarshal([]byte(config), &rkeConfig); err != nil {
+ return nil, err
+ }
+ return &rkeConfig, nil
+}