-
Notifications
You must be signed in to change notification settings - Fork 74
/
client.go
53 lines (44 loc) · 1.33 KB
/
client.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
package client
import (
"net/http"
"os"
"path/filepath"
"github.com/abahmed/kwatch/config"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
// Create returns kubernetes client after initializing it with in-cluster, or
// out of cluster config
func Create(appConfig *config.App) kubernetes.Interface {
// try to use in cluster config
clientConfig, err := rest.InClusterConfig()
if err != nil {
logrus.Warnf("cannot get kubernetes in cluster config: %v", err)
// try to use out of cluster config
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
home := homedir.HomeDir()
kubeconfigPath = filepath.Join(home, ".kube", "config")
}
clientConfig, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
logrus.Fatalf(
"cannot build kubernetes out of cluster config: %v",
err)
}
}
// avoid using default app proxy if it's set
if len(appConfig.ProxyURL) > 0 && clientConfig.Proxy == nil {
clientConfig.Proxy = http.ProxyURL(nil)
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
logrus.Fatalf("cannot create kubernetes client: %v", err)
}
logrus.Debugf("created kubernetes client successfully")
return clientset
}