blob: dade6a67ad938c0918b4596ae9d6e6c1d00f29ea [file] [log] [blame]
Pawel Wieczorek2b5b2e02019-08-06 16:04:53 +02001// Package config reads relevant SSH access information from cluster config declaration.
2package config
3
4import (
5 "io/ioutil"
6
7 v3 "github.com/rancher/types/apis/management.cattle.io/v3"
8 "gopkg.in/yaml.v2"
9)
10
11const (
12 defaultConfigFile = "cluster.yml"
13)
14
15// NodeInfo contains role and SSH access information for a single cluster node.
16type 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.
25func 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
45func 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
53func 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}