-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
helpers.go
217 lines (186 loc) 路 4.99 KB
/
helpers.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
package view
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
)
func k8sEnv(c *client.Config) Env {
ctx, err := c.CurrentContextName()
if err != nil {
ctx = render.NAValue
}
cluster, err := c.CurrentClusterName()
if err != nil {
cluster = render.NAValue
}
user, err := c.CurrentUserName()
if err != nil {
user = render.NAValue
}
groups, err := c.CurrentGroupNames()
if err != nil {
groups = []string{render.NAValue}
}
var cfg string
kcfg := c.Flags().KubeConfig
if kcfg != nil && *kcfg != "" {
cfg = *kcfg
}
return Env{
"CONTEXT": ctx,
"CLUSTER": cluster,
"USER": user,
"GROUPS": strings.Join(groups, ","),
"KUBECONFIG": cfg,
}
}
func defaultEnv(c *client.Config, path string, header render.Header, row render.Row) Env {
env := k8sEnv(c)
log.Debug().Msgf("PATH %q::%q", path, row.Fields[1])
env["NAMESPACE"], env["NAME"] = client.Namespaced(path)
for _, col := range header.Columns(true) {
env["COL-"+col] = row.Fields[header.IndexOf(col, true)]
}
return env
}
func describeResource(app *App, model ui.Tabular, gvr, path string) {
ctx := context.Background()
ctx = context.WithValue(ctx, internal.KeyFactory, app.factory)
yaml, err := model.Describe(ctx, path)
if err != nil {
app.Flash().Errf("Describe command failed: %s", err)
return
}
details := NewDetails(app, "Describe", path, true).Update(yaml)
if err := app.inject(details); err != nil {
app.Flash().Err(err)
}
}
func showPodsWithLabels(app *App, path string, sel map[string]string) {
labels := make([]string, 0, len(sel))
for k, v := range sel {
labels = append(labels, fmt.Sprintf("%s=%s", k, v))
}
showPods(app, path, strings.Join(labels, ","), "")
}
func showPods(app *App, path, labelSel, fieldSel string) {
if err := app.switchNS(client.AllNamespaces); err != nil {
app.Flash().Err(err)
return
}
v := NewPod(client.NewGVR("v1/pods"))
v.SetContextFn(podCtx(app, path, labelSel, fieldSel))
v.GetTable().SetColorerFn(render.Pod{}.ColorerFunc())
ns, _ := client.Namespaced(path)
if err := app.Config.SetActiveNamespace(ns); err != nil {
log.Error().Err(err).Msg("Config NS set failed!")
}
if err := app.inject(v); err != nil {
app.Flash().Err(err)
}
}
func podCtx(app *App, path, labelSel, fieldSel string) ContextFunc {
return func(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, internal.KeyPath, path)
ctx = context.WithValue(ctx, internal.KeyLabels, labelSel)
ns, _ := client.Namespaced(path)
mx := client.NewMetricsServer(app.factory.Client())
nmx, err := mx.FetchPodsMetrics(ctx, ns)
if err != nil {
log.Debug().Err(err).Msgf("No pods metrics")
}
ctx = context.WithValue(ctx, internal.KeyMetrics, nmx)
return context.WithValue(ctx, internal.KeyFields, fieldSel)
}
}
func extractApp(ctx context.Context) (*App, error) {
app, ok := ctx.Value(internal.KeyApp).(*App)
if !ok {
return nil, errors.New("No application found in context")
}
return app, nil
}
// AsKey maps a string representation of a key to a tcell key.
func asKey(key string) (tcell.Key, error) {
for k, v := range tcell.KeyNames {
if v == key {
return k, nil
}
}
return 0, fmt.Errorf("No matching key found %s", key)
}
// FwFQN returns a fully qualified ns/name:container id.
func fwFQN(po, co string) string {
return po + ":" + co
}
func isTCPPort(p string) bool {
return !strings.Contains(p, "UDP")
}
// ContainerID computes container ID based on ns/po/co.
func containerID(path, co string) string {
ns, n := client.Namespaced(path)
po := strings.Split(n, "-")[0]
return ns + "/" + po + ":" + co
}
// UrlFor computes fq url for a given benchmark configuration.
func urlFor(cfg config.BenchConfig, port string) string {
host := "localhost"
if cfg.HTTP.Host != "" {
host = cfg.HTTP.Host
}
path := "/"
if cfg.HTTP.Path != "" {
path = cfg.HTTP.Path
}
return "http://" + host + ":" + port + path
}
func fqn(ns, n string) string {
if ns == "" {
return n
}
return ns + "/" + n
}
func decorateCpuMemHeaderRows(app *App, data render.TableData) render.TableData {
for colIndex, header := range data.Header {
check := ""
if header.Name == "%CPU/L" {
check = "cpu"
}
if header.Name == "%MEM/L" {
check = "memory"
}
if len(check) == 0 {
continue
}
for _, re := range data.RowEvents {
if re.Row.Fields[colIndex] == render.NAValue {
continue
}
n, err := strconv.Atoi(re.Row.Fields[colIndex])
if err != nil {
continue
}
if n > 100 {
n = 100
}
severity := app.Config.K9s.Thresholds.LevelFor(check, n)
if severity == config.SeverityLow {
continue
}
color := app.Config.K9s.Thresholds.SeverityColor(check, n)
if len(color) > 0 {
re.Row.Fields[colIndex] = "[" + color + "::b]" + re.Row.Fields[colIndex]
}
}
}
return data
}