@@ -25,6 +25,7 @@ import (
25
25
26
26
"k8s.io/client-go/rest"
27
27
"k8s.io/client-go/tools/clientcmd"
28
+ clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
28
29
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
29
30
)
30
31
@@ -61,7 +62,27 @@ func init() {
61
62
//
62
63
// * $HOME/.kube/config if exists
63
64
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 )
65
86
if err != nil {
66
87
return nil , err
67
88
}
@@ -75,30 +96,42 @@ func GetConfig() (*rest.Config, error) {
75
96
}
76
97
77
98
// 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
+
79
101
// If a flag is specified with the config location, use that
80
102
if len (kubeconfig ) > 0 {
81
- return clientcmd . BuildConfigFromFlags (apiServerURL , kubeconfig )
103
+ return loadConfigWithContext (apiServerURL , kubeconfig , context )
82
104
}
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
84
106
if len (os .Getenv ("KUBECONFIG" )) > 0 {
85
- return clientcmd . BuildConfigFromFlags (apiServerURL , os .Getenv ("KUBECONFIG" ))
107
+ return loadConfigWithContext (apiServerURL , os .Getenv ("KUBECONFIG" ), context )
86
108
}
87
109
// If no explicit location, try the in-cluster config
88
110
if c , err := rest .InClusterConfig (); err == nil {
89
111
return c , nil
90
112
}
91
113
// If no in-cluster config, try the default location in the user's home directory
92
114
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 {
95
117
return c , nil
96
118
}
97
119
}
98
120
99
121
return nil , fmt .Errorf ("could not locate a kubeconfig" )
100
122
}
101
123
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
+
102
135
// GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver.
103
136
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
104
137
// in cluster and use the cluster provided kubeconfig.
0 commit comments