Pawel Wieczorek | 2b5b2e0 | 2019-08-06 16:04:53 +0200 | [diff] [blame] | 1 | // Package config reads relevant SSH access information from cluster config declaration. |
| 2 | package config |
| 3 | |
| 4 | import ( |
| 5 | "io/ioutil" |
| 6 | |
| 7 | v3 "github.com/rancher/types/apis/management.cattle.io/v3" |
| 8 | "gopkg.in/yaml.v2" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | defaultConfigFile = "cluster.yml" |
| 13 | ) |
| 14 | |
| 15 | // NodeInfo contains role and SSH access information for a single cluster node. |
| 16 | type NodeInfo struct { |
| 17 | Role []string |
| 18 | User string |
| 19 | Address string |
| 20 | Port string |
| 21 | SSHKeyPath string |
| 22 | } |
| 23 | |
| 24 | // GetNodesInfo returns nodes' roles and SSH access information for a whole cluster. |
| 25 | func GetNodesInfo() ([]NodeInfo, error) { |
| 26 | config, err := readConfig(defaultConfigFile) |
| 27 | if err != nil { |
| 28 | return []NodeInfo{}, err |
| 29 | } |
| 30 | |
| 31 | cluster, err := parseConfig(config) |
| 32 | if err != nil { |
| 33 | return []NodeInfo{}, err |
| 34 | } |
| 35 | |
| 36 | var nodes []NodeInfo |
| 37 | for _, node := range cluster.Nodes { |
| 38 | nodes = append(nodes, NodeInfo{ |
| 39 | node.Role, node.User, node.Address, node.Port, node.SSHKeyPath, |
| 40 | }) |
| 41 | } |
| 42 | return nodes, nil |
| 43 | } |
| 44 | |
| 45 | func readConfig(configFile string) (string, error) { |
| 46 | config, err := ioutil.ReadFile(configFile) |
| 47 | if err != nil { |
| 48 | return "", err |
| 49 | } |
| 50 | return string(config), nil |
| 51 | } |
| 52 | |
| 53 | func parseConfig(config string) (*v3.RancherKubernetesEngineConfig, error) { |
| 54 | var rkeConfig v3.RancherKubernetesEngineConfig |
| 55 | if err := yaml.Unmarshal([]byte(config), &rkeConfig); err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | return &rkeConfig, nil |
| 59 | } |