forked from derailed/k9s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs.go
89 lines (75 loc) · 2.27 KB
/
rs.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
package view
import (
"fmt"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
)
// ReplicaSet presents a replicaset viewer.
type ReplicaSet struct {
ResourceViewer
}
// NewReplicaSet returns a new viewer.
func NewReplicaSet(gvr client.GVR) ResourceViewer {
r := ReplicaSet{
ResourceViewer: NewBrowser(gvr),
}
r.AddBindKeysFn(r.bindKeys)
r.GetTable().SetEnterFn(r.showPods)
r.GetTable().SetColorerFn(render.ReplicaSet{}.ColorerFunc())
return &r
}
func (r *ReplicaSet) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftD: ui.NewKeyAction("Sort Desired", r.GetTable().SortColCmd("DESIRED", true), false),
ui.KeyShiftC: ui.NewKeyAction("Sort Current", r.GetTable().SortColCmd("CURRENT", true), false),
ui.KeyShiftR: ui.NewKeyAction("Sort Ready", r.GetTable().SortColCmd(readyCol, true), false),
tcell.KeyCtrlL: ui.NewKeyAction("Rollback", r.rollbackCmd, true),
})
}
func (r *ReplicaSet) showPods(app *App, model ui.Tabular, gvr, path string) {
var drs dao.ReplicaSet
rs, err := drs.Load(app.factory, path)
if err != nil {
app.Flash().Err(err)
return
}
showPodsFromSelector(app, path, rs.Spec.Selector)
}
func (r *ReplicaSet) rollbackCmd(evt *tcell.EventKey) *tcell.EventKey {
path := r.GetTable().GetSelectedItem()
if path == "" {
return evt
}
r.showModal(fmt.Sprintf("Rollback %s %s?", r.GVR(), path), func(_ int, button string) {
defer r.dismissModal()
if button != "OK" {
return
}
r.App().Flash().Infof("Rolling back %s %s", r.GVR(), path)
var drs dao.ReplicaSet
drs.Init(r.App().factory, r.GVR())
if err := drs.Rollback(path); err != nil {
r.App().Flash().Err(err)
} else {
r.App().Flash().Infof("%s successfully rolled back", path)
}
r.Refresh()
})
return nil
}
func (r *ReplicaSet) dismissModal() {
r.App().Content.RemovePage("confirm")
}
func (r *ReplicaSet) showModal(msg string, done func(int, string)) {
confirm := tview.NewModal().
AddButtons([]string{"Cancel", "OK"}).
SetTextColor(tcell.ColorFuchsia).
SetText(msg).
SetDoneFunc(done)
r.App().Content.AddPage("confirm", confirm, false, false)
r.App().Content.ShowPage("confirm")
}