-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cluster_info.go
159 lines (137 loc) · 3.91 KB
/
cluster_info.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
package view
import (
"fmt"
"os"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tview"
"github.com/gdamore/tcell"
)
var _ model.ClusterInfoListener = (*ClusterInfo)(nil)
// ClusterInfo represents a cluster info view.
type ClusterInfo struct {
*tview.Table
app *App
styles *config.Styles
}
// NewClusterInfo returns a new cluster info view.
func NewClusterInfo(app *App) *ClusterInfo {
return &ClusterInfo{
Table: tview.NewTable(),
app: app,
styles: app.Styles,
}
}
// Init initializes the view.
func (c *ClusterInfo) Init() {
c.SetBorderPadding(0, 0, 1, 0)
c.app.Styles.AddListener(c)
c.layout()
c.StylesChanged(c.app.Styles)
}
// StylesChanged notifies skin changed.
func (c *ClusterInfo) StylesChanged(s *config.Styles) {
c.styles = s
c.SetBackgroundColor(s.BgColor())
c.updateStyle()
}
func (c *ClusterInfo) layout() {
for row, section := range []string{"Context", "Cluster", "User", "K9s Rev", "K8s Rev", "CPU", "MEM"} {
if (section == "CPU" || section == "MEM") && !c.app.Conn().HasMetrics() {
continue
}
c.SetCell(row, 0, c.sectionCell(section))
c.SetCell(row, 1, c.infoCell(render.NAValue))
}
}
func (c *ClusterInfo) sectionCell(t string) *tview.TableCell {
cell := tview.NewTableCell(t + ":")
cell.SetAlign(tview.AlignLeft)
cell.SetBackgroundColor(tcell.ColorGreen)
return cell
}
func (c *ClusterInfo) infoCell(t string) *tview.TableCell {
cell := tview.NewTableCell(t)
cell.SetExpansion(2)
cell.SetTextColor(c.styles.K9s.Info.FgColor.Color())
cell.SetBackgroundColor(c.app.Styles.BgColor())
return cell
}
func (c *ClusterInfo) setCell(row int, s string) int {
if s == "" {
s = render.NAValue
}
c.GetCell(row, 1).SetText(s)
return row + 1
}
// ClusterInfoUpdated notifies the cluster meta was updated.
func (c *ClusterInfo) ClusterInfoUpdated(data model.ClusterMeta) {
c.ClusterInfoChanged(data, data)
}
// ClusterInfoChanged notifies the cluster meta was changed.
func (c *ClusterInfo) ClusterInfoChanged(prev, curr model.ClusterMeta) {
c.app.QueueUpdateDraw(func() {
c.Clear()
c.layout()
row := c.setCell(0, curr.Context)
row = c.setCell(row, curr.Cluster)
row = c.setCell(row, curr.User)
row = c.setCell(row, fmt.Sprintf("%s [%d]", curr.K9sVer, os.Getpid()))
row = c.setCell(row, curr.K8sVer)
if c.app.Conn().HasMetrics() {
row = c.setCell(row, ui.AsPercDelta(prev.Cpu, curr.Cpu))
_ = c.setCell(row, ui.AsPercDelta(prev.Mem, curr.Mem))
c.setDefCon(curr.Cpu, curr.Mem)
}
c.updateStyle()
})
}
const defconFmt = "%s %s level!"
func (c *ClusterInfo) setDefCon(cpu, mem int) {
var set bool
l := c.app.Config.K9s.Thresholds.LevelFor("cpu", cpu)
if l > config.SeverityLow {
c.app.Status(flashLevel(l), fmt.Sprintf(defconFmt, flashMessage(l), "CPU"))
set = true
}
l = c.app.Config.K9s.Thresholds.LevelFor("memory", mem)
if l > config.SeverityLow {
c.app.Status(flashLevel(l), fmt.Sprintf(defconFmt, flashMessage(l), "Memory"))
set = true
}
if !set && !c.app.IsBenchmarking() {
c.app.ClearStatus(true)
}
}
func (c *ClusterInfo) updateStyle() {
for row := 0; row < c.GetRowCount(); row++ {
c.GetCell(row, 0).SetTextColor(c.styles.K9s.Info.FgColor.Color())
c.GetCell(row, 0).SetBackgroundColor(c.styles.BgColor())
var s tcell.Style
c.GetCell(row, 1).SetStyle(s.Bold(true).Foreground(c.styles.K9s.Info.SectionColor.Color()))
}
}
// ----------------------------------------------------------------------------
// Helpers...
func flashLevel(l config.SeverityLevel) model.FlashLevel {
switch l {
case config.SeverityHigh:
return model.FlashErr
case config.SeverityMedium:
return model.FlashWarn
default:
return model.FlashInfo
}
}
func flashMessage(l config.SeverityLevel) string {
switch l {
case config.SeverityHigh:
return "Critical"
case config.SeverityMedium:
return "Warning"
default:
return "OK"
}
}