-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubernetes-context.go
More file actions
60 lines (49 loc) · 1.02 KB
/
Copy pathkubernetes-context.go
File metadata and controls
60 lines (49 loc) · 1.02 KB
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v3"
)
type Context struct {
Cluster string
Namespace string
User string
}
type ContextAndName struct {
Context Context
Name string
}
type Config struct {
Contexts []ContextAndName
CurrentContext string `yaml:"current-context"`
}
func main() {
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig != "" {
log.Fatal("$KUBECONFIG is not supported")
}
data, err := ioutil.ReadFile(fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")))
if err != nil {
log.Fatalf("Cannot read config: %v", err)
}
config := Config{}
if err := yaml.Unmarshal([]byte(data), &config); err != nil {
log.Fatalf("Cannot parse config: %v", err)
}
if config.CurrentContext == "" {
return
}
namespace := ""
for _, context := range config.Contexts {
if context.Name == config.CurrentContext {
namespace = context.Context.Namespace
break
}
}
fmt.Printf("%v", config.CurrentContext)
if namespace != "" {
fmt.Printf(" (%v)", namespace)
}
}