-
Notifications
You must be signed in to change notification settings - Fork 3
/
kubedconf.go
115 lines (99 loc) · 2.35 KB
/
kubedconf.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"errors"
"io/ioutil"
"path/filepath"
log "github.com/Sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
// Cluster structure to setup kubeconfig
type Cluster struct {
Name string `yaml:"name"`
APIServer string `yaml:"apiserver"`
IssuerURL string `yaml:"issuer"`
ClientID string `yaml:"clientid"`
KubeConfig string `yaml:"kubeconfig"`
KeepContext bool `yaml:"keepcontext"`
Port int `yaml:"port"`
NameSpace string `yaml:"namespace"`
ManualInput bool `yaml:"manualinput"`
}
func readConfig(name string) (*Cluster, error) {
path := filepath.Join(home, kubedConf)
confBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Warn("Failed in reading kubed config file ", err)
return nil, err
}
var clusters []Cluster
err = yaml.Unmarshal(confBytes, &clusters)
if err != nil {
log.Error("Failed in parsing config file ", err)
}
for _, c := range clusters {
if c.Name == name {
return &c, nil
}
}
return nil, errors.New("Provided cluster not found, run with full config parameters to configure it")
}
func setConfig(
name string,
apiserver string,
issuerURL string,
clientID string,
kubeconfig string,
keepContext bool,
port int,
namespace string,
manualInput bool) *Cluster {
return &Cluster{
Name: name,
APIServer: apiserver,
IssuerURL: issuerURL,
ClientID: clientID,
KubeConfig: kubeconfig,
KeepContext: keepContext,
Port: port,
NameSpace: namespace,
ManualInput: manualInput,
}
}
func saveConfig(cluster *Cluster) error {
path := filepath.Join(home, kubedConf)
var clusters []Cluster
oldConfBytes, err := ioutil.ReadFile(path)
if err == nil {
err = yaml.Unmarshal(oldConfBytes, &clusters)
if err != nil {
log.Error("Failed in parsing config file ", err)
clusters = nil
}
}
found := false
if clusters != nil {
for i, c := range clusters {
// Insert the recent config
if c.Name == cluster.Name {
clusters[i] = *cluster
found = true
}
}
if !found {
clusters = append(clusters, *cluster)
}
} else {
clusters = append(clusters, *cluster)
}
newConfBytes, err := yaml.Marshal(clusters)
if err != nil {
log.Warn("Failed in marshaling kubedconfig ", err)
return err
}
err = ioutil.WriteFile(path, newConfBytes, 0644)
if err != nil {
log.Warn("Failed in saving kubedconfig ", err)
return err
}
return nil
}