-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (42 loc) · 814 Bytes
/
Copy pathmain.go
File metadata and controls
47 lines (42 loc) · 814 Bytes
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
package main
import (
"encoding/json"
"fmt"
"log"
"os/exec"
)
type context struct {
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
}
type contextinfo struct {
Name string `json:"name"`
Context context `json:"context"`
}
type config struct {
Clusters []contextinfo `json:"contexts"`
Current string `json:"current-context"`
}
func main() {
cmd := exec.Command("kubectl", "config", "view", "-o", "json")
out, err := cmd.Output()
cfg := config{}
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(out, &cfg)
if err != nil {
log.Fatal(err)
}
context := cfg.Current
ns := "default"
for _, x := range cfg.Clusters {
if x.Name == context {
ns = x.Context.Namespace
if ns == "" {
ns = "default"
}
}
}
fmt.Printf("%s/%s", context, ns)
}