-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
36 lines (30 loc) · 837 Bytes
/
namespace.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
func extractCurrentNamespace(byteArray []byte) (string, error) {
err := preflightAsset(&byteArray)
if err != nil {
return "", fmt.Errorf("kubeconfig failed preflight check")
}
var kubeconfig KubeconfigObject
if err = json.Unmarshal(byteArray, &kubeconfig); err != nil {
return "", fmt.Errorf("can't unmarshal data: %v", err)
}
currentContext := kubeconfig.CurrentContext
for _, context := range kubeconfig.Contexts {
if context.Name == currentContext {
return context.Context.Namespace, nil
}
}
return "default", nil
}
func extractCurrentNamespaceFromFile(path string) (string, error) {
byteArray, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("can't read %s: %v", path, err)
}
return extractCurrentNamespace(byteArray)
}