-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
sanitizer.go
416 lines (348 loc) · 9.38 KB
/
sanitizer.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package view
import (
"context"
"fmt"
"strings"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/k9s/internal/xray"
"github.com/derailed/tview"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var _ ResourceViewer = (*Sanitizer)(nil)
// Sanitizer represents an sanitizer tree view.
type Sanitizer struct {
*ui.Tree
app *App
gvr client.GVR
meta metav1.APIResource
model *model.Tree
cancelFn context.CancelFunc
envFn EnvFunc
contextFn ContextFunc
}
// NewSanitizer returns a new view.
func NewSanitizer(gvr client.GVR) ResourceViewer {
return &Sanitizer{
gvr: gvr,
Tree: ui.NewTree(),
model: model.NewTree(gvr),
}
}
// Init initializes the view
func (s *Sanitizer) Init(ctx context.Context) error {
s.envFn = s.k9sEnv
if err := s.Tree.Init(ctx); err != nil {
return err
}
s.SetKeyListenerFn(s.keyEntered)
var err error
s.meta, err = dao.MetaAccess.MetaFor(s.gvr)
if err != nil {
return err
}
if s.app, err = extractApp(ctx); err != nil {
return err
}
s.bindKeys()
s.SetBackgroundColor(s.app.Styles.Xray().BgColor.Color())
s.SetBorderColor(s.app.Styles.Xray().FgColor.Color())
s.SetBorderFocusColor(s.app.Styles.Frame().Border.FocusColor.Color())
s.SetGraphicsColor(s.app.Styles.Xray().GraphicColor.Color())
s.SetTitle(strings.Title(s.gvr.R()))
s.model.SetNamespace(client.CleanseNamespace(s.app.Config.ActiveNamespace()))
s.model.AddListener(s)
s.SetChangedFunc(func(n *tview.TreeNode) {
spec, ok := n.GetReference().(xray.NodeSpec)
if !ok {
log.Error().Msgf("No ref found on node %s", n.GetText())
return
}
s.SetSelectedItem(spec.AsPath())
s.refreshActions()
})
s.refreshActions()
return nil
}
// ExtraHints returns additional hints.
func (s *Sanitizer) ExtraHints() map[string]string {
if s.app.Config.K9s.NoIcons {
return nil
}
return xray.EmojiInfo()
}
// SetInstance sets specific resource instance.
func (s *Sanitizer) SetInstance(string) {}
func (s *Sanitizer) bindKeys() {
s.Actions().Add(ui.KeyActions{
ui.KeySlash: ui.NewSharedKeyAction("Filter Mode", s.activateCmd, false),
tcell.KeyEscape: ui.NewSharedKeyAction("Filter Reset", s.resetCmd, false),
tcell.KeyEnter: ui.NewKeyAction("Goto", s.gotoCmd, true),
})
}
func (s *Sanitizer) keyEntered() {
s.ClearSelection()
s.update(s.filter(s.model.Peek()))
}
func (s *Sanitizer) refreshActions() {}
// GetSelectedPath returns the current selection as string.
func (s *Sanitizer) GetSelectedPath() string {
spec := s.selectedSpec()
if spec == nil {
return ""
}
return spec.Path()
}
func (s *Sanitizer) selectedSpec() *xray.NodeSpec {
node := s.GetCurrentNode()
if node == nil {
return nil
}
ref, ok := node.GetReference().(xray.NodeSpec)
if !ok {
log.Error().Msgf("Expecting a NodeSpec!")
return nil
}
return &ref
}
// EnvFn returns an plugin env function if available.
func (s *Sanitizer) EnvFn() EnvFunc {
return s.envFn
}
func (s *Sanitizer) k9sEnv() Env {
env := k8sEnv(s.app.Conn().Config())
spec := s.selectedSpec()
if spec == nil {
return env
}
env["FILTER"] = s.CmdBuff().GetText()
if env["FILTER"] == "" {
ns, n := client.Namespaced(spec.Path())
env["NAMESPACE"], env["FILTER"] = ns, n
}
switch spec.GVR() {
case "containers":
_, co := client.Namespaced(spec.Path())
env["CONTAINER"] = co
ns, n := client.Namespaced(*spec.ParentPath())
env["NAMESPACE"], env["POD"], env["NAME"] = ns, n, co
default:
ns, n := client.Namespaced(spec.Path())
env["NAMESPACE"], env["NAME"] = ns, n
}
return env
}
// Aliases returns all available aliases.
func (s *Sanitizer) Aliases() []string {
return append(s.meta.ShortNames, s.meta.SingularName, s.meta.Name)
}
func (s *Sanitizer) activateCmd(evt *tcell.EventKey) *tcell.EventKey {
if s.app.InCmdMode() {
return evt
}
s.app.ResetPrompt(s.CmdBuff())
return nil
}
func (s *Sanitizer) resetCmd(evt *tcell.EventKey) *tcell.EventKey {
if !s.CmdBuff().InCmdMode() {
s.CmdBuff().Reset()
return s.app.PrevCmd(evt)
}
s.CmdBuff().Reset()
s.model.ClearFilter()
s.Start()
return nil
}
func (s *Sanitizer) gotoCmd(evt *tcell.EventKey) *tcell.EventKey {
if s.CmdBuff().IsActive() {
if ui.IsLabelSelector(s.CmdBuff().GetText()) {
s.Start()
}
s.CmdBuff().SetActive(false)
s.GetRoot().ExpandAll()
return nil
}
spec := s.selectedSpec()
if spec == nil {
return nil
}
if len(spec.GVRs) <= 2 {
return nil
}
path := strings.Replace(spec.Path(), "::", "/", 1)
if strings.Contains(path, "[") {
return nil
}
if len(strings.Split(path, "/")) == 1 && spec.GVR() != "node" {
path = "-/" + path
}
if err := s.app.gotoResource(client.NewGVR(spec.GVR()).R(), path, false); err != nil {
log.Debug().Err(err)
}
return nil
}
func (s *Sanitizer) filter(root *xray.TreeNode) *xray.TreeNode {
q := s.CmdBuff().GetText()
if s.CmdBuff().Empty() || ui.IsLabelSelector(q) {
return root
}
s.UpdateTitle()
if ui.IsFuzzySelector(q) {
return root.Filter(q, fuzzyFilter)
}
return root.Filter(q, rxFilter)
}
// TreeNodeSelected callback for node selection.
func (s *Sanitizer) TreeNodeSelected() {
s.app.QueueUpdateDraw(func() {
n := s.GetCurrentNode()
if n != nil {
n.SetColor(s.app.Styles.Xray().CursorColor.Color())
}
})
}
// TreeLoadFailed notifies the load failed.
func (s *Sanitizer) TreeLoadFailed(err error) {
s.app.Flash().Err(err)
}
func (s *Sanitizer) update(node *xray.TreeNode) {
root := makeTreeNode(node, s.ExpandNodes(), s.app.Config.K9s.NoIcons, s.app.Styles)
if node == nil {
s.app.QueueUpdateDraw(func() {
s.SetRoot(root)
})
return
}
for _, c := range node.Children {
s.hydrate(root, c)
}
if s.GetSelectedItem() == "" {
s.SetSelectedItem(node.Spec().Path())
}
s.app.QueueUpdateDraw(func() {
s.SetRoot(root)
root.Walk(func(node, parent *tview.TreeNode) bool {
spec, ok := node.GetReference().(xray.NodeSpec)
if !ok {
log.Error().Msgf("Expecting a NodeSpec but got %T", node.GetReference())
return false
}
// BOZO!! Figure this out expand/collapse but the root
if parent != nil {
node.SetExpanded(s.ExpandNodes())
} else {
node.SetExpanded(true)
}
if spec.AsPath() == s.GetSelectedItem() {
node.SetExpanded(true).SetSelectable(true)
s.SetCurrentNode(node)
}
return true
})
})
}
// TreeChanged notifies the model data changed.
func (s *Sanitizer) TreeChanged(node *xray.TreeNode) {
s.Count = node.Count(s.gvr.String())
s.update(s.filter(node))
s.UpdateTitle()
}
func (s *Sanitizer) hydrate(parent *tview.TreeNode, n *xray.TreeNode) {
node := makeTreeNode(n, s.ExpandNodes(), s.app.Config.K9s.NoIcons, s.app.Styles)
for _, c := range n.Children {
s.hydrate(node, c)
}
parent.AddChild(node)
}
// SetEnvFn sets the custom environment function.
func (s *Sanitizer) SetEnvFn(EnvFunc) {}
// Refresh updates the view
func (s *Sanitizer) Refresh() {}
// BufferChanged indicates the buffer was changed.
func (s *Sanitizer) BufferChanged(q string) {
s.update(s.filter(s.model.Peek()))
}
// BufferActive indicates the buff activity changed.
func (s *Sanitizer) BufferActive(state bool, k model.BufferKind) {
s.app.BufferActive(state, k)
}
func (s *Sanitizer) defaultContext() context.Context {
ctx := context.WithValue(context.Background(), internal.KeyFactory, s.app.factory)
ctx = context.WithValue(ctx, internal.KeyFields, "")
if s.CmdBuff().Empty() {
ctx = context.WithValue(ctx, internal.KeyLabels, "")
} else {
ctx = context.WithValue(ctx, internal.KeyLabels, ui.TrimLabelSelector(s.CmdBuff().GetText()))
}
return ctx
}
// Start initializes resource watch loop.
func (s *Sanitizer) Start() {
s.Stop()
s.CmdBuff().AddListener(s)
ctx := s.defaultContext()
ctx, s.cancelFn = context.WithCancel(ctx)
if s.contextFn != nil {
ctx = s.contextFn(ctx)
}
s.model.Refresh(ctx)
s.UpdateTitle()
}
// Stop terminates watch loop.
func (s *Sanitizer) Stop() {
if s.cancelFn == nil {
return
}
s.cancelFn()
s.cancelFn = nil
s.CmdBuff().RemoveListener(s)
}
// SetBindKeysFn sets up extra key bindings.
func (s *Sanitizer) SetBindKeysFn(BindKeysFunc) {}
// SetContextFn sets custom context.
func (s *Sanitizer) SetContextFn(f ContextFunc) {
s.contextFn = f
}
// Name returns the component name.
func (s *Sanitizer) Name() string { return "report" }
// GetTable returns the underlying table.
func (s *Sanitizer) GetTable() *Table { return nil }
// GVR returns a resource descriptor.
func (s *Sanitizer) GVR() client.GVR { return s.gvr }
// App returns the current app handle.
func (s *Sanitizer) App() *App {
return s.app
}
// UpdateTitle updates the view title.
func (s *Sanitizer) UpdateTitle() {
t := s.styleTitle()
s.app.QueueUpdateDraw(func() {
s.SetTitle(t)
})
}
func (s *Sanitizer) styleTitle() string {
base := strings.Title(s.gvr.R())
ns := s.model.GetNamespace()
if client.IsAllNamespaces(ns) {
ns = client.NamespaceAll
}
var title string
if ns == client.ClusterScope {
title = ui.SkinTitle(fmt.Sprintf(ui.TitleFmt, base, s.Count), s.app.Styles.Frame())
} else {
title = ui.SkinTitle(fmt.Sprintf(ui.NSTitleFmt, base, ns, s.Count), s.app.Styles.Frame())
}
buff := s.CmdBuff().GetText()
if buff == "" {
return title
}
if ui.IsLabelSelector(buff) {
buff = ui.TrimLabelSelector(buff)
}
return title + ui.SkinTitle(fmt.Sprintf(ui.SearchFmt, buff), s.app.Styles.Frame())
}