forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
163 lines (141 loc) · 4.17 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package kube
import (
"fmt"
"io/ioutil"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
const (
// DefaultNamespace the standard namespace for Jenkins X
DefaultNamespace = "jx"
// PodNamespaceFile the file path and name for pod namespace
PodNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
// LoadConfig loads the Kubernetes configuration
func LoadConfig() (*api.Config, *clientcmd.PathOptions, error) {
po := clientcmd.NewDefaultPathOptions()
if po == nil {
return nil, po, fmt.Errorf("Could not find any default path options for the kubeconfig file usually found at ~/.kube/config")
}
config, err := po.GetStartingConfig()
if err != nil {
return nil, po, fmt.Errorf("Could not load the kube config file %s due to %s", po.GetDefaultFilename(), err)
}
return config, po, err
}
// CurrentNamespace returns the current namespace in the context
func CurrentNamespace(config *api.Config) string {
ctx := CurrentContext(config)
if ctx != nil {
n := ctx.Namespace
if n != "" {
return n
}
}
// if we are in a pod lets try load the pod namespace file
data, err := ioutil.ReadFile(PodNamespaceFile)
if err == nil {
n := string(data)
if n != "" {
return n
}
}
return "default"
}
// CurrentContext returns the current context
func CurrentContext(config *api.Config) *api.Context {
if config != nil {
name := config.CurrentContext
if name != "" && config.Contexts != nil {
return config.Contexts[name]
}
}
return nil
}
// CurrentCluster returns the current cluster
func CurrentCluster(config *api.Config) (string, *api.Cluster) {
if config != nil {
context := CurrentContext(config)
if context != nil && config.Clusters != nil {
return context.Cluster, config.Clusters[context.Cluster]
}
}
return "", nil
}
// CurrentServer returns the current context's server
func CurrentServer(config *api.Config) string {
context := CurrentContext(config)
return Server(config, context)
}
// Server returns the server of the given context
func Server(config *api.Config, context *api.Context) string {
if context != nil && config != nil && config.Clusters != nil {
cluster := config.Clusters[context.Cluster]
if cluster != nil {
return cluster.Server
}
}
return ""
}
// CertificateAuthorityData returns the certificate authority data for the given context
func CertificateAuthorityData(config *api.Config, context *api.Context) []byte {
if context != nil && config != nil && config.Clusters != nil {
cluster := config.Clusters[context.Cluster]
if cluster != nil {
return cluster.CertificateAuthorityData
}
}
return []byte{}
}
// UpdateConfig defines new config entries for jx
func UpdateConfig(namespace string, server string, caData string, user string, token string) error {
config, po, err := LoadConfig()
if err != nil {
return errors.Wrap(err, "loading existing config")
}
clusterName := "jx-cluster"
cluster := &api.Cluster{
Server: server,
CertificateAuthorityData: []byte(caData),
}
authInfo := &api.AuthInfo{
Token: token,
}
ctxName := fmt.Sprintf("jx-cluster-%s-ctx", user)
ctx := &api.Context{
Cluster: clusterName,
AuthInfo: user,
Namespace: namespace,
}
config.Clusters[clusterName] = cluster
config.AuthInfos[user] = authInfo
config.Contexts[ctxName] = ctx
config.CurrentContext = ctxName
return clientcmd.ModifyConfig(po, *config, false)
}
// AddUserToConfig adds the given user to the config
func AddUserToConfig(user string, token string, config *api.Config) (*api.Config, error) {
currentCluserName, currentCluster := CurrentCluster(config)
if currentCluster == nil || currentCluserName == "" {
return config, errors.New("no cluster found in config")
}
currentCtx := CurrentContext(config)
currentNamespace := DefaultNamespace
if currentCtx != nil {
currentNamespace = currentCtx.Namespace
}
ctx := &api.Context{
Cluster: currentCluserName,
AuthInfo: user,
Namespace: currentNamespace,
}
authInfo := &api.AuthInfo{
Token: token,
}
config.AuthInfos[user] = authInfo
ctxName := fmt.Sprintf("jx-%s-%s-ctx", currentCluserName, user)
config.Contexts[ctxName] = ctx
config.CurrentContext = ctxName
return config, nil
}