@@ -14,7 +14,7 @@ import (
14
14
15
15
"k8s.io/cli-runtime/pkg/genericclioptions"
16
16
restclient "k8s.io/client-go/rest"
17
- clientcmd "k8s.io/client-go/tools/clientcmd"
17
+ "k8s.io/client-go/tools/clientcmd"
18
18
"k8s.io/client-go/tools/clientcmd/api"
19
19
)
20
20
@@ -23,6 +23,8 @@ const (
23
23
24
24
// UsePersistentConfig caches client config to avoid reloads.
25
25
UsePersistentConfig = true
26
+
27
+ incluster = "incluster"
26
28
)
27
29
28
30
// Config tracks a kubernetes configuration.
@@ -87,6 +89,10 @@ func (c *Config) SwitchContext(name string) error {
87
89
if err != nil {
88
90
return fmt .Errorf ("context %q does not exist" , name )
89
91
}
92
+ if name == incluster && ct .LocationOfOrigin == incluster {
93
+ return nil
94
+ }
95
+
90
96
// !!BOZO!! Do you need to reset the flags?
91
97
flags := genericclioptions .NewConfigFlags (UsePersistentConfig )
92
98
flags .Context , flags .ClusterName = & name , & ct .Cluster
@@ -131,6 +137,9 @@ func (c *Config) CurrentClusterName() (string, error) {
131
137
132
138
ct , ok := cfg .Contexts [cfg .CurrentContext ]
133
139
if ! ok {
140
+ if c .isIncluster (cfg ) {
141
+ return incluster , nil
142
+ }
134
143
return "" , fmt .Errorf ("invalid current context specified: %q" , cfg .CurrentContext )
135
144
}
136
145
if isSet (c .flags .Context ) {
@@ -154,6 +163,10 @@ func (c *Config) CurrentContextName() (string, error) {
154
163
return "" , fmt .Errorf ("fail to load rawConfig: %w" , err )
155
164
}
156
165
166
+ if c .isIncluster (cfg ) {
167
+ return incluster , nil
168
+ }
169
+
157
170
return cfg .CurrentContext , nil
158
171
}
159
172
@@ -162,6 +175,7 @@ func (c *Config) CurrentContextNamespace() (string, error) {
162
175
if err != nil {
163
176
return "" , err
164
177
}
178
+
165
179
context , err := c .GetContext (name )
166
180
if err != nil {
167
181
return "" , err
@@ -185,10 +199,16 @@ func (c *Config) GetContext(n string) (*api.Context, error) {
185
199
if err != nil {
186
200
return nil , err
187
201
}
202
+
188
203
if c , ok := cfg .Contexts [n ]; ok {
189
204
return c , nil
190
205
}
191
206
207
+ if n == incluster {
208
+ nc := c .newInclusterContext ()
209
+ return nc , nil
210
+ }
211
+
192
212
return nil , fmt .Errorf ("getcontext - invalid context specified: %q" , n )
193
213
}
194
214
@@ -208,6 +228,12 @@ func (c *Config) Contexts() (map[string]*api.Context, error) {
208
228
return nil , err
209
229
}
210
230
231
+ if len (cfg .Contexts ) == 0 && c .isIncluster (cfg ) {
232
+ return map [string ]* api.Context {
233
+ incluster : c .newInclusterContext (),
234
+ }, nil
235
+ }
236
+
211
237
return cfg .Contexts , nil
212
238
}
213
239
@@ -320,6 +346,9 @@ func (c *Config) CurrentUserName() (string, error) {
320
346
if ctx , ok := cfg .Contexts [current ]; ok {
321
347
return ctx .AuthInfo , nil
322
348
}
349
+ if c .isIncluster (cfg ) {
350
+ return incluster , nil
351
+ }
323
352
324
353
return "" , errors .New ("unable to locate current user" )
325
354
}
@@ -347,6 +376,30 @@ func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error) {
347
376
return c .clientConfig ().ConfigAccess (), nil
348
377
}
349
378
379
+ func (c * Config ) newInclusterContext () * api.Context {
380
+ ns , _ , _ := c .clientConfig ().Namespace ()
381
+ if ns == "" {
382
+ ns = "default"
383
+ }
384
+ nc := api .NewContext ()
385
+ nc .LocationOfOrigin = incluster
386
+ nc .Cluster = incluster
387
+ nc .Namespace = ns
388
+ nc .AuthInfo = incluster
389
+ return nc
390
+ }
391
+
392
+ func (c * Config ) isIncluster (cfg api.Config ) bool {
393
+ if (cfg .CurrentContext == "" || cfg .CurrentContext == incluster ) &&
394
+ len (cfg .Contexts ) == 0 &&
395
+ c .flags .KubeConfig != nil && * c .flags .KubeConfig == "" &&
396
+ c .flags .ClusterName != nil && * c .flags .ClusterName == "" &&
397
+ c .flags .APIServer != nil && * c .flags .APIServer == "" {
398
+ return true
399
+ }
400
+ return false
401
+ }
402
+
350
403
// ----------------------------------------------------------------------------
351
404
// Helpers...
352
405
0 commit comments