-
Notifications
You must be signed in to change notification settings - Fork 292
/
config.go
87 lines (77 loc) · 2.57 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
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
package kubex
import (
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/spf13/pflag"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var kubeconfig string
// RegisterKubeconfigFlag registers `--kubeconfig` flag.
func RegisterKubeconfigFlag(flags *pflag.FlagSet) {
flags.StringVar(&kubeconfig, clientcmd.RecommendedConfigPathFlag, "", "Paths to a kubeconfig. Only required if out-of-cluster.")
}
type ConfigWithMeta struct {
K8s *rest.Config
CurrentContext string
}
// LoadRestConfigWithMetaInformation loads a REST Config. Config precedence:
//
// * --kubeconfig flag pointing at a file
//
// * KUBECONFIG environment variable pointing at a file
//
// * In-cluster config if running in cluster
//
// * $HOME/.kube/config if exists.
//
// code inspired by sigs.k8s.io/controller-runtime@v0.13.1/pkg/client/config/config.go
func LoadRestConfigWithMetaInformation() (*ConfigWithMeta, error) {
// 1. --kubeconfig flag
if kubeconfig != "" {
c := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}, nil)
return transform(c)
}
// 2. KUBECONFIG environment variable pointing at a file
kubeconfigPath := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(kubeconfigPath) == 0 {
if c, err := rest.InClusterConfig(); err == nil {
return &ConfigWithMeta{
K8s: c,
CurrentContext: "In cluster",
}, nil
}
}
// 3. In-cluster config if running in cluster
// 4. $HOME/.kube/config if exists
// 5. user.HomeDir/.kube/config if exists
//
// NOTE: For default config file locations, upstream only checks
// $HOME for the user's home directory, but we can also try
// os/user.HomeDir when $HOME is unset.
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if _, ok := os.LookupEnv("HOME"); !ok {
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not get current user: %w", err)
}
loadingRules.Precedence = append(loadingRules.Precedence, filepath.Join(u.HomeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName))
}
return transform(clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, nil))
}
func transform(c clientcmd.ClientConfig) (*ConfigWithMeta, error) {
rawConfig, err := c.RawConfig()
if err != nil {
return nil, fmt.Errorf("while getting raw config: %v", err)
}
clientConfig, err := c.ClientConfig()
if err != nil {
return nil, fmt.Errorf("while getting client config: %v", err)
}
return &ConfigWithMeta{
K8s: clientConfig,
CurrentContext: rawConfig.CurrentContext,
}, nil
}