-
Notifications
You must be signed in to change notification settings - Fork 3
/
kubeconfig.go
56 lines (46 loc) · 966 Bytes
/
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
package main
import (
"os"
"github.com/goccy/go-yaml"
)
type Kubeconfig struct {
Contexts []KubeContexts `yaml:"contexts"`
CurrentContext string `yaml:"current-context"`
}
type KubeContexts struct {
Context KubeContext `yaml:"context"`
Name string `yaml:"name"`
}
type KubeContext struct {
Namespace string `yaml:"namespace"`
}
func loadKubeconfig(path string) (k Kubeconfig, err error) {
f, err := os.Open(path)
if err != nil {
return k, err
}
defer f.Close()
d := yaml.NewDecoder(f)
err = d.Decode(&k)
return k, err
}
func (k Kubeconfig) CurrentNamespace() string {
kc, ok := k.currentContext()
if !ok {
return "default"
}
if kc.Namespace == "" {
return "default"
}
return kc.Namespace
}
func (k Kubeconfig) currentContext() (KubeContext, bool) {
for _, kc := range k.Contexts {
if kc.Name == k.CurrentContext {
return kc.Context, true
}
}
return KubeContext{
Namespace: "default",
}, false
}