forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
45 lines (39 loc) · 1.06 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package kubernetes
import (
"encoding/json"
"log"
"os/exec"
"strings"
)
// Config represents a kubernetes config object
type Config struct {
Clusters []Cluster `json:"clusters"`
}
// Cluster contains the name and the cluster info
type Cluster struct {
Name string `json:"name"`
ClusterInfo ClusterInfo `json:"cluster"`
}
// ClusterInfo holds the server and cert
type ClusterInfo struct {
Server string `json:"server"`
}
// GetConfig returns a Config value representing the current kubeconfig
func GetConfig() (*Config, error) {
out, err := exec.Command("kubectl", "config", "view", "-o", "json").CombinedOutput()
if err != nil {
log.Printf("Error trying to run 'kubectl config view':%s\n", err)
return nil, err
}
c := Config{}
err = json.Unmarshal(out, &c)
if err != nil {
log.Printf("Error unmarshalling config json:%s\n", err)
}
return &c, nil
}
// GetServerName returns the server for the given config in an sshable format
func (c *Config) GetServerName() string {
s := c.Clusters[0].ClusterInfo.Server
return strings.Split(s, "://")[1]
}