This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
getter.go
112 lines (95 loc) · 3.08 KB
/
getter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Package kubeconfig gets a valid kube config
package kubeconfig
import (
"fmt"
"os"
"os/user"
"path/filepath"
"go.uber.org/zap"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// Getter is the interface that wraps the Get method that returns the Kubernetes configuration used
// to communicate with it using its API.
type Getter interface {
Get(configPath string) (*rest.Config, error)
}
// NewGetter constructs a default getter that satisfies the Getter interface.
func NewGetter(log *zap.SugaredLogger) Getter {
return &getter{
log: log,
inClusterConfig: rest.InClusterConfig,
stat: os.Stat,
restConfigFromKubeConfig: clientcmd.NewNonInteractiveDeferredLoadingClientConfig,
lookupEnv: os.LookupEnv,
currentUser: user.Current,
defaultRESTConfig: clientcmd.DefaultClientConfig.ClientConfig,
}
}
type getter struct {
log *zap.SugaredLogger
inClusterConfig func() (*rest.Config, error)
stat func(name string) (os.FileInfo, error)
restConfigFromKubeConfig func(loader clientcmd.ClientConfigLoader, overrides *clientcmd.ConfigOverrides) clientcmd.ClientConfig
lookupEnv func(key string) (string, bool)
currentUser func() (*user.User, error)
defaultRESTConfig func() (*rest.Config, error)
}
func (g *getter) Get(configPath string) (*rest.Config, error) {
if configPath == "" {
// If no explicit location, try the in-cluster config.
_, okHost := g.lookupEnv("KUBERNETES_SERVICE_HOST")
_, okPort := g.lookupEnv("KUBERNETES_SERVICE_PORT")
if okHost && okPort {
c, err := g.inClusterConfig()
if err == nil {
g.log.Info("Using in-cluster kube config")
return c, nil
} else if !os.IsNotExist(err) {
return nil, &getConfigError{err}
}
}
// If no in-cluster config, set the config path to the user's ~/.kube directory.
usr, err := g.currentUser()
if err != nil {
return nil, &getConfigError{err}
}
homeFile := filepath.Join(usr.HomeDir, ".kube", "config")
_, err = g.stat(homeFile)
if err != nil {
if !os.IsNotExist(err) {
return nil, &getConfigError{err}
}
// If neither the custom config path, nor the user's ~/.kube directory config path exist, use a
// default config.
c, err := g.defaultRESTConfig()
if err != nil {
return nil, &getConfigError{err}
}
g.log.Infof("%s does not exist, using default kube config", configPath)
return c, nil
}
configPath = homeFile
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if len(configPath) > 0 {
paths := filepath.SplitList(configPath)
if len(paths) == 1 {
loadingRules.ExplicitPath = paths[0]
} else {
loadingRules.Precedence = paths
}
}
c, err := g.restConfigFromKubeConfig(loadingRules, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, &getConfigError{err}
}
g.log.Infof("Using kube server '%s'", c.Host)
return c, nil
}
type getConfigError struct {
err error
}
func (e *getConfigError) Error() string {
return fmt.Sprintf("failed to get kube config: %v", e.err)
}