-
Notifications
You must be signed in to change notification settings - Fork 6
/
system.go
38 lines (33 loc) · 883 Bytes
/
system.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
package kubeclient
import (
"os"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// Return auto-detected system/user Kubernetes configuration,
// either from in-cluster autoconfiguration or a $KUBECONFIG file.
func SystemConfig() (*rest.Config, error) {
cfg, err := rest.InClusterConfig()
if err == nil {
log.Tracef("Running inside Kubernetes, using in-cluster configuration")
return cfg, nil
}
cf := kubeConfigPath()
log.Tracef("Not running inside Kubernetes, using configuration file %s", cf)
return clientcmd.BuildConfigFromFlags("", cf)
}
func DefaultClient() (Interface, error) {
config, err := SystemConfig()
if err != nil {
return nil, err
}
return New(config)
}
func kubeConfigPath() string {
env, found := os.LookupEnv("KUBECONFIG")
if !found {
return clientcmd.RecommendedHomeFile
}
return env
}