Skip to content

Commit

Permalink
Respect existing behavior
Browse files Browse the repository at this point in the history
and set k9s's active ns if no ns specified in the context
  • Loading branch information
mikutas committed Aug 29, 2023
1 parent f6b6071 commit 26792e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 19 additions & 1 deletion internal/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ func (c *Config) CurrentContextName() (string, error) {
return cfg.CurrentContext, nil
}

func (c *Config) CurrentContextNamespace() (string, error) {
name, err := c.CurrentContextName()
if err != nil {
return "", err
}
context, err := c.GetContext(name)
if err != nil {
return "", err
}

return context.Namespace, nil
}

// GetContext fetch a given context or error if it does not exists.
func (c *Config) GetContext(n string) (*clientcmdapi.Context, error) {
cfg, err := c.RawConfig()
Expand Down Expand Up @@ -281,7 +294,12 @@ func (c *Config) CurrentUserName() (string, error) {

// CurrentNamespaceName retrieves the active namespace.
func (c *Config) CurrentNamespaceName() (string, error) {
ns, _, err := c.clientConfig().Namespace()
ns, err := c.CurrentContextNamespace()
if ns == "" && err == nil {
return "", errors.New("No namespace specified in context")
}

ns, _, err = c.clientConfig().Namespace()

return ns, err
}
Expand Down
6 changes: 5 additions & 1 deletion internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ func (a *App) switchContext(name string) error {
a.Halt()
defer a.Resume()
{
ns := a.Config.ActiveNamespace()
ns, err := a.Conn().Config().CurrentNamespaceName()
if err != nil {
log.Warn().Msg("No namespace specified in context. Using K9s config")
ns = a.Config.ActiveNamespace()
}
a.initFactory(ns)

if e := a.command.Reset(true); e != nil {
Expand Down

0 comments on commit 26792e5

Please sign in to comment.