-
Notifications
You must be signed in to change notification settings - Fork 3
/
k8sclient.go
48 lines (42 loc) · 1.43 KB
/
k8sclient.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
package main
import (
"fmt"
"os"
"path/filepath"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type KubeConfig string
func GetKubeConfig() KubeConfig {
// Allow for kubie and other tools that set a different KUBECONFIG
c := os.Getenv("KUBECONFIG")
if c == "" {
homeDir, _ := os.UserHomeDir()
c = filepath.Join(homeDir, ".kube", "config")
}
return KubeConfig(c)
}
func NewRestConfig(c KubeConfig, cluster string) (*rest.Config, error) {
configLoadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: string(c)}
configOverrides := &clientcmd.ConfigOverrides{}
if cluster != "" {
configOverrides.CurrentContext = cluster
}
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(configLoadingRules, configOverrides).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to load config for cluster '%s': %w", cluster, err)
}
return config, nil
}
func GetNamespace(c KubeConfig, cluster string) (string, bool, error) {
configLoadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: string(c)}
configOverrides := &clientcmd.ConfigOverrides{}
if cluster != "" {
configOverrides.CurrentContext = cluster
}
ns, overriden, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(configLoadingRules, configOverrides).Namespace()
if err != nil {
return ns, overriden, fmt.Errorf("failed to get namespace for cluster '%s': %w", cluster, err)
}
return ns, overriden, nil
}