-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
pf.go
208 lines (179 loc) 路 5.37 KB
/
pf.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
package view
import (
"context"
"fmt"
"regexp"
"time"
"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/perf"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
"github.com/rs/zerolog/log"
)
const promptPage = "prompt"
// PortForward presents active portforward viewer.
type PortForward struct {
ResourceViewer
bench *perf.Benchmark
}
// NewPortForward returns a new viewer.
func NewPortForward(gvr client.GVR) ResourceViewer {
p := PortForward{
ResourceViewer: NewBrowser(gvr),
}
p.GetTable().SetBorderFocusColor(tcell.ColorDodgerBlue)
p.GetTable().SetSelectedStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorDodgerBlue).Attributes(tcell.AttrNone))
p.GetTable().SetColorerFn(render.PortForward{}.ColorerFunc())
p.GetTable().SetSortCol(ageCol, true)
p.SetContextFn(p.portForwardContext)
p.AddBindKeysFn(p.bindKeys)
return &p
}
func (p *PortForward) portForwardContext(ctx context.Context) context.Context {
return context.WithValue(ctx, internal.KeyBenchCfg, p.App().BenchFile)
}
func (p *PortForward) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
tcell.KeyEnter: ui.NewKeyAction("View Benchmarks", p.showBenchCmd, true),
tcell.KeyCtrlL: ui.NewKeyAction("Benchmark Run/Stop", p.toggleBenchCmd, true),
tcell.KeyCtrlD: ui.NewKeyAction("Delete", p.deleteCmd, true),
ui.KeyShiftP: ui.NewKeyAction("Sort Ports", p.GetTable().SortColCmd("PORTS", true), false),
ui.KeyShiftU: ui.NewKeyAction("Sort URL", p.GetTable().SortColCmd("URL", true), false),
})
}
func (p *PortForward) showBenchCmd(evt *tcell.EventKey) *tcell.EventKey {
b := NewBenchmark(client.NewGVR("benchmarks"))
b.SetContextFn(p.getContext)
if err := p.App().inject(b); err != nil {
p.App().Flash().Err(err)
}
return nil
}
func (p *PortForward) getContext(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, internal.KeyDir, benchDir(p.App().Config))
path := p.GetTable().GetSelectedItem()
if path == "" {
return ctx
}
return context.WithValue(ctx, internal.KeyPath, path)
}
func (p *PortForward) toggleBenchCmd(evt *tcell.EventKey) *tcell.EventKey {
if p.bench != nil {
p.App().Status(model.FlashErr, "Benchmark Canceled!")
p.bench.Cancel()
p.App().ClearStatus(true)
return nil
}
path := p.GetTable().GetSelectedItem()
if path == "" {
return nil
}
cfg := dao.BenchConfigFor(p.App().BenchFile, path)
cfg.Name = path
r, _ := p.GetTable().GetSelection()
log.Debug().Msgf("PF NS %q", p.GetTable().GetModel().GetNamespace())
col := 3
if client.IsAllNamespaces(p.GetTable().GetModel().GetNamespace()) {
col = 4
}
base := ui.TrimCell(p.GetTable().SelectTable, r, col)
var err error
p.bench, err = perf.NewBenchmark(base, p.App().version, cfg)
if err != nil {
p.App().Flash().Errf("Bench failed %v", err)
p.App().ClearStatus(false)
return nil
}
p.App().Status(model.FlashWarn, "Benchmark in progress...")
go p.runBenchmark()
return nil
}
func (p *PortForward) runBenchmark() {
log.Debug().Msg("Bench starting...")
p.bench.Run(p.App().Config.K9s.CurrentCluster, func() {
log.Debug().Msg("Bench Completed!")
p.App().QueueUpdate(func() {
if p.bench.Canceled() {
p.App().Status(model.FlashInfo, "Benchmark canceled")
} else {
p.App().Status(model.FlashInfo, "Benchmark Completed!")
p.bench.Cancel()
}
p.bench = nil
go func() {
<-time.After(2 * time.Second)
p.App().QueueUpdate(func() { p.App().ClearStatus(true) })
}()
})
})
}
func (p *PortForward) deleteCmd(evt *tcell.EventKey) *tcell.EventKey {
if !p.GetTable().CmdBuff().Empty() {
p.GetTable().CmdBuff().Reset()
return nil
}
selections := p.GetTable().GetSelectedItems()
if len(selections) == 0 {
return evt
}
p.Stop()
defer p.Start()
var msg string
if len(selections) > 1 {
msg = fmt.Sprintf("Delete %d marked %s?", len(selections), p.GVR())
} else {
h, err := pfToHuman(selections[0])
if err == nil {
msg = fmt.Sprintf("Delete %s %s?", p.GVR().R(), h)
} else {
p.App().Flash().Err(err)
return nil
}
}
showModal(p.App().Content.Pages, msg, func() {
for _, s := range selections {
var pf dao.PortForward
pf.Init(p.App().factory, client.NewGVR("portforwards"))
if err := pf.Delete(s, nil, true); err != nil {
p.App().Flash().Err(err)
return
}
}
p.App().Flash().Infof("Successfully deleted %d PortForward!", len(selections))
p.GetTable().Refresh()
})
return nil
}
// ----------------------------------------------------------------------------
// Helpers...
var selRx = regexp.MustCompile(`\A([\w-]+)/([\w-]+)\|([\w-]+)\|(\d+):(\d+)`)
func pfToHuman(s string) (string, error) {
mm := selRx.FindStringSubmatch(s)
if len(mm) < 6 {
return "", fmt.Errorf("Unable to parse selection %s", s)
}
return fmt.Sprintf("%s::%s %s->%s", mm[2], mm[3], mm[4], mm[5]), nil
}
func showModal(p *ui.Pages, msg string, ok func()) {
m := tview.NewModal().
AddButtons([]string{"Cancel", "OK"}).
SetTextColor(tcell.ColorFuchsia).
SetText(msg).
SetDoneFunc(func(_ int, b string) {
if b == "OK" {
ok()
}
dismissModal(p)
})
m.SetTitle("<Delete Benchmark>")
p.AddPage(promptPage, m, false, false)
p.ShowPage(promptPage)
}
func dismissModal(p *ui.Pages) {
p.RemovePage(promptPage)
}