/
config.go
42 lines (36 loc) · 1.07 KB
/
config.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
package config
import (
"fmt"
"os"
"os/user"
"path/filepath"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
DefaultUpdateInterval = 30
DefaultProfilePath = "/etc/linuxptp"
)
func GetKubeConfig() (*rest.Config, error) {
configFromFlags := func(kubeConfig string) (*rest.Config, error) {
if _, err := os.Stat(kubeConfig); err != nil {
return nil, fmt.Errorf("Cannot stat kubeconfig '%s'", kubeConfig)
}
return clientcmd.BuildConfigFromFlags("", kubeConfig)
}
// If an env variable is specified with the config location, use that
kubeConfig := os.Getenv("KUBECONFIG")
if len(kubeConfig) > 0 {
return configFromFlags(kubeConfig)
}
// If no explicit location, try the in-cluster config
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}
// If no in-cluster config, try the default location in the user's home directory
if usr, err := user.Current(); err == nil {
kubeConfig := filepath.Join(usr.HomeDir, ".kube", "config")
return configFromFlags(kubeConfig)
}
return nil, fmt.Errorf("Could not locate a kubeconfig")
}