This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
status.go
561 lines (471 loc) · 16.8 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package kubestatus
import (
"context"
"errors"
"fmt"
"time"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/dynamic"
typestatus "github.com/seal-io/walrus/pkg/dao/types/status"
"github.com/seal-io/walrus/pkg/operator/k8s/polymorphic"
)
var (
GeneralStatusReady typestatus.Status
GeneralStatusReadyTransitioning typestatus.Status
)
func init() {
GeneralStatusReady.SetSummary(
&typestatus.Summary{
SummaryStatus: string(typestatus.GeneralStatusReady),
},
)
GeneralStatusReadyTransitioning.SetSummary(
&typestatus.Summary{
SummaryStatus: typestatus.GeneralStatusReadyTransitioning,
Transitioning: true,
},
)
}
// StatusError with error message to generate the status error.
func StatusError(msg string) *typestatus.Status {
var s string
if msg == "" {
s = "Server Error"
} else {
s = "Server Error: " + msg
}
return &typestatus.Status{
Summary: typestatus.Summary{
SummaryStatus: typestatus.GeneralStatusError,
Error: true,
SummaryStatusMessage: s,
},
}
}
// Get returns status of the given k8s resource.
func Get(
ctx context.Context,
dynamicCli *dynamic.DynamicClient,
o *unstructured.Unstructured,
) (*typestatus.Status, error) {
switch o.GetKind() {
case "Service":
return getService(o)
case "Pod":
return getPod(o)
case "PersistentVolume", "PersistentVolumeClaim":
return getPersistentVolume(o)
case "APIService":
return getAPIService(o)
case "Deployment":
return getDeployment(o)
case "DaemonSet":
return getDaemonSet(o)
case "StatefulSet":
return getStatefulSet(o)
case "ReplicationController", "ReplicaSet":
return getReplicas(ctx, dynamicCli, o)
case "Job":
return getJob(o)
case "HorizontalPodAutoscaler":
return getHorizontalPodAutoscaler(o)
case "CertificateSigningRequest":
return getCertificateSigningRequest(o)
case "Ingress":
return getIngress(o)
case "PodDisruptionBudget":
return getPodDisruptionBudget(o)
case "ValidatingWebhookConfiguration", "MutatingWebhookConfiguration":
return getWebhookConfiguration(ctx, dynamicCli, o)
}
return &GeneralStatusReady, nil
}
// getService returns the status of kubernetes service resource.
func getService(o *unstructured.Unstructured) (*typestatus.Status, error) {
spec, exist, _ := unstructured.NestedMap(o.Object, "spec")
if !exist {
return nil, errors.New("not found 'service' spec")
}
// If .spec.type == ExternalName, then ready.
specType, _, _ := unstructured.NestedString(spec, "type")
if core.ServiceType(specType) == core.ServiceTypeExternalName {
return &GeneralStatusReady, nil
}
// If .spec.clusterIP == "", then not ready.
specClusterIP, _, _ := unstructured.NestedString(spec, "clusterIP")
if specClusterIP == "" {
return &GeneralStatusReadyTransitioning, nil
}
// If .spec.type == LoadBalancer && len(.spec.externalIPs) > 0, then ready.
// If .spec.type == LoadBalancer && len(.status.loadBalancer.ingress) > 0, then ready.
if core.ServiceType(specType) == core.ServiceTypeLoadBalancer {
specExternalIPs, _, _ := unstructured.NestedStringSlice(spec, "externalIPs")
if len(specExternalIPs) > 0 {
return &GeneralStatusReady, nil
}
statusLBIngresses, _, _ := unstructured.NestedSlice(o.Object, "status", "loadBalancer", "ingress")
if len(statusLBIngresses) > 0 {
return &GeneralStatusReady, nil
}
return &GeneralStatusReadyTransitioning, nil
}
// Otherwise, ready.
return &GeneralStatusReady, nil
}
// getPod returns the status of kubernetes pod resource.
func getPod(o *unstructured.Unstructured) (*typestatus.Status, error) {
status, exist, _ := unstructured.NestedMap(o.Object, "status")
if !exist {
return nil, errors.New("not found 'pod' status")
}
var (
statusConds, _, _ = unstructured.NestedSlice(status, "conditions")
st = &typestatus.Status{}
)
st.SetConditions(toConditions(statusConds))
st.SetSummary(podStatusPaths.Walk(st))
// Dig clearer error message from status.
if st.Error {
st.SummaryStatusMessage = digPodErrorReason(status)
}
return st, nil
}
// getPersistentVolume returns the status of kubernetes persistent volume(claim) resource,.
func getPersistentVolume(o *unstructured.Unstructured) (*typestatus.Status, error) {
status, exist, _ := unstructured.NestedMap(o.Object, "status")
if !exist {
return nil, errors.New("not found 'persistent volume(claim)' status")
}
var (
isErr, isTransition bool
phase, _, _ = unstructured.NestedString(status, "phase")
message, _, _ = unstructured.NestedString(status, "message")
)
switch phase {
case string(core.VolumePending):
isTransition = true
case string(core.VolumeFailed):
isErr = true
}
return &typestatus.Status{
Summary: typestatus.Summary{
SummaryStatus: phase,
SummaryStatusMessage: message,
Error: isErr,
Transitioning: isTransition,
},
}, nil
}
// getReplicas returns the status of kubernetes replica set resource.
func getReplicas(
ctx context.Context,
dynamicCli *dynamic.DynamicClient,
o *unstructured.Unstructured,
) (*typestatus.Status, error) {
st := &typestatus.Status{}
// Use conditions to generate status while it existed.
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if exist {
st.SetConditions(toConditions(statusConditions))
st.SetSummary(replicaSetStatusPaths.Walk(st))
return st, nil
}
// If getPod(all pod) == Running|Succeeded, then ready.
ns, s, err := polymorphic.SelectorsForObject(o)
if err != nil {
return nil, fmt.Errorf("error gettting selector of kubernetes %s %s/%s: %w",
o.GroupVersionKind(), o.GetNamespace(), o.GetName(), err)
}
ss := s.String()
pl, err := dynamicCli.Resource(core.SchemeGroupVersion.WithResource("pods")).
Namespace(ns).
List(ctx, meta.ListOptions{ResourceVersion: "0", LabelSelector: ss})
if err != nil {
return nil, fmt.Errorf("error listing kubernetes %s pods with %s: %w",
ns, ss, err)
}
for i := range pl.Items {
p := pl.Items[i]
ps, err := getPod(&p)
if err != nil {
return nil, fmt.Errorf("error stating kubernetes pod %s/%s: %w",
p.GetNamespace(), p.GetName(), err)
}
switch ps.Summary.SummaryStatus {
default:
return ps, nil
case string(core.PodReady):
// Expected pod phase.
}
}
// Otherwise, not ready.
return &GeneralStatusReady, nil
}
// getAPIService returns the status of kubernetes api service resource,.
func getAPIService(o *unstructured.Unstructured) (*typestatus.Status, error) {
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if !exist {
return nil, errors.New("not found 'api service' status conditions")
}
st := &typestatus.Status{}
st.SetConditions(toConditions(statusConditions))
st.SetSummary(apiServiceStatusPaths.Walk(st))
return st, nil
}
// getDeployment returns the status of kubernetes deployment resource.
func getDeployment(o *unstructured.Unstructured) (*typestatus.Status, error) {
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if !exist {
return nil, errors.New("not found 'deployment' status conditions")
}
st := &typestatus.Status{}
st.SetConditions(toConditions(statusConditions))
st.SetSummary(deploymentStatusPaths.Walk(st))
return st, nil
}
// getDaemonSet returns the status of kubernetes daemon set resource,
// daemonSet status condition is empty, judge the summary based on other fields.
func getDaemonSet(o *unstructured.Unstructured) (*typestatus.Status, error) {
status, exist, _ := unstructured.NestedMap(o.Object, "status")
if !exist {
return nil, errors.New("not found 'daemonSet' status")
}
// If .status.observedGeneration < .metadata.generation, then not ready.
statusObservedGeneration, _, _ := unstructured.NestedInt64(status, "observedGeneration")
if statusObservedGeneration < o.GetGeneration() {
return &GeneralStatusReadyTransitioning, nil
}
spec, exist, _ := unstructured.NestedMap(o.Object, "spec")
if !exist {
return nil, errors.New("not found 'daemonSet' spec")
}
// If .spec.strategy.type != RollingUpdate, then ready.
specStrategyType, _, _ := unstructured.NestedString(spec, "strategy", "type")
if apps.DaemonSetUpdateStrategyType(specStrategyType) !=
apps.RollingUpdateDaemonSetStrategyType {
return &GeneralStatusReady, nil
}
// If .status.desiredNumberScheduled != .status.updatedNumberScheduled, then not ready.
statusDesiredNumberScheduled, _, _ := unstructured.NestedInt64(status, "desiredNumberScheduled")
statusUpdatedNumberScheduled, _, _ := unstructured.NestedInt64(status, "updatedNumberScheduled")
if statusDesiredNumberScheduled != statusUpdatedNumberScheduled {
return &GeneralStatusReadyTransitioning, nil
}
// Expected replicas =
// .status.desiredNumberScheduled - min(.spec.strategy.rollingUpdate.maxUnavailable, .status.desiredNumberScheduled)
// if .status.numberReady < expected replicas, then not ready.
expectedReplicas := statusDesiredNumberScheduled
if statusDesiredNumberScheduled > 0 {
var maxUnavailable int64
specRollingUpdate, exist, _ := unstructured.NestedMap(
spec,
"strategy",
"rollingUpdate",
)
if exist {
maxUnavailableIntStr := intstr.Parse(fmt.Sprint(specRollingUpdate["maxUnavailable"]))
maxUnavailableInt, _ := intstr.GetScaledValueFromIntOrPercent(
&maxUnavailableIntStr,
int(statusDesiredNumberScheduled),
true,
)
maxUnavailable = int64(maxUnavailableInt)
}
if maxUnavailable > statusDesiredNumberScheduled {
maxUnavailable = statusDesiredNumberScheduled
}
expectedReplicas = statusDesiredNumberScheduled - maxUnavailable
}
statusNumberReady, _, _ := unstructured.NestedInt64(status, "numberReady")
if statusNumberReady < expectedReplicas {
return &GeneralStatusReadyTransitioning, nil
}
// Otherwise, ready.
return &GeneralStatusReady, nil
}
// getStatefulSet returns the status of kubernetes stateful set resource,
// daemonSet status condition is empty, judge the summary based on other fields.
func getStatefulSet(o *unstructured.Unstructured) (*typestatus.Status, error) {
status, exist, _ := unstructured.NestedMap(o.Object, "status")
if !exist {
return nil, errors.New("not found 'statefulSet' status")
}
// If .status.observedGeneration < .metadata.generation, then not ready.
statusObservedGeneration, _, _ := unstructured.NestedInt64(status, "observedGeneration")
if statusObservedGeneration < o.GetGeneration() {
return &GeneralStatusReadyTransitioning, nil
}
spec, exist, _ := unstructured.NestedMap(o.Object, "spec")
if !exist {
return nil, errors.New("not found 'statefulSet' spec")
}
// If .status.strategy.type != RollingUpdate, then ready.
specStrategyType, _, _ := unstructured.NestedString(spec, "strategy", "type")
if apps.StatefulSetUpdateStrategyType(specStrategyType) !=
apps.RollingUpdateStatefulSetStrategyType {
return &GeneralStatusReady, nil
}
// Expected replicas = .spec.replicas - .spec.strategy.rollingUpdate.partition.
// If .status.updateReplicas < expected replicas, then not ready.
specReplicas, _, _ := unstructured.NestedInt64(spec, "replicas")
specPartition, _, _ := unstructured.NestedInt64(spec, "strategy", "rollingUpdate", "partition")
expectedReplicas := specReplicas - specPartition
statusUpdatedReplicas, _, _ := unstructured.NestedInt64(status, "updateReplicas")
if statusUpdatedReplicas < expectedReplicas {
return &GeneralStatusReadyTransitioning, nil
}
// If .status.readyReplicas != .spec.replicas, then not ready.
statusReadyReplicas, _, _ := unstructured.NestedInt64(status, "readyReplicas")
if statusReadyReplicas != specReplicas {
return &GeneralStatusReadyTransitioning, nil
}
// If .status.currentRevision != .status.updateRevision, then not ready.
statusCurrentRevision, _, _ := unstructured.NestedString(status, "currentRevision")
statusUpdateRevision, _, _ := unstructured.NestedString(status, "updateRevision")
if statusCurrentRevision != statusUpdateRevision {
return &GeneralStatusReadyTransitioning, nil
}
// Otherwise, ready.
return &GeneralStatusReady, nil
}
// getJob returns the status of kubernetes job resource.
func getJob(o *unstructured.Unstructured) (*typestatus.Status, error) {
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if !exist {
return &typestatus.Status{
Summary: typestatus.Summary{
SummaryStatus: string(typestatus.ResourceStatusProgressing),
Error: false,
Transitioning: true,
},
}, nil
}
st := &typestatus.Status{}
st.SetConditions(toConditions(statusConditions))
st.SetSummary(jobStatusPaths.Walk(st))
return st, nil
}
// getHorizontalPodAutoscaler returns the status of kubernetes hpa resource.
func getHorizontalPodAutoscaler(o *unstructured.Unstructured) (*typestatus.Status, error) {
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if !exist {
return nil, errors.New("not found 'horizontal pod autoscaler' status conditions")
}
st := &typestatus.Status{}
st.SetConditions(toConditions(statusConditions))
st.SetSummary(hpaStatusPaths.Walk(st))
return st, nil
}
// getCertificateSigningRequest returns the status of kubernetes csr resource.
func getCertificateSigningRequest(o *unstructured.Unstructured) (*typestatus.Status, error) {
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if !exist {
return nil, errors.New("not found 'certificate signing request' status conditions")
}
st := &typestatus.Status{}
st.SetConditions(toConditions(statusConditions))
st.SetSummary(csrStatusPaths.Walk(st))
return st, nil
}
// getIngress returns the status of kubernetes ingress resource,
// ingress status isn't contain conditions, judge the summary based on other fields.
func getIngress(o *unstructured.Unstructured) (*typestatus.Status, error) {
// If len(.status.loadBalancer.ingress) != 0, then ready.
statusLBIngresses, _, _ := unstructured.NestedSlice(o.Object, "status", "loadBalancer", "ingress")
if len(statusLBIngresses) > 0 {
return &GeneralStatusReady, nil
}
// Otherwise, not ready.
return &GeneralStatusReadyTransitioning, nil
}
// getPodDisruptionBudget returns the status of kubernetes pdb resource.
func getPodDisruptionBudget(o *unstructured.Unstructured) (*typestatus.Status, error) {
statusConditions, exist, _ := unstructured.NestedSlice(o.Object, "status", "conditions")
if !exist {
return nil, errors.New("not found 'pod disruption budget' status conditions")
}
st := &typestatus.Status{}
st.SetConditions(toConditions(statusConditions))
st.SetSummary(pdbStatusPaths.Walk(st))
return st, nil
}
// getWebhookConfiguration returns the status of kubernetes webhook configuration resource.
func getWebhookConfiguration(
ctx context.Context,
dynamicCli *dynamic.DynamicClient,
o *unstructured.Unstructured,
) (*typestatus.Status, error) {
// If getService(.spec.webhooks[.clientConfig.service?]) == NotReady, then not ready.
specWebhooks, _, _ := unstructured.NestedSlice(o.Object, "spec", "webhooks")
for i := range specWebhooks {
webhook, ok := specWebhooks[i].(map[string]any)
if !ok {
continue
}
webhookService, exist, _ := unstructured.NestedMap(webhook, "clientConfig", "service")
if !exist {
continue
}
svcName := webhookService["name"].(string)
if svcName == "" {
continue
}
svcNamespace := webhookService["namespace"].(string)
svc, err := dynamicCli.Resource(core.SchemeGroupVersion.WithResource("services")).
Namespace(svcNamespace).
Get(ctx, svcName, meta.GetOptions{ResourceVersion: "0"})
if err != nil {
if kerrors.IsNotFound(err) {
return &GeneralStatusReadyTransitioning, nil
}
return nil, err
}
svcState, err := getService(svc)
if err != nil {
return nil, fmt.Errorf("error stating kubernetes service: %w", err)
}
if svcState.SummaryStatus != GeneralStatusReady.SummaryStatus {
return svcState, nil
}
}
// Otherwise, ready.
return &GeneralStatusReady, nil
}
func toConditions(statusConds []any) (conds []typestatus.Condition) {
for i := range statusConds {
condition, ok := statusConds[i].(map[string]any)
if !ok {
continue
}
condType, ok, _ := unstructured.NestedString(condition, "type")
if !ok {
continue
}
condTypeStatus, ok, _ := unstructured.NestedString(condition, "status")
if !ok {
continue
}
msg, _, _ := unstructured.NestedString(condition, "message")
reason, _, _ := unstructured.NestedString(condition, "reason")
cond := typestatus.Condition{
Type: typestatus.ConditionType(condType),
Status: typestatus.ConditionStatus(condTypeStatus),
Message: msg,
Reason: reason,
}
ts, ok, _ := unstructured.NestedString(condition, "lastTransitionTime")
if ok {
lastUpdateTime, err := time.Parse(time.RFC3339, ts)
if err == nil {
cond.LastUpdateTime = lastUpdateTime
}
}
conds = append(conds, cond)
}
return conds
}