-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ns.go
101 lines (87 loc) 路 2.35 KB
/
ns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package view
import (
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/config/data"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
)
const (
favNSIndicator = "+"
defaultNSIndicator = "(*)"
)
// Namespace represents a namespace viewer.
type Namespace struct {
ResourceViewer
}
// NewNamespace returns a new viewer.
func NewNamespace(gvr client.GVR) ResourceViewer {
n := Namespace{
ResourceViewer: NewBrowser(gvr),
}
n.GetTable().SetDecorateFn(n.decorate)
n.GetTable().SetEnterFn(n.switchNs)
n.AddBindKeysFn(n.bindKeys)
return &n
}
func (n *Namespace) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyU: ui.NewKeyAction("Use", n.useNsCmd, true),
ui.KeyShiftS: ui.NewKeyAction("Sort Status", n.GetTable().SortColCmd(statusCol, true), false),
})
}
func (n *Namespace) switchNs(app *App, _ ui.Tabular, _ client.GVR, path string) {
n.useNamespace(path)
app.gotoResource("pods", "", false)
}
func (n *Namespace) useNsCmd(evt *tcell.EventKey) *tcell.EventKey {
path := n.GetTable().GetSelectedItem()
if path == "" {
return nil
}
n.useNamespace(path)
return nil
}
func (n *Namespace) useNamespace(fqn string) {
_, ns := client.Namespaced(fqn)
if client.CleanseNamespace(n.App().Config.ActiveNamespace()) == ns {
return
}
if err := n.App().switchNS(ns); err != nil {
n.App().Flash().Err(err)
return
}
if err := n.App().Config.SetActiveNamespace(ns); err != nil {
n.App().Flash().Err(err)
return
}
}
func (n *Namespace) decorate(td *render.TableData) {
if n.App().Conn() == nil || len(td.RowEvents) == 0 {
return
}
// checks if all ns is in the list if not add it.
if _, ok := td.RowEvents.FindIndex(client.NamespaceAll); !ok {
td.RowEvents = append(td.RowEvents,
render.RowEvent{
Kind: render.EventUnchanged,
Row: render.Row{
ID: client.NamespaceAll,
Fields: render.Fields{client.NamespaceAll, "Active", "", "", ""},
},
},
)
}
for _, re := range td.RowEvents {
if data.InList(n.App().Config.FavNamespaces(), re.Row.ID) {
re.Row.Fields[0] += favNSIndicator
re.Kind = render.EventUnchanged
}
if n.App().Config.ActiveNamespace() == re.Row.ID {
re.Row.Fields[0] += defaultNSIndicator
re.Kind = render.EventUnchanged
}
}
}