-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
kube.go
60 lines (50 loc) · 1.34 KB
/
kube.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
57
58
59
60
package util
import (
"log"
"os"
"os/user"
"path"
"k8s.io/client-go/rest"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
)
type Kube struct {
Namespace string
Context string
}
func getDefaultKubeConfigPath(homeDir string) string {
return path.Join(homeDir, ".kube", "config")
}
func getKubeConfigPath() string {
var kubeConfigPath string
currentUser, _ := user.Current()
if currentUser != nil {
kubeConfigPath = os.Getenv("KUBECONFIG")
if kubeConfigPath == "" {
kubeConfigPath = getDefaultKubeConfigPath(currentUser.HomeDir)
}
}
return kubeConfigPath
}
func ConnectToK8sArgoClientSet() *appclientset.Clientset {
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfigPath())
if err != nil {
log.Panicln("failed to create Argocd K8s config")
}
return appclientset.NewForConfigOrDie(config)
}
func ConnectToK8sConfig() *rest.Config {
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfigPath())
if err != nil {
log.Panicln("failed to create K8s config")
}
return config
}
func ConnectToK8sClientSet() *kubernetes.Clientset {
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfigPath())
if err != nil {
log.Panicln("failed to create K8s config")
}
return kubernetes.NewForConfigOrDie(config)
}