-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubeconfig.go
94 lines (80 loc) · 1.83 KB
/
kubeconfig.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
package main
import (
"io/ioutil"
"os"
)
type kubeconfig struct {
Kind string `json:"kind"`
Clusters []kubeCluster `json:"clusters"`
Contexts []kubeContext `json:"contexts"`
CurrentContext string `json:"current-context"`
}
type kubeCluster struct {
Name string `json:"name"`
Context kubeClusterData `json:"cluster"`
}
type kubeClusterData struct {
Server string `yaml:"server"`
}
type kubeContext struct {
Name string `json:"name"`
Context kubeContextData `json:"context"`
}
type kubeContextData struct {
Cluster string `json:"cluster"`
User string `json:"user"`
}
func validateKubeconfig(validate bool) bool {
fn := os.Getenv("KUBECONFIG")
if fn == "" {
log.Warning("Env 'KUBECONFIG' is missing")
return false
}
buf, err := ioutil.ReadFile(fn)
if err != nil {
if validate {
panic(err)
}
return false
}
var kc kubeconfig
var o interface{}
//yaml_mapstr.Unmarshal(buf, &o)
err = yaml_Unmarshal(buf, &o)
if err != nil {
log.Error("Unable to read kubeconfig: %s", err.Error())
return false
}
buf, err = json_Marshal(o)
if err != nil {
log.Error("Unable to marshal kubeconfig: %s", err.Error())
return false
}
err = json_Unmarshal(buf, &kc)
if err != nil {
log.Error("Unable to unmarshal kubeconfig: %s", err.Error())
return false
}
var ctx *kubeContext
for _, c := range kc.Contexts {
if c.Name == kc.CurrentContext {
ctx = &c
}
}
if ctx == nil {
log.Error("Current-context doesn't exist in the KUBECONFIG")
return false
}
var cluster *kubeCluster
for _, cl := range kc.Clusters {
if cl.Name == ctx.Context.Cluster {
cluster = &cl
}
}
if cluster == nil {
log.Error("Can't find the clusterdata for the current context")
return false
}
log.Noticef("Current Kubeconfig: %s", cluster.Context.Server)
return true
}