forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
121 lines (105 loc) · 3.04 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
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
}
// 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(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,
}
config.Clusters[clusterName] = cluster
config.AuthInfos[user] = authInfo
config.Contexts[ctxName] = ctx
config.CurrentContext = ctxName
return clientcmd.ModifyConfig(po, *config, false)
}