/
summarizers.go
480 lines (420 loc) · 12.1 KB
/
summarizers.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package summary
import (
"strings"
"time"
"github.com/rancher/wrangler/pkg/data"
"github.com/rancher/wrangler/pkg/data/convert"
"github.com/rancher/wrangler/pkg/kv"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
)
const (
kindSep = ", Kind="
reason = "%REASON%"
)
var (
// True ==
// False == error
// Unknown == transitioning
TransitioningUnknown = map[string]string{
"Active": "activating",
"AddonDeploy": "provisioning",
"AgentDeployed": "provisioning",
"BackingNamespaceCreated": "configuring",
"Built": "building",
"CertsGenerated": "provisioning",
"ConfigOK": "configuring",
"Created": "creating",
"CreatorMadeOwner": "configuring",
"DefaultNamespaceAssigned": "configuring",
"DefaultNetworkPolicyCreated": "configuring",
"DefaultProjectCreated": "configuring",
"DockerProvisioned": "provisioning",
"Deployed": "deploying",
"Drained": "draining",
"Downloaded": "downloading",
"etcd": "provisioning",
"Inactive": "deactivating",
"Initialized": "initializing",
"Installed": "installing",
"NodesCreated": "provisioning",
"Pending": "pending",
"PodScheduled": "scheduling",
"Provisioned": "provisioning",
"Reconciled": "reconciling",
"Refreshed": "refreshed",
"Registered": "registering",
"Removed": "removing",
"Saved": "saving",
"Updated": "updating",
"Updating": "updating",
"Upgraded": "upgrading",
"Waiting": "waiting",
"InitialRolesPopulated": "activating",
"ScalingActive": "pending",
"AbleToScale": "pending",
"RunCompleted": "running",
"Processed": "processed",
}
// True == error
// False ==
// Unknown ==
ErrorTrue = map[string]bool{
"OutOfDisk": true,
"MemoryPressure": true,
"DiskPressure": true,
"NetworkUnavailable": true,
"KernelHasNoDeadlock": true,
"Unschedulable": true,
"ReplicaFailure": true,
"Stalled": true,
}
// True ==
// False == error
// Unknown ==
ErrorFalse = map[string]bool{
"Failed": true,
}
// True ==
// False == transitioning
// Unknown == error
TransitioningFalse = map[string]string{
"Completed": "activating",
"Ready": "unavailable",
"Available": "updating",
"BootstrapReady": reason,
"InfrastructureReady": reason,
"NodeHealthy": reason,
}
// True == transitioning
// False ==
// Unknown == error
TransitioningTrue = map[string]string{
"Reconciling": "reconciling",
}
Summarizers []Summarizer
ConditionSummarizers []Summarizer
)
type Summarizer func(obj data.Object, conditions []Condition, summary Summary) Summary
func init() {
ConditionSummarizers = []Summarizer{
checkErrors,
checkTransitioning,
checkRemoving,
checkCattleReady,
}
Summarizers = []Summarizer{
checkStatusSummary,
checkErrors,
checkTransitioning,
checkActive,
checkPhase,
checkInitializing,
checkRemoving,
checkStandard,
checkLoadBalancer,
checkPod,
checkHasPodSelector,
checkHasPodTemplate,
checkOwner,
checkApplyOwned,
checkCattleTypes,
}
}
func checkOwner(obj data.Object, conditions []Condition, summary Summary) Summary {
ustr := &unstructured.Unstructured{
Object: obj,
}
for _, ownerref := range ustr.GetOwnerReferences() {
rel := Relationship{
Name: ownerref.Name,
Kind: ownerref.Kind,
APIVersion: ownerref.APIVersion,
Type: "owner",
Inbound: true,
}
if ownerref.Controller != nil && *ownerref.Controller {
rel.ControlledBy = true
}
summary.Relationships = append(summary.Relationships, rel)
}
return summary
}
func checkStatusSummary(obj data.Object, _ []Condition, summary Summary) Summary {
summaryObj := obj.Map("status", "display")
if len(summaryObj) == 0 {
summaryObj = obj.Map("status", "summary")
if len(summaryObj) == 0 {
return summary
}
}
obj = summaryObj
if _, ok := obj["state"]; ok {
summary.State = obj.String("state")
}
if _, ok := obj["transitioning"]; ok {
summary.Transitioning = obj.Bool("transitioning")
}
if _, ok := obj["error"]; ok {
summary.Error = obj.Bool("error")
}
if _, ok := obj["message"]; ok {
summary.Message = append(summary.Message, obj.String("message"))
}
return summary
}
func checkStandard(obj data.Object, _ []Condition, summary Summary) Summary {
if summary.State != "" {
return summary
}
// this is a hack to not call the standard summarizers on norman mapped objects
if strings.HasPrefix(obj.String("type"), "/") {
return summary
}
result, err := kstatus.Compute(&unstructured.Unstructured{Object: obj})
if err != nil {
return summary
}
switch result.Status {
case kstatus.InProgressStatus:
summary.State = "in-progress"
summary.Message = append(summary.Message, result.Message)
summary.Transitioning = true
case kstatus.FailedStatus:
summary.State = "failed"
summary.Message = append(summary.Message, result.Message)
summary.Error = true
case kstatus.CurrentStatus:
summary.State = "active"
summary.Message = append(summary.Message, result.Message)
case kstatus.TerminatingStatus:
summary.State = "removing"
summary.Message = append(summary.Message, result.Message)
summary.Transitioning = true
}
return summary
}
func checkErrors(_ data.Object, conditions []Condition, summary Summary) Summary {
for _, c := range conditions {
if (ErrorFalse[c.Type()] && c.Status() == "False") || c.Reason() == "Error" {
summary.Error = true
summary.Message = append(summary.Message, c.Message())
if summary.State == "active" || summary.State == "" {
summary.State = "error"
}
break
}
}
if summary.Error {
return summary
}
for _, c := range conditions {
if ErrorTrue[c.Type()] && c.Status() == "True" {
summary.Error = true
summary.Message = append(summary.Message, c.Message())
}
}
return summary
}
func checkTransitioning(_ data.Object, conditions []Condition, summary Summary) Summary {
for _, c := range conditions {
newState, ok := TransitioningUnknown[c.Type()]
if !ok {
continue
}
if c.Status() == "False" {
summary.Error = true
summary.State = newState
summary.Message = append(summary.Message, c.Message())
} else if c.Status() == "Unknown" && summary.State == "" {
summary.Transitioning = true
summary.State = newState
summary.Message = append(summary.Message, c.Message())
}
}
for _, c := range conditions {
if summary.State != "" {
break
}
newState, ok := TransitioningTrue[c.Type()]
if !ok {
continue
}
if c.Status() == "True" {
summary.Transitioning = true
summary.State = newState
summary.Message = append(summary.Message, c.Message())
}
}
ready := true
readyMessage := ""
for _, c := range conditions {
if summary.State != "" {
break
}
if c.Type() == "Ready" && c.Status() == "False" {
ready = false
readyMessage = c.Message()
continue
}
newState, ok := TransitioningFalse[c.Type()]
if !ok {
continue
}
if newState == reason {
newState = c.Reason()
}
if c.Status() == "False" {
summary.Transitioning = true
summary.State = newState
summary.Message = append(summary.Message, c.Message())
} else if c.Status() == "Unknown" {
summary.Error = true
summary.State = newState
summary.Message = append(summary.Message, c.Message())
}
}
if summary.State == "" && !ready {
summary.Transitioning = true
summary.State = "unavailable"
summary.Message = append(summary.Message, readyMessage)
}
return summary
}
func checkActive(obj data.Object, _ []Condition, summary Summary) Summary {
if summary.State != "" {
return summary
}
switch obj.String("spec", "active") {
case "true":
summary.State = "active"
case "false":
summary.State = "inactive"
}
return summary
}
func checkPhase(obj data.Object, _ []Condition, summary Summary) Summary {
phase := obj.String("status", "phase")
if phase == "Succeeded" {
summary.State = "succeeded"
summary.Transitioning = false
} else if phase == "Bound" {
summary.State = "bound"
summary.Transitioning = false
} else if phase != "" && summary.State == "" {
summary.State = phase
}
return summary
}
func checkInitializing(obj data.Object, conditions []Condition, summary Summary) Summary {
apiVersion := obj.String("apiVersion")
_, hasConditions := obj.Map("status")["conditions"]
if summary.State == "" && hasConditions && len(conditions) == 0 && strings.Contains(apiVersion, "cattle.io") {
val := obj.String("metadata", "created")
if i, err := convert.ToTimestamp(val); err == nil {
if time.Unix(i/1000, 0).Add(5 * time.Second).After(time.Now()) {
summary.State = "initializing"
summary.Transitioning = true
}
}
}
return summary
}
func checkRemoving(obj data.Object, conditions []Condition, summary Summary) Summary {
removed := obj.String("metadata", "removed")
if removed == "" {
return summary
}
summary.State = "removing"
summary.Transitioning = true
finalizers := obj.StringSlice("metadata", "finalizers")
if len(finalizers) == 0 {
finalizers = obj.StringSlice("spec", "finalizers")
}
for _, cond := range conditions {
if cond.Type() == "Removed" && (cond.Status() == "Unknown" || cond.Status() == "False") && cond.Message() != "" {
summary.Message = append(summary.Message, cond.Message())
}
}
if len(finalizers) == 0 {
return summary
}
_, f := kv.RSplit(finalizers[0], "controller.cattle.io/")
if f == "foregroundDeletion" {
f = "object cleanup"
}
summary.Message = append(summary.Message, "waiting on "+f)
if i, err := convert.ToTimestamp(removed); err == nil {
if time.Unix(i/1000, 0).Add(5 * time.Minute).Before(time.Now()) {
summary.Error = true
}
}
return summary
}
func checkLoadBalancer(obj data.Object, _ []Condition, summary Summary) Summary {
if (summary.State == "active" || summary.State == "") &&
obj.String("kind") == "Service" &&
(obj.String("spec", "serviceKind") == "LoadBalancer" ||
obj.String("spec", "type") == "LoadBalancer") {
addresses := obj.Slice("status", "loadBalancer", "ingress")
if len(addresses) == 0 {
summary.State = "pending"
summary.Transitioning = true
summary.Message = append(summary.Message, "Load balancer is being provisioned")
}
}
return summary
}
func isKind(obj data.Object, kind string, apiGroups ...string) bool {
if obj.String("kind") != kind {
return false
}
if len(apiGroups) == 0 {
return obj.String("apiVersion") == "v1"
}
if len(apiGroups) == 0 {
apiGroups = []string{""}
}
for _, group := range apiGroups {
switch {
case group == "":
if obj.String("apiVersion") == "v1" {
return true
}
case group[len(group)-1] == '/':
if strings.HasPrefix(obj.String("apiVersion"), group) {
return true
}
default:
if obj.String("apiVersion") != group {
return true
}
}
}
return false
}
func checkApplyOwned(obj data.Object, conditions []Condition, summary Summary) Summary {
if len(obj.Slice("metadata", "ownerReferences")) > 0 {
return summary
}
annotations := obj.Map("metadata", "annotations")
gvkString := convert.ToString(annotations["objectset.rio.cattle.io/owner-gvk"])
i := strings.Index(gvkString, kindSep)
if i <= 0 {
return summary
}
name := convert.ToString(annotations["objectset.rio.cattle.io/owner-name"])
namespace := convert.ToString(annotations["objectset.rio.cattle.io/owner-namespace"])
apiVersion := gvkString[:i]
kind := gvkString[i+len(kindSep):]
rel := Relationship{
Name: name,
Namespace: namespace,
Kind: kind,
APIVersion: apiVersion,
Type: "applies",
Inbound: true,
}
summary.Relationships = append(summary.Relationships, rel)
return summary
}