-
Notifications
You must be signed in to change notification settings - Fork 196
/
status.go
404 lines (324 loc) · 10.3 KB
/
status.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package status
import (
"bytes"
"fmt"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/internal/utils"
)
const (
Red = "\033[31m"
Yellow = "\033[33m"
Blue = "\033[34m"
Green = "\033[32m"
Magenta = "\033[35m"
Cyan = "\033[36m"
Reset = "\033[0m"
)
const (
OutputJSON = "json"
OutputSummary = "summary"
)
// MapCount is a map to count number of occurrences of a string
type MapCount map[string]int
// MapMapCount is a map of MapCount indexed by string
type MapMapCount map[string]MapCount
// PodStateCount counts the number of pods in the k8s cluster
type PodsCount struct {
// All is the number of all pods in the k8s cluster
All int `json:"all"`
// ByCilium is the number of all the pods in the k8s cluster
ByCilium int `json:"by_cilium"`
}
// PodStateCount counts the number of pods in a particular state
type PodStateCount struct {
// Type is the type of deployment ("Deployment", "DaemonSet", ...)
Type string
// Desired is the number of desired pods to be scheduled
Desired int
// Ready is the number of ready pods
Ready int
// Available is the number of available pods
Available int
// Unavailable is the number of unavailable pods
Unavailable int
}
type PodStateMap map[string]PodStateCount
type CiliumStatusMap map[string]*models.StatusResponse
type ErrorCount struct {
Errors []error
Warnings []error
Disabled bool
}
type ErrorCountMap map[string]*ErrorCount
type ErrorCountMapMap map[string]ErrorCountMap
// Status is the overall status of Cilium
type Status struct {
// ImageCount is a map counting the number of images in use indexed by
// the image name
ImageCount MapMapCount `json:"image_count,omitempty"`
// PhaseCount is a map counting the number of pods in each phase
// (running, failing, scheduled, ...)
PhaseCount MapMapCount `json:"phase_count,omitempty"`
// PodState counts the number of pods matching conditions such as
// desired, ready, available, and unavailable
PodState PodStateMap `json:"pod_state,omitempty"`
// PodsCount is the number of pods in the k8s cluster
// all pods, and pods managed by cilium
PodsCount PodsCount `json:"pods_count,omitempty"`
CiliumStatus CiliumStatusMap `json:"cilium_status,omitempty"`
// Errors is the aggregated errors and warnings of all pods of a
// particular deployment type
Errors ErrorCountMapMap `json:"errors,omitempty"`
// CollectionErrors is the errors that accumulated while collecting the
// status
CollectionErrors []error `json:"collection_errors,omitempty"`
// HelmChartVersion is the Helm chart version that is currently installed.
// For Helm mode only.
HelmChartVersion string `json:"helm_chart_version,omitempty"`
mutex *sync.Mutex
}
func newStatus() *Status {
return &Status{
ImageCount: MapMapCount{},
PhaseCount: MapMapCount{},
PodState: PodStateMap{},
PodsCount: PodsCount{},
CiliumStatus: CiliumStatusMap{},
Errors: ErrorCountMapMap{},
mutex: &sync.Mutex{},
}
}
func (s *Status) aggregatedErrorCount(deployment, pod string) *ErrorCount {
m := s.Errors[deployment]
if m == nil {
m = ErrorCountMap{}
s.Errors[deployment] = m
}
if m[pod] == nil {
m[pod] = &ErrorCount{}
}
return m[pod]
}
func (s *Status) SetDisabled(deployment, pod string, disabled bool) {
m := s.aggregatedErrorCount(deployment, pod)
m.Disabled = disabled
}
func (s *Status) AddAggregatedError(deployment, pod string, err error) {
m := s.aggregatedErrorCount(deployment, pod)
m.Errors = append(m.Errors, err)
}
func (s *Status) AddAggregatedWarning(deployment, pod string, warning error) {
m := s.aggregatedErrorCount(deployment, pod)
m.Warnings = append(m.Warnings, warning)
}
func (s *Status) CollectionError(err error) {
s.CollectionErrors = append(s.CollectionErrors, err)
}
func (s *Status) parseCiliumSubsystemState(deployment, podName, subsystem, state, msg string) {
switch strings.ToLower(state) {
case "warning":
s.AddAggregatedWarning(deployment, podName, fmt.Errorf("%s: %s", subsystem, msg))
case "failure":
s.AddAggregatedError(deployment, podName, fmt.Errorf("%s: %s", subsystem, msg))
}
}
func (s *Status) totalErrors() (total int) {
for _, pods := range s.Errors {
for _, pod := range pods {
total += len(pod.Errors)
}
}
return total
}
func (s *Status) totalWarnings() (total int) {
for _, pods := range s.Errors {
for _, pod := range pods {
total += len(pod.Warnings)
}
}
return total
}
func (s *Status) parseCiliumSubsystemStatus(deployment, podName, subsystem string, status *models.Status) {
if status != nil {
s.parseCiliumSubsystemState(deployment, podName, subsystem, status.State, status.Msg)
}
}
func (s *Status) parseStatusResponse(deployment, podName string, r *models.StatusResponse, err error) {
if err != nil {
s.AddAggregatedError(deployment, podName, fmt.Errorf("unable to retrieve cilium status: %s", err))
return
}
if r.Cilium != nil {
s.parseCiliumSubsystemStatus(deployment, podName, "Cilium", r.Cilium)
}
if r.Cluster != nil {
s.parseCiliumSubsystemStatus(deployment, podName, "Health", r.Cluster.CiliumHealth)
}
if r.Hubble != nil {
s.parseCiliumSubsystemState(deployment, podName, "Hubble", r.Hubble.State, r.Hubble.Msg)
}
if r.Kubernetes != nil {
s.parseCiliumSubsystemState(deployment, podName, "Kubernetes", r.Kubernetes.State, r.Kubernetes.Msg)
}
if r.Kvstore != nil {
s.parseCiliumSubsystemStatus(deployment, podName, "Kvstore", r.Kvstore)
}
if r.AuthCertificateProvider != nil {
s.parseCiliumSubsystemStatus(deployment, podName, "AuthCertificateProvider", r.AuthCertificateProvider)
}
if len(r.Controllers) > 0 {
for _, ctrl := range r.Controllers {
if ctrl.Status == nil || ctrl.Status.ConsecutiveFailureCount == 0 {
continue
}
s.AddAggregatedError(deployment, podName, fmt.Errorf("controller %s is failing since %s (%dx): %s",
ctrl.Name,
time.Since(time.Time(ctrl.Status.LastFailureTimestamp)).Truncate(time.Second).String(),
ctrl.Status.ConsecutiveFailureCount,
ctrl.Status.LastFailureMsg))
}
}
}
func (s *Status) statusSummary(name string) (text string) {
var errors, warnings int
if a := s.Errors[name]; a != nil {
var disabled bool
for _, c := range a {
errors += len(c.Errors)
warnings += len(c.Warnings)
if c.Disabled {
disabled = true
}
}
var s []string
if errors > 0 {
s = append(s, Red+fmt.Sprintf("%d errors", errors)+Reset)
}
if warnings > 0 {
s = append(s, Yellow+fmt.Sprintf("%d warnings", warnings)+Reset)
}
if disabled {
s = append(s, Cyan+"disabled"+Reset)
}
text = strings.Join(s, ", ")
}
if text == "" {
text = Green + "OK" + Reset
}
return
}
func formatPhaseCount(m MapCount) string {
var items []string
for phase, count := range m {
color := ""
switch phase {
case "Failed", "Unknown":
color = Red
case "Running", "Pending", "Succeeded":
color = Green
}
items = append(items, fmt.Sprintf("%s: "+color+"%d"+Reset, phase, count))
}
return strings.Join(items, ", ")
}
func formatPodsCount(count PodsCount) string {
return fmt.Sprintf("%d/%d managed by Cilium", count.ByCilium, count.All)
}
func (c PodStateCount) Format() string {
var items []string
if c.Desired > 0 {
items = append(items, fmt.Sprintf("Desired: %d", c.Desired))
}
if c.Ready > 0 {
color := Green
if c.Ready < c.Desired {
color = Yellow
}
items = append(items, fmt.Sprintf("Ready: "+color+"%d/%d"+Reset, c.Ready, c.Desired))
}
if c.Available > 0 {
color := Green
if c.Ready < c.Desired {
color = Yellow
}
items = append(items, fmt.Sprintf("Available: "+color+"%d/%d"+Reset, c.Available, c.Desired))
}
if c.Unavailable > 0 {
items = append(items, fmt.Sprintf("Unavailable: "+Red+"%d/%d"+Reset, c.Unavailable, c.Desired))
}
return strings.Join(items, ", ")
}
func (s *Status) Format() string {
if s == nil {
return ""
}
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 4, ' ', 0)
fmt.Fprintf(w, Yellow+" /¯¯\\\n")
fmt.Fprintf(w, Cyan+" /¯¯"+Yellow+"\\__/"+Green+"¯¯\\"+Reset+"\tCilium:\t"+s.statusSummary(defaults.AgentDaemonSetName)+"\n")
fmt.Fprintf(w, Cyan+" \\__"+Red+"/¯¯\\"+Green+"__/"+Reset+"\tOperator:\t"+s.statusSummary(defaults.OperatorDeploymentName)+"\n")
fmt.Fprintf(w, Green+" /¯¯"+Red+"\\__/"+Magenta+"¯¯\\"+Reset+"\tEnvoy DaemonSet:\t"+envoyStatusSummary(s.statusSummary(defaults.EnvoyDaemonSetName))+"\n")
fmt.Fprintf(w, Green+" \\__"+Blue+"/¯¯\\"+Magenta+"__/"+Reset+"\tHubble Relay:\t"+s.statusSummary(defaults.RelayDeploymentName)+"\n")
fmt.Fprintf(w, Blue+Blue+Blue+" \\__/"+Reset+"\tClusterMesh:\t"+s.statusSummary(defaults.ClusterMeshDeploymentName)+"\n")
fmt.Fprintf(w, "\n")
if len(s.PodState) > 0 {
for name, podState := range s.PodState {
fmt.Fprintf(w, "%s\t%s\t%s\n", podState.Type, name, podState.Format())
}
}
if len(s.PhaseCount) > 0 {
header := "Containers:"
for name, phaseCount := range s.PhaseCount {
fmt.Fprintf(w, "%s\t%s\t%s\n", header, name, formatPhaseCount(phaseCount))
header = ""
}
}
fmt.Fprintf(w, "Cluster Pods:\t%s\n", formatPodsCount(s.PodsCount))
if utils.IsInHelmMode() {
fmt.Fprintf(w, "Helm chart version:\t%s\n", s.HelmChartVersion)
}
if len(s.ImageCount) > 0 {
header := "Image versions"
for name, imageCount := range s.ImageCount {
for image, count := range imageCount {
fmt.Fprintf(w, "%s\t%s\t%s: %d\n", header, name, image, count)
header = ""
}
}
}
header := "Errors:"
for deployment, pods := range s.Errors {
for pod, a := range pods {
for _, err := range a.Errors {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", header, deployment, pod, err)
header = ""
}
}
}
header = "Warnings:"
for deployment, pods := range s.Errors {
for pod, a := range pods {
for _, err := range a.Warnings {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", header, deployment, pod, err)
header = ""
}
}
}
w.Flush()
return buf.String()
}
// envoyStatusSummary adds some more context to the default `disabled` - mainly to prevent confusion.
// This might get removed once the DaemonSet mode becomes the only available option.
func envoyStatusSummary(statusSummary string) string {
if strings.Contains(statusSummary, "disabled") {
return strings.Replace(statusSummary, "disabled", "disabled (using embedded mode)", 1)
}
return statusSummary
}