/
health.go
201 lines (183 loc) · 5.82 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
package health
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type Health string
const (
HealthHealthy Health = "healthy"
HealthUnhealthy Health = "unhealthy"
HealthUnknown Health = "unknown"
HealthWarning Health = "warning"
)
// Represents resource health status
type HealthStatusCode string
const (
// Indicates that health assessment failed and actual health status is unknown
HealthStatusUnknown HealthStatusCode = "Unknown"
// Progressing health status means that resource is not healthy but still have a chance to reach healthy state
HealthStatusProgressing HealthStatusCode = "Progressing"
// Resource is 100% healthy
HealthStatusHealthy HealthStatusCode = "Healthy"
// Assigned to resources that are suspended or paused. The typical example is a
// [suspended](https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#suspend) CronJob.
HealthStatusSuspended HealthStatusCode = "Suspended"
// Degrade status is used if resource status indicates failure or resource could not reach healthy state
// within some timeout.
HealthStatusDegraded HealthStatusCode = "Degraded"
// Indicates that resource is missing in the cluster.
HealthStatusMissing HealthStatusCode = "Missing"
HealthStatusCompleted HealthStatusCode = "Completed"
HealthStatusCrashLoopBackoff HealthStatusCode = "CrashLoopBackOff"
HealthStatusCreating HealthStatusCode = "Creating"
HealthStatusDeleted HealthStatusCode = "Deleted"
HealthStatusDeleting HealthStatusCode = "Deleting"
HealthStatusError HealthStatusCode = "Error"
HealthStatusRolloutFailed HealthStatusCode = "Rollout Failed"
HealthStatusInaccesible HealthStatusCode = "Inaccesible"
HealthStatusInfo HealthStatusCode = "Info"
HealthStatusPending HealthStatusCode = "Pending"
HealthStatusMaintenance HealthStatusCode = "Maintenance"
HealthStatusScaling HealthStatusCode = "Scaling"
HealthStatusRestart HealthStatusCode = "Restarting"
HealthStatusStarting HealthStatusCode = "Starting"
HealthStatusScalingUp HealthStatusCode = "Scaling Up"
HealthStatusScaledToZero HealthStatusCode = "Scaled to Zero"
HealthStatusScalingDown HealthStatusCode = "Scaling Down"
HealthStatusRunning HealthStatusCode = "Running"
HealthStatusRollingOut HealthStatusCode = "Rolling Out"
HealthStatusUnhealthy HealthStatusCode = "Unhealthy"
HealthStatusUpdating HealthStatusCode = "Updating"
HealthStatusWarning HealthStatusCode = "Warning"
HealthStatusStopped HealthStatusCode = "Stopped"
HealthStatusStopping HealthStatusCode = "Stopping"
)
// Implements custom health assessment that overrides built-in assessment
type HealthOverride interface {
GetResourceHealth(obj *unstructured.Unstructured) (*HealthStatus, error)
}
// healthOrder is a list of health codes in order of most healthy to least healthy
var healthOrder = []HealthStatusCode{
HealthStatusHealthy,
HealthStatusSuspended,
HealthStatusProgressing,
HealthStatusMissing,
HealthStatusDegraded,
HealthStatusUnknown,
}
// IsWorse returns whether or not the new health status code is a worse condition than the current
func IsWorse(current, new HealthStatusCode) bool {
currentIndex := 0
newIndex := 0
for i, code := range healthOrder {
if current == code {
currentIndex = i
}
if new == code {
newIndex = i
}
}
return newIndex > currentIndex
}
// GetResourceHealth returns the health of a k8s resource
func GetResourceHealth(obj *unstructured.Unstructured, healthOverride HealthOverride) (health *HealthStatus, err error) {
if healthCheck := GetHealthCheckFunc(obj.GroupVersionKind()); healthCheck != nil {
if health, err = healthCheck(obj); err != nil {
health = &HealthStatus{
Status: HealthStatusUnknown,
Message: err.Error(),
}
} else {
return health, nil
}
}
if healthOverride != nil {
health, err := healthOverride.GetResourceHealth(obj)
if err != nil {
health = &HealthStatus{
Status: HealthStatusUnknown,
Message: err.Error(),
}
return health, err
}
if health != nil {
return health, nil
}
}
if obj.GetDeletionTimestamp() != nil {
return &HealthStatus{
Status: HealthStatusDeleting,
}, nil
}
if health == nil {
return &HealthStatus{
Status: HealthStatusUnknown,
Ready: true,
}, nil
}
return health, err
}
// GetHealthCheckFunc returns built-in health check function or nil if health check is not supported
func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unstructured) (*HealthStatus, error) {
if gvk.Kind == "Node" {
return getNodeHealth
}
switch gvk.Group {
case "apps":
switch gvk.Kind {
case DeploymentKind:
return getDeploymentHealth
case StatefulSetKind:
return getStatefulSetHealth
case ReplicaSetKind:
return getReplicaSetHealth
case DaemonSetKind:
return getDaemonSetHealth
}
case "extensions":
switch gvk.Kind {
case IngressKind:
return getIngressHealth
}
case "argoproj.io":
switch gvk.Kind {
case "Workflow":
return GetArgoWorkflowHealth
case "Application":
return getArgoApplicationHealth
}
case "kustomize.toolkit.fluxcd.io", "helm.toolkit.fluxcd.io", "source.toolkit.fluxcd.io":
return GetDefaultHealth
case "cert-manager.io":
return GetDefaultHealth
case "networking.k8s.io":
switch gvk.Kind {
case IngressKind:
return getIngressHealth
}
case "":
switch gvk.Kind {
case ServiceKind:
return getServiceHealth
case PersistentVolumeClaimKind:
return getPVCHealth
case PodKind:
return getPodHealth
case NamespaceKind:
return getNamespaceHealth
}
case "batch":
switch gvk.Kind {
case JobKind:
return getJobHealth
case CronJobKind:
return getCronJobHealth
}
case "autoscaling":
switch gvk.Kind {
case HorizontalPodAutoscalerKind:
return getHPAHealth
}
}
return nil
}