Skip to content

Commit c3b44c3

Browse files
committed
Add possibility to execute k9s inside a Kubernetes POD with working incluster config
1 parent 43408da commit c3b44c3

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

internal/client/config.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"k8s.io/cli-runtime/pkg/genericclioptions"
1616
restclient "k8s.io/client-go/rest"
17-
clientcmd "k8s.io/client-go/tools/clientcmd"
17+
"k8s.io/client-go/tools/clientcmd"
1818
"k8s.io/client-go/tools/clientcmd/api"
1919
)
2020

@@ -23,6 +23,8 @@ const (
2323

2424
// UsePersistentConfig caches client config to avoid reloads.
2525
UsePersistentConfig = true
26+
27+
incluster = "incluster"
2628
)
2729

2830
// Config tracks a kubernetes configuration.
@@ -87,6 +89,10 @@ func (c *Config) SwitchContext(name string) error {
8789
if err != nil {
8890
return fmt.Errorf("context %q does not exist", name)
8991
}
92+
if name == incluster && ct.LocationOfOrigin == incluster {
93+
return nil
94+
}
95+
9096
// !!BOZO!! Do you need to reset the flags?
9197
flags := genericclioptions.NewConfigFlags(UsePersistentConfig)
9298
flags.Context, flags.ClusterName = &name, &ct.Cluster
@@ -131,6 +137,9 @@ func (c *Config) CurrentClusterName() (string, error) {
131137

132138
ct, ok := cfg.Contexts[cfg.CurrentContext]
133139
if !ok {
140+
if c.isIncluster(cfg) {
141+
return incluster, nil
142+
}
134143
return "", fmt.Errorf("invalid current context specified: %q", cfg.CurrentContext)
135144
}
136145
if isSet(c.flags.Context) {
@@ -154,6 +163,10 @@ func (c *Config) CurrentContextName() (string, error) {
154163
return "", fmt.Errorf("fail to load rawConfig: %w", err)
155164
}
156165

166+
if c.isIncluster(cfg) {
167+
return incluster, nil
168+
}
169+
157170
return cfg.CurrentContext, nil
158171
}
159172

@@ -162,6 +175,7 @@ func (c *Config) CurrentContextNamespace() (string, error) {
162175
if err != nil {
163176
return "", err
164177
}
178+
165179
context, err := c.GetContext(name)
166180
if err != nil {
167181
return "", err
@@ -185,10 +199,16 @@ func (c *Config) GetContext(n string) (*api.Context, error) {
185199
if err != nil {
186200
return nil, err
187201
}
202+
188203
if c, ok := cfg.Contexts[n]; ok {
189204
return c, nil
190205
}
191206

207+
if n == incluster {
208+
nc := c.newInclusterContext()
209+
return nc, nil
210+
}
211+
192212
return nil, fmt.Errorf("getcontext - invalid context specified: %q", n)
193213
}
194214

@@ -208,6 +228,12 @@ func (c *Config) Contexts() (map[string]*api.Context, error) {
208228
return nil, err
209229
}
210230

231+
if len(cfg.Contexts) == 0 && c.isIncluster(cfg) {
232+
return map[string]*api.Context{
233+
incluster: c.newInclusterContext(),
234+
}, nil
235+
}
236+
211237
return cfg.Contexts, nil
212238
}
213239

@@ -320,6 +346,9 @@ func (c *Config) CurrentUserName() (string, error) {
320346
if ctx, ok := cfg.Contexts[current]; ok {
321347
return ctx.AuthInfo, nil
322348
}
349+
if c.isIncluster(cfg) {
350+
return incluster, nil
351+
}
323352

324353
return "", errors.New("unable to locate current user")
325354
}
@@ -347,6 +376,30 @@ func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error) {
347376
return c.clientConfig().ConfigAccess(), nil
348377
}
349378

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+
350403
// ----------------------------------------------------------------------------
351404
// Helpers...
352405

0 commit comments

Comments
 (0)