This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathhealth.go
More file actions
178 lines (161 loc) · 5.47 KB
/
Copy pathhealth.go
File metadata and controls
178 lines (161 loc) · 5.47 KB
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
package health
import (
"context"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/argoproj/gitops-engine/pkg/sync/hook"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
)
// 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"
)
// Implements custom health assessment that overrides built-in assessment
type HealthOverride interface {
GetResourceHealth(obj *unstructured.Unstructured) (*HealthStatus, error)
}
// Implements custom health assessment that overrides built-in assessment
type HealthOverrideContext interface {
GetResourceHealth(ctx context.Context, obj *unstructured.Unstructured) (*HealthStatus, error)
}
type HealthOverrideFuncContext func(ctx context.Context, obj *unstructured.Unstructured) (*HealthStatus, error)
// Holds health assessment results
type HealthStatus struct {
Status HealthStatusCode `json:"status,omitempty"`
Message string `json:"message,omitempty"`
}
// 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) {
healthOverrideContext := func(_ context.Context, obj *unstructured.Unstructured) (*HealthStatus, error) {
return healthOverride.GetResourceHealth(obj)
}
return getResourceHealth(context.Background(), obj, healthOverrideContext)
}
// GetResourceHealth returns the health of a k8s resource
func GetResourceHealthContext(ctx context.Context, obj *unstructured.Unstructured, healthOverride HealthOverrideContext) (health *HealthStatus, err error) {
var healthOverrideFunc HealthOverrideFuncContext
if healthOverride != nil {
healthOverrideFunc = func(ctx context.Context, obj *unstructured.Unstructured) (*HealthStatus, error) {
return healthOverride.GetResourceHealth(ctx, obj)
}
}
return getResourceHealth(ctx, obj, healthOverrideFunc)
}
// GetResourceHealth returns the health of a k8s resource
func getResourceHealth(ctx context.Context, obj *unstructured.Unstructured, healthOverride HealthOverrideFuncContext) (health *HealthStatus, err error) {
if obj.GetDeletionTimestamp() != nil && !hook.HasHookFinalizer(obj) {
return &HealthStatus{
Status: HealthStatusProgressing,
Message: "Pending deletion",
}, nil
}
if healthOverride != nil {
health, err := healthOverride(ctx, obj)
if err != nil {
health = &HealthStatus{
Status: HealthStatusUnknown,
Message: err.Error(),
}
return health, err
}
if health != nil {
return health, nil
}
}
if healthCheck := GetHealthCheckFunc(obj.GroupVersionKind()); healthCheck != nil {
if health, err = healthCheck(obj); err != nil {
health = &HealthStatus{
Status: HealthStatusUnknown,
Message: err.Error(),
}
}
}
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) {
switch gvk.Group {
case "apps":
switch gvk.Kind {
case kube.DeploymentKind:
return getDeploymentHealth
case kube.StatefulSetKind:
return getStatefulSetHealth
case kube.ReplicaSetKind:
return getReplicaSetHealth
case kube.DaemonSetKind:
return getDaemonSetHealth
}
case "extensions":
if gvk.Kind == kube.IngressKind {
return getIngressHealth
}
case "argoproj.io":
if gvk.Kind == "Workflow" {
return getArgoWorkflowHealth
}
case "apiregistration.k8s.io":
if gvk.Kind == kube.APIServiceKind {
return getAPIServiceHealth
}
case "networking.k8s.io":
if gvk.Kind == kube.IngressKind {
return getIngressHealth
}
case "":
switch gvk.Kind {
case kube.ServiceKind:
return getServiceHealth
case kube.PersistentVolumeClaimKind:
return getPVCHealth
case kube.PodKind:
return getPodHealth
}
case "batch":
if gvk.Kind == kube.JobKind {
return getJobHealth
}
case "autoscaling":
if gvk.Kind == kube.HorizontalPodAutoscalerKind {
return getHPAHealth
}
}
return nil
}