forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
475 lines (451 loc) · 16.3 KB
/
health.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
package health
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
coreV1 "k8s.io/api/core/v1"
extv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
"k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/kubernetes/pkg/kubectl/scheme"
appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
hookutil "github.com/argoproj/argo-cd/util/hook"
"github.com/argoproj/argo-cd/util/kube"
"github.com/argoproj/argo-cd/util/lua"
"github.com/argoproj/argo-cd/util/settings"
)
// SetApplicationHealth updates the health statuses of all resources performed in the comparison
func SetApplicationHealth(resStatuses []appv1.ResourceStatus, liveObjs []*unstructured.Unstructured, resourceOverrides map[string]settings.ResourceOverride) (*appv1.HealthStatus, error) {
var savedErr error
appHealth := appv1.HealthStatus{Status: appv1.HealthStatusHealthy}
for i, liveObj := range liveObjs {
var resHealth *appv1.HealthStatus
var err error
if liveObj == nil {
resHealth = &appv1.HealthStatus{Status: appv1.HealthStatusMissing}
} else {
resHealth, err = GetResourceHealth(liveObj, resourceOverrides)
if err != nil && savedErr == nil {
savedErr = err
}
}
resStatuses[i].Health = *resHealth
// Don't allow resource hooks to affect health status
isHook := liveObj != nil && hookutil.IsHook(liveObj)
if !isHook && IsWorse(appHealth.Status, resHealth.Status) {
appHealth.Status = resHealth.Status
}
}
return &appHealth, savedErr
}
// GetResourceHealth returns the health of a k8s resource
func GetResourceHealth(obj *unstructured.Unstructured, resourceOverrides map[string]settings.ResourceOverride) (*appv1.HealthStatus, error) {
var err error
var health *appv1.HealthStatus
health, err = getResourceHealthFromLuaScript(obj, resourceOverrides)
if err != nil {
health = &appv1.HealthStatus{
Status: appv1.HealthStatusUnknown,
Message: err.Error(),
}
return health, err
}
if health != nil {
return health, nil
}
gvk := obj.GroupVersionKind()
switch gvk.Group {
case "apps", "extensions":
switch gvk.Kind {
case kube.DeploymentKind:
health, err = getDeploymentHealth(obj)
case kube.IngressKind:
health, err = getIngressHealth(obj)
case kube.StatefulSetKind:
health, err = getStatefulSetHealth(obj)
case kube.ReplicaSetKind:
health, err = getReplicaSetHealth(obj)
case kube.DaemonSetKind:
health, err = getDaemonSetHealth(obj)
}
case "":
switch gvk.Kind {
case kube.ServiceKind:
health, err = getServiceHealth(obj)
case kube.PersistentVolumeClaimKind:
health, err = getPVCHealth(obj)
case kube.PodKind:
health, err = getPodHealth(obj)
}
case "batch":
switch gvk.Kind {
case kube.JobKind:
health, err = getJobHealth(obj)
}
}
if err != nil {
health = &appv1.HealthStatus{
Status: appv1.HealthStatusUnknown,
Message: err.Error(),
}
} else if health == nil {
health = &appv1.HealthStatus{Status: appv1.HealthStatusHealthy}
}
return health, err
}
// healthOrder is a list of health codes in order of most healthy to least healthy
var healthOrder = []appv1.HealthStatusCode{
appv1.HealthStatusHealthy,
appv1.HealthStatusSuspended,
appv1.HealthStatusProgressing,
appv1.HealthStatusDegraded,
appv1.HealthStatusMissing,
appv1.HealthStatusUnknown,
}
// IsWorse returns whether or not the new health status code is a worser condition than the current
func IsWorse(current, new appv1.HealthStatusCode) bool {
currentIndex := 0
newIndex := 0
for i, code := range healthOrder {
if current == code {
currentIndex = i
}
if new == code {
newIndex = i
}
}
return newIndex > currentIndex
}
func getResourceHealthFromLuaScript(obj *unstructured.Unstructured, resourceOverrides map[string]settings.ResourceOverride) (*appv1.HealthStatus, error) {
luaVM := lua.VM{
ResourceOverrides: resourceOverrides,
}
script, err := luaVM.GetHealthScript(obj)
if err != nil {
return nil, err
}
if script == "" {
return nil, nil
}
result, err := luaVM.ExecuteHealthLua(obj, script)
if err != nil {
return nil, err
}
return result, nil
}
func getPVCHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
pvc := &coreV1.PersistentVolumeClaim{}
err := scheme.Scheme.Convert(obj, pvc, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, pvc, err)
}
switch pvc.Status.Phase {
case coreV1.ClaimLost:
return &appv1.HealthStatus{Status: appv1.HealthStatusDegraded}, nil
case coreV1.ClaimPending:
return &appv1.HealthStatus{Status: appv1.HealthStatusProgressing}, nil
case coreV1.ClaimBound:
return &appv1.HealthStatus{Status: appv1.HealthStatusHealthy}, nil
default:
return &appv1.HealthStatus{Status: appv1.HealthStatusUnknown}, nil
}
}
func getIngressHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
ingress := &extv1beta1.Ingress{}
err := scheme.Scheme.Convert(obj, ingress, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, ingress, err)
}
health := appv1.HealthStatus{}
if len(ingress.Status.LoadBalancer.Ingress) > 0 {
health.Status = appv1.HealthStatusHealthy
} else {
health.Status = appv1.HealthStatusProgressing
}
return &health, nil
}
func getServiceHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
service := &coreV1.Service{}
err := scheme.Scheme.Convert(obj, service, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, service, err)
}
health := appv1.HealthStatus{Status: appv1.HealthStatusHealthy}
if service.Spec.Type == coreV1.ServiceTypeLoadBalancer {
if len(service.Status.LoadBalancer.Ingress) > 0 {
health.Status = appv1.HealthStatusHealthy
} else {
health.Status = appv1.HealthStatusProgressing
}
}
return &health, nil
}
func getDeploymentHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
deployment := &appsv1.Deployment{}
err := scheme.Scheme.Convert(obj, deployment, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, deployment, err)
}
if deployment.Spec.Paused {
return &appv1.HealthStatus{
Status: appv1.HealthStatusSuspended,
Message: "Deployment is paused",
}, nil
}
// Borrowed at kubernetes/kubectl/rollout_status.go https://github.com/kubernetes/kubernetes/blob/5232ad4a00ec93942d0b2c6359ee6cd1201b46bc/pkg/kubectl/rollout_status.go#L80
if deployment.Generation <= deployment.Status.ObservedGeneration {
cond := getDeploymentCondition(deployment.Status, v1.DeploymentProgressing)
if cond != nil && cond.Reason == "ProgressDeadlineExceeded" {
return &appv1.HealthStatus{
Status: appv1.HealthStatusDegraded,
Message: fmt.Sprintf("Deployment %q exceeded its progress deadline", obj.GetName()),
}, nil
} else if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d out of %d new replicas have been updated...", deployment.Status.UpdatedReplicas, *deployment.Spec.Replicas),
}, nil
} else if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d old replicas are pending termination...", deployment.Status.Replicas-deployment.Status.UpdatedReplicas),
}, nil
} else if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d of %d updated replicas are available...", deployment.Status.AvailableReplicas, deployment.Status.UpdatedReplicas),
}, nil
}
} else {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: "Waiting for rollout to finish: observed deployment generation less then desired generation",
}, nil
}
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
}, nil
}
func getDaemonSetHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
daemon := &appsv1.DaemonSet{}
err := scheme.Scheme.Convert(obj, daemon, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, daemon, err)
}
// Borrowed at kubernetes/kubectl/rollout_status.go https://github.com/kubernetes/kubernetes/blob/5232ad4a00ec93942d0b2c6359ee6cd1201b46bc/pkg/kubectl/rollout_status.go#L110
if daemon.Generation <= daemon.Status.ObservedGeneration {
if daemon.Status.UpdatedNumberScheduled < daemon.Status.DesiredNumberScheduled {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d out of %d new pods have been updated...", daemon.Name, daemon.Status.UpdatedNumberScheduled, daemon.Status.DesiredNumberScheduled),
}, nil
}
if daemon.Status.NumberAvailable < daemon.Status.DesiredNumberScheduled {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d of %d updated pods are available...", daemon.Name, daemon.Status.NumberAvailable, daemon.Status.DesiredNumberScheduled),
}, nil
}
} else {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: "Waiting for rollout to finish: observed daemon set generation less then desired generation",
}, nil
}
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
}, nil
}
func getStatefulSetHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
sts := &appsv1.StatefulSet{}
err := scheme.Scheme.Convert(obj, sts, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, sts, err)
}
// Borrowed at kubernetes/kubectl/rollout_status.go https://github.com/kubernetes/kubernetes/blob/5232ad4a00ec93942d0b2c6359ee6cd1201b46bc/pkg/kubectl/rollout_status.go#L131
if sts.Status.ObservedGeneration == 0 || sts.Generation > sts.Status.ObservedGeneration {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: "Waiting for statefulset spec update to be observed...",
}, nil
}
if sts.Spec.Replicas != nil && sts.Status.ReadyReplicas < *sts.Spec.Replicas {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for %d pods to be ready...", *sts.Spec.Replicas-sts.Status.ReadyReplicas),
}, nil
}
if sts.Spec.UpdateStrategy.Type == apps.RollingUpdateStatefulSetStrategyType && sts.Spec.UpdateStrategy.RollingUpdate != nil {
if sts.Spec.Replicas != nil && sts.Spec.UpdateStrategy.RollingUpdate.Partition != nil {
if sts.Status.UpdatedReplicas < (*sts.Spec.Replicas - *sts.Spec.UpdateStrategy.RollingUpdate.Partition) {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for partitioned roll out to finish: %d out of %d new pods have been updated...",
sts.Status.UpdatedReplicas, (*sts.Spec.Replicas - *sts.Spec.UpdateStrategy.RollingUpdate.Partition)),
}, nil
}
}
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
Message: fmt.Sprintf("partitioned roll out complete: %d new pods have been updated...", sts.Status.UpdatedReplicas),
}, nil
}
if sts.Status.UpdateRevision != sts.Status.CurrentRevision {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("waiting for statefulset rolling update to complete %d pods at revision %s...", sts.Status.UpdatedReplicas, sts.Status.UpdateRevision),
}, nil
}
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
Message: fmt.Sprintf("statefulset rolling update complete %d pods at revision %s...", sts.Status.CurrentReplicas, sts.Status.CurrentRevision),
}, nil
}
func getReplicaSetHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
replicaSet := &appsv1.ReplicaSet{}
err := scheme.Scheme.Convert(obj, replicaSet, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, replicaSet, err)
}
if replicaSet.Generation <= replicaSet.Status.ObservedGeneration {
cond := getReplicaSetCondition(replicaSet.Status, v1.ReplicaSetReplicaFailure)
if cond != nil && cond.Status == coreV1.ConditionTrue {
return &appv1.HealthStatus{
Status: appv1.HealthStatusDegraded,
Message: cond.Message,
}, nil
} else if replicaSet.Spec.Replicas != nil && replicaSet.Status.AvailableReplicas < *replicaSet.Spec.Replicas {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: fmt.Sprintf("Waiting for rollout to finish: %d out of %d new replicas are available...", replicaSet.Status.AvailableReplicas, *replicaSet.Spec.Replicas),
}, nil
}
} else {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: "Waiting for rollout to finish: observed replica set generation less then desired generation",
}, nil
}
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
}, nil
}
func getDeploymentCondition(status v1.DeploymentStatus, condType v1.DeploymentConditionType) *v1.DeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
func getReplicaSetCondition(status v1.ReplicaSetStatus, condType v1.ReplicaSetConditionType) *v1.ReplicaSetCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
func getJobHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
job := &batchv1.Job{}
err := scheme.Scheme.Convert(obj, job, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, job, err)
}
failed := false
var failMsg string
complete := false
var message string
for _, condition := range job.Status.Conditions {
switch condition.Type {
case batchv1.JobFailed:
failed = true
complete = true
failMsg = condition.Message
case batchv1.JobComplete:
complete = true
message = condition.Message
}
}
if !complete {
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: message,
}, nil
} else if failed {
return &appv1.HealthStatus{
Status: appv1.HealthStatusDegraded,
Message: failMsg,
}, nil
} else {
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
Message: message,
}, nil
}
}
func getPodHealth(obj *unstructured.Unstructured) (*appv1.HealthStatus, error) {
pod := &coreV1.Pod{}
err := scheme.Scheme.Convert(obj, pod, nil)
if err != nil {
return nil, fmt.Errorf("failed to convert %T to %T: %v", obj, pod, err)
}
switch pod.Status.Phase {
case coreV1.PodPending:
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: pod.Status.Message,
}, nil
case coreV1.PodSucceeded:
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
Message: pod.Status.Message,
}, nil
case coreV1.PodFailed:
return &appv1.HealthStatus{
Status: appv1.HealthStatusDegraded,
Message: pod.Status.Message,
}, nil
case coreV1.PodRunning:
switch pod.Spec.RestartPolicy {
case coreV1.RestartPolicyAlways:
// if pod is ready, it is automatically healthy
if podutil.IsPodReady(pod) {
return &appv1.HealthStatus{
Status: appv1.HealthStatusHealthy,
Message: pod.Status.Message,
}, nil
}
// if it's not ready, check to see if any container terminated, if so, it's degraded
for _, ctrStatus := range pod.Status.ContainerStatuses {
if ctrStatus.LastTerminationState.Terminated != nil {
return &appv1.HealthStatus{
Status: appv1.HealthStatusDegraded,
Message: pod.Status.Message,
}, nil
}
}
// otherwise we are progressing towards a ready state
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: pod.Status.Message,
}, nil
case coreV1.RestartPolicyOnFailure, coreV1.RestartPolicyNever:
// pods set with a restart policy of OnFailure or Never, have a finite life.
// These pods are typically resource hooks. Thus, we consider these as Progressing
// instead of healthy.
return &appv1.HealthStatus{
Status: appv1.HealthStatusProgressing,
Message: pod.Status.Message,
}, nil
}
}
return &appv1.HealthStatus{
Status: appv1.HealthStatusUnknown,
Message: pod.Status.Message,
}, nil
}