forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
76 lines (67 loc) · 1.83 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
package kube
import (
"fmt"
"io/ioutil"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
const (
DefaultNamespace = "jx"
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 ""
}