From 6bd186c3779e9b10c99112972c6c851f1372dd8b Mon Sep 17 00:00:00 2001 From: derailed Date: Tue, 5 May 2020 18:28:54 -0600 Subject: [PATCH] fix issues #692 #699 --- .golangci.yml | 2 +- internal/client/client.go | 2 -- internal/config/config.go | 7 +++++++ internal/config/plugin.go | 6 +++--- internal/render/header.go | 2 +- internal/render/rob.go | 2 +- internal/ui/select_table.go | 2 +- internal/ui/table.go | 11 +++++------ internal/ui/table_helper.go | 2 +- internal/view/app.go | 24 ++++++++++-------------- internal/view/helpers.go | 2 +- internal/view/node.go | 4 ++-- internal/view/pf_dialog.go | 2 +- internal/view/pf_dialog_test.go | 6 ++++++ internal/xray/svc.go | 2 +- internal/xray/tree_node.go | 2 +- 16 files changed, 42 insertions(+), 36 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 33fcb57f..0b122496 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -232,9 +232,9 @@ linters: - govet - funlen - gocyclo - disable: - maligned - prealloc + disable: - gosec disable-all: false presets: diff --git a/internal/client/client.go b/internal/client/client.go index 799bd521..55978be4 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -173,14 +173,12 @@ func (a *APIClient) ValidNamespaces() ([]v1.Namespace, error) { return nss, nil } } - log.Debug().Msgf(">>>>> Loading all namespaces") ctx, cancel := context.WithTimeout(context.Background(), CallTimeout) defer cancel() nn, err := a.DialOrDie().CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) if err != nil { return nil, err } - a.cache.Add("validNamespaces", nn.Items, cacheExpiry) return nn.Items, nil diff --git a/internal/config/config.go b/internal/config/config.go index 6ede6418..ed4bc780 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -124,6 +124,13 @@ func (c *Config) CurrentCluster() *Cluster { // ActiveNamespace returns the active namespace in the current cluster. func (c *Config) ActiveNamespace() string { + if c.client != nil { + ns := c.client.ActiveNamespace() + if client.IsNamespaced(ns) { + return ns + } + } + if cl := c.CurrentCluster(); cl != nil { if cl.Namespace != nil { return cl.Namespace.Active diff --git a/internal/config/plugin.go b/internal/config/plugin.go index 2979deea..61b893ff 100644 --- a/internal/config/plugin.go +++ b/internal/config/plugin.go @@ -17,13 +17,13 @@ type Plugins struct { // Plugin describes a K9s plugin type Plugin struct { - ShortCut string `yaml:"shortCut"` - Confirm bool `yaml:"confirm"` Scopes []string `yaml:"scopes"` + Args []string `yaml:"args"` + ShortCut string `yaml:"shortCut"` Description string `yaml:"description"` Command string `yaml:"command"` + Confirm bool `yaml:"confirm"` Background bool `yaml:"background"` - Args []string `yaml:"args"` } // NewPlugins returns a new plugin. diff --git a/internal/render/header.go b/internal/render/header.go index 231ca2b5..bb993f1e 100644 --- a/internal/render/header.go +++ b/internal/render/header.go @@ -129,7 +129,7 @@ func (h Header) Columns(wide bool) []string { if len(h) == 0 { return nil } - var cc []string + cc := make([]string, 0, len(h)) for _, c := range h { if !wide && c.Wide { continue diff --git a/internal/render/rob.go b/internal/render/rob.go index 5009f7bb..d7443e2f 100644 --- a/internal/render/rob.go +++ b/internal/render/rob.go @@ -76,7 +76,7 @@ func renderSubjects(ss []rbacv1.Subject) (kind string, subjects string) { return NAValue, "" } - var tt []string + tt := make([]string, 0, len(ss)) for _, s := range ss { kind = toSubjectAlias(s.Kind) tt = append(tt, s.Name) diff --git a/internal/ui/select_table.go b/internal/ui/select_table.go index 1002698a..273f0f6e 100644 --- a/internal/ui/select_table.go +++ b/internal/ui/select_table.go @@ -43,7 +43,7 @@ func (s *SelectTable) GetSelectedItems() []string { return []string{s.GetSelectedItem()} } - var items []string + items := make([]string, 0, len(s.marks)) for item := range s.marks { items = append(items, item) } diff --git a/internal/ui/table.go b/internal/ui/table.go index 648e5d7b..fabedc87 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -29,21 +29,20 @@ type ( // Table represents tabular data. type Table struct { + gvr client.GVR + sortCol SortColumn + header render.Header + Path string + Extras string *SelectTable - actions KeyActions - gvr client.GVR - Path string - Extras string cmdBuff *model.FishBuff styles *config.Styles viewSetting *config.ViewSetting - sortCol SortColumn colorerFn render.ColorerFunc decorateFn DecorateFunc wide bool toast bool - header render.Header hasMetrics bool } diff --git a/internal/ui/table_helper.go b/internal/ui/table_helper.go index 2d3f02b2..2e1662aa 100644 --- a/internal/ui/table_helper.go +++ b/internal/ui/table_helper.go @@ -160,7 +160,7 @@ func rxFilter(q string, data render.TableData) (render.TableData, error) { func fuzzyFilter(q string, data render.TableData) render.TableData { q = strings.TrimSpace(q) - var ss []string + ss := make([]string, 0, len(data.RowEvents)) for _, re := range data.RowEvents { ss = append(ss, re.Row.ID) } diff --git a/internal/view/app.go b/internal/view/app.go index a2c2ad15..a5493882 100644 --- a/internal/view/app.go +++ b/internal/view/app.go @@ -21,6 +21,7 @@ import ( "github.com/derailed/tview" "github.com/gdamore/tcell" "github.com/rs/zerolog/log" + "k8s.io/apimachinery/pkg/labels" ) // ExitStatus indicates UI exit conditions. @@ -36,18 +37,17 @@ const ( // App represents an application view. type App struct { + version string *ui.App - Content *PageStack command *Command factory *watch.Factory - version string - showHeader bool cancelFn context.CancelFunc - conRetry int32 clusterModel *model.ClusterInfo cmdHistory *model.History filterHistory *model.History + conRetry int32 + showHeader bool } // NewApp returns a K9s app instance. @@ -93,6 +93,9 @@ func (a *App) Init(version string, rate int) error { } a.factory = watch.NewFactory(a.Conn()) + if !a.isValidNS(ns) { + return fmt.Errorf("Invalid namespace %s", ns) + } a.initFactory(ns) a.clusterModel = model.NewClusterInfo(a.factory, version) @@ -315,16 +318,9 @@ func (a *App) isValidNS(ns string) bool { if ns == client.AllNamespaces || ns == client.NamespaceAll { return true } - nn, err := a.Conn().ValidNamespaces() - if err != nil { - return false - } - for _, n := range nn { - if n.Name == ns { - return true - } - } - return false + _, err := a.factory.Get("v1/namespaces", client.FQN(client.ClusterScope, ns), true, labels.Everything()) + + return err == nil } func (a *App) switchCtx(name string, loadPods bool) error { diff --git a/internal/view/helpers.go b/internal/view/helpers.go index da214707..2ec5223c 100644 --- a/internal/view/helpers.go +++ b/internal/view/helpers.go @@ -76,7 +76,7 @@ func describeResource(app *App, model ui.Tabular, gvr, path string) { } func showPodsWithLabels(app *App, path string, sel map[string]string) { - var labels []string + labels := make([]string, 0, len(sel)) for k, v := range sel { labels = append(labels, fmt.Sprintf("%s=%s", k, v)) } diff --git a/internal/view/node.go b/internal/view/node.go index d4b23f7c..f405c5bd 100644 --- a/internal/view/node.go +++ b/internal/view/node.go @@ -53,8 +53,8 @@ func (n *Node) bindKeys(aa ui.KeyActions) { } } -func (n *Node) showPods(app *App, _ ui.Tabular, _, path string) { - showPods(app, n.GetTable().GetSelectedItem(), "", "spec.nodeName="+path) +func (n *Node) showPods(a *App, _ ui.Tabular, _, path string) { + showPods(a, n.GetTable().GetSelectedItem(), client.AllNamespaces, "spec.nodeName="+path) } func (n *Node) drainCmd(evt *tcell.EventKey) *tcell.EventKey { diff --git a/internal/view/pf_dialog.go b/internal/view/pf_dialog.go index 2b9919f6..3774d119 100644 --- a/internal/view/pf_dialog.go +++ b/internal/view/pf_dialog.go @@ -73,7 +73,7 @@ func DismissPortForwards(v ResourceViewer, p *ui.Pages) { // Helpers... func extractPort(p string) string { - rx := regexp.MustCompile(`\A(\w+)/?(\w+)?:?(\d+)?(╱UDP)?\z`) + rx := regexp.MustCompile(`\A([\w|-]+)/?([\w|-]+)?:?(\d+)?(╱UDP)?\z`) mm := rx.FindStringSubmatch(p) if len(mm) != 5 { return p diff --git a/internal/view/pf_dialog_test.go b/internal/view/pf_dialog_test.go index c97c9a29..8f8854ab 100644 --- a/internal/view/pf_dialog_test.go +++ b/internal/view/pf_dialog_test.go @@ -28,6 +28,12 @@ func TestExtractPort(t *testing.T) { "unamed": { "dns/53", "53", }, + "pod-dashed": { + "blee-fred/:5000", "5000", + }, + "co-dashed": { + "blee/fred-doh:5000", "5000", + }, } for k := range uu { diff --git a/internal/xray/svc.go b/internal/xray/svc.go index 7084d7e8..b4ae0602 100644 --- a/internal/xray/svc.go +++ b/internal/xray/svc.go @@ -75,7 +75,7 @@ func (s *Service) locatePods(ctx context.Context, ns string, sel map[string]stri return nil, fmt.Errorf("Expecting a factory but got %T", ctx.Value(internal.KeyFactory)) } - var ll []string + ll := make([]string, 0, len(sel)) for k, v := range sel { ll = append(ll, fmt.Sprintf("%s=%s", k, v)) } diff --git a/internal/xray/tree_node.go b/internal/xray/tree_node.go index 21f0bed4..b9d744d3 100644 --- a/internal/xray/tree_node.go +++ b/internal/xray/tree_node.go @@ -205,7 +205,7 @@ func (t *TreeNode) Spec() NodeSpec { // Flatten returns a collection of node specs. func (t *TreeNode) Flatten() []NodeSpec { - var refs []NodeSpec + refs := make([]NodeSpec, 0, len(t.Children)) for _, c := range t.Children { if c.IsLeaf() { refs = append(refs, c.Spec())