Skip to content

Commit 3b365c5

Browse files
committed
add GetConfigWithContext to retrieve a config with a specific context
1 parent 6548892 commit 3b365c5

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/config/config.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"k8s.io/client-go/rest"
2727
"k8s.io/client-go/tools/clientcmd"
28+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2829
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
2930
)
3031

@@ -61,7 +62,27 @@ func init() {
6162
//
6263
// * $HOME/.kube/config if exists
6364
func GetConfig() (*rest.Config, error) {
64-
cfg, err := loadConfig()
65+
return GetConfigWithContext("")
66+
}
67+
68+
// GetConfigWithContext creates a *rest.Config for talking to a Kubernetes API server with a specific context.
69+
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
70+
// in cluster and use the cluster provided kubeconfig.
71+
//
72+
// It also applies saner defaults for QPS and burst based on the Kubernetes
73+
// controller manager defaults (20 QPS, 30 burst)
74+
//
75+
// Config precedence
76+
//
77+
// * --kubeconfig flag pointing at a file
78+
//
79+
// * KUBECONFIG environment variable pointing at a file
80+
//
81+
// * In-cluster config if running in cluster
82+
//
83+
// * $HOME/.kube/config if exists
84+
func GetConfigWithContext(context string) (*rest.Config, error) {
85+
cfg, err := loadConfig(context)
6586
if err != nil {
6687
return nil, err
6788
}
@@ -75,30 +96,42 @@ func GetConfig() (*rest.Config, error) {
7596
}
7697

7798
// loadConfig loads a REST Config as per the rules specified in GetConfig
78-
func loadConfig() (*rest.Config, error) {
99+
func loadConfig(context string) (*rest.Config, error) {
100+
79101
// If a flag is specified with the config location, use that
80102
if len(kubeconfig) > 0 {
81-
return clientcmd.BuildConfigFromFlags(apiServerURL, kubeconfig)
103+
return loadConfigWithContext(apiServerURL, kubeconfig, context)
82104
}
83-
// If an env variable is specified with the config locaiton, use that
105+
// If an env variable is specified with the config location, use that
84106
if len(os.Getenv("KUBECONFIG")) > 0 {
85-
return clientcmd.BuildConfigFromFlags(apiServerURL, os.Getenv("KUBECONFIG"))
107+
return loadConfigWithContext(apiServerURL, os.Getenv("KUBECONFIG"), context)
86108
}
87109
// If no explicit location, try the in-cluster config
88110
if c, err := rest.InClusterConfig(); err == nil {
89111
return c, nil
90112
}
91113
// If no in-cluster config, try the default location in the user's home directory
92114
if usr, err := user.Current(); err == nil {
93-
if c, err := clientcmd.BuildConfigFromFlags(
94-
"", filepath.Join(usr.HomeDir, ".kube", "config")); err == nil {
115+
if c, err := loadConfigWithContext(apiServerURL, filepath.Join(usr.HomeDir, ".kube", "config"),
116+
context); err == nil {
95117
return c, nil
96118
}
97119
}
98120

99121
return nil, fmt.Errorf("could not locate a kubeconfig")
100122
}
101123

124+
func loadConfigWithContext(apiServerURL, kubeconfig, context string) (*rest.Config, error) {
125+
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
126+
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
127+
&clientcmd.ConfigOverrides{
128+
ClusterInfo: clientcmdapi.Cluster{
129+
Server: apiServerURL,
130+
},
131+
CurrentContext: context,
132+
}).ClientConfig()
133+
}
134+
102135
// GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver.
103136
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
104137
// in cluster and use the cluster provided kubeconfig.

0 commit comments

Comments
 (0)