-
Notifications
You must be signed in to change notification settings - Fork 8
/
loader.go
52 lines (44 loc) · 1.95 KB
/
loader.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
package kubeconfig
import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// Fetch ClientConfig. If kubeConfigPath is not specified, retrieve the kubeconfig from environment in which this is invoked.
// Override the API Server URL and current context if specified.
func GetClientConfigWithContext(kubeConfigPath, kubeContext, apiServerUrl string) (clientcmd.ClientConfig, error) {
// default loading rules checks for KUBECONFIG env var
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// also check recommended default kubeconfig file locations
loadingRules.Precedence = append(loadingRules.Precedence, clientcmd.RecommendedHomeFile)
// explicit path overrides all loading rules, will error if not found
if kubeConfigPath != "" {
loadingRules.ExplicitPath = kubeConfigPath
}
overrides := &clientcmd.ConfigOverrides{}
if kubeContext != "" {
overrides.CurrentContext = kubeContext
}
if apiServerUrl != "" {
overrides.ClusterInfo = clientcmdapi.Cluster{
Server: apiServerUrl,
}
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides), nil
}
// Fetch rest.Config for environment in which this is invoked, override the API Server URL and current context if specified.
func GetRestConfigWithContext(kubeConfigPath, kubeContext, apiServerUrl string) (*rest.Config, error) {
clientConfig, err := GetClientConfigWithContext(kubeConfigPath, kubeContext, apiServerUrl)
if err != nil {
return nil, err
}
return clientConfig.ClientConfig()
}
// Fetch raw Config for environment in which this is invoked, override the API Server URL and current context if specified.
func GetRawConfigWithContext(kubeConfigPath, kubeContext, apiServerUrl string) (clientcmdapi.Config, error) {
clientConfig, err := GetClientConfigWithContext(kubeConfigPath, kubeContext, apiServerUrl)
if err != nil {
return clientcmdapi.Config{}, err
}
return clientConfig.RawConfig()
}