Skip to content

Commit

Permalink
fix namespace suggestion error on context switch
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec committed Nov 27, 2023
1 parent fcfff57 commit 0885704
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
19 changes: 13 additions & 6 deletions internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type App struct {
showHeader bool
showLogo bool
showCrumbs bool
namespaces []string
}

// NewApp returns a K9s app instance.
Expand Down Expand Up @@ -120,6 +121,11 @@ func (a *App) Init(version string, rate int) error {
a.layout(ctx)
a.initSignals()

a.namespaces, err = a.namespaceNames()
if err != nil {
log.Error().Err(err).Msg("failed to list namespaces")
}

return nil
}

Expand Down Expand Up @@ -151,11 +157,6 @@ func (a *App) initSignals() {
}

func (a *App) suggestCommand() model.SuggestionFunc {
namespaceNames, err := a.namespaceNames()
if err != nil {
log.Error().Err(err).Msg("failed to list namespaces")
}

contextNames, err := a.contextNames()
if err != nil {
log.Error().Err(err).Msg("failed to list contexts")
Expand All @@ -176,7 +177,7 @@ func (a *App) suggestCommand() model.SuggestionFunc {
}
}

entries = append(entries, suggestSubCommand(s, namespaceNames, contextNames)...)
entries = append(entries, suggestSubCommand(s, a.namespaces, contextNames)...)
if len(entries) == 0 {
return nil
}
Expand Down Expand Up @@ -473,6 +474,12 @@ func (a *App) switchContext(name string) error {
a.clusterModel.Reset(a.factory)
}

namespaceNames, err := a.namespaceNames()
if err != nil {
log.Error().Err(err).Msg("failed to list namespaces")
}
a.namespaces = namespaceNames

return nil
}

Expand Down
38 changes: 38 additions & 0 deletions internal/view/app_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s

package view

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_suggestSubCommand(t *testing.T) {
namespaceNames := []string{"kube-system", "kube-public", "default", "nginx-ingress"}
contextNames := []string{"develop", "test", "pre", "prod"}

tests := []struct {
Command string
Suggestions []string
}{
{Command: "q", Suggestions: nil},
{Command: "xray dp", Suggestions: nil},
{Command: "help k", Suggestions: nil},
{Command: "ctx p", Suggestions: []string{"re", "rod"}},
{Command: "ctx p", Suggestions: []string{"re", "rod"}},
{Command: "ctx pr", Suggestions: []string{"e", "od"}},
{Command: "context d", Suggestions: []string{"evelop"}},
{Command: "contexts t", Suggestions: []string{"est"}},
{Command: "po ", Suggestions: nil},
{Command: "po x", Suggestions: nil},
{Command: "po k", Suggestions: []string{"ube-system", "ube-public"}},
{Command: "po kube-", Suggestions: []string{"system", "public"}},
}

for _, tt := range tests {
got := suggestSubCommand(tt.Command, namespaceNames, contextNames)
assert.Equal(t, tt.Suggestions, got)
}
}
33 changes: 3 additions & 30 deletions internal/view/app_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s

package view
package view_test

import (
"testing"

"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/view"
"github.com/stretchr/testify/assert"
)

func TestAppNew(t *testing.T) {
a := NewApp(config.NewConfig(ks{}))
a := view.NewApp(config.NewConfig(ks{}))
_ = a.Init("blee", 10)

assert.Equal(t, 11, len(a.GetActions()))
}

func Test_suggestSubCommand(t *testing.T) {
namespaceNames := []string{"kube-system", "kube-public", "default", "nginx-ingress"}
contextNames := []string{"develop", "test", "pre", "prod"}

tests := []struct {
Command string
Suggestions []string
}{
{Command: "q", Suggestions: nil},
{Command: "xray dp", Suggestions: nil},
{Command: "help k", Suggestions: nil},
{Command: "ctx p", Suggestions: []string{"re", "rod"}},
{Command: "ctx p", Suggestions: []string{"re", "rod"}},
{Command: "ctx pr", Suggestions: []string{"e", "od"}},
{Command: "context d", Suggestions: []string{"evelop"}},
{Command: "contexts t", Suggestions: []string{"est"}},
{Command: "po ", Suggestions: nil},
{Command: "po x", Suggestions: nil},
{Command: "po k", Suggestions: []string{"ube-system", "ube-public"}},
{Command: "po kube-", Suggestions: []string{"system", "public"}},
}

for _, tt := range tests {
got := suggestSubCommand(tt.Command, namespaceNames, contextNames)
assert.Equal(t, tt.Suggestions, got)
}
}

0 comments on commit 0885704

Please sign in to comment.