-
Notifications
You must be signed in to change notification settings - Fork 1
/
health_statefulset.go
66 lines (56 loc) · 1.82 KB
/
health_statefulset.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
package health
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func getStatefulSetHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
gvk := obj.GroupVersionKind()
switch gvk {
case appsv1.SchemeGroupVersion.WithKind(StatefulSetKind):
var sts appsv1.StatefulSet
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &sts)
if err != nil {
return nil, fmt.Errorf("failed to convert unstructured StatefulSet to typed: %v", err)
}
return getAppsv1StatefulSetHealth(&sts)
default:
return nil, fmt.Errorf("unsupported StatefulSet GVK: %s", gvk)
}
}
func getAppsv1StatefulSetHealth(sts *appsv1.StatefulSet) (*HealthStatus, error) {
health := HealthHealthy
if sts.Status.ReadyReplicas == 0 {
health = HealthUnhealthy
} else if sts.Status.UpdatedReplicas == 0 {
health = HealthWarning
} else if sts.Spec.Replicas != nil && sts.Status.ReadyReplicas >= *sts.Spec.Replicas {
health = HealthHealthy
}
if sts.Spec.Replicas != nil && sts.Status.ReadyReplicas < *sts.Spec.Replicas {
return &HealthStatus{
Health: health,
Status: HealthStatusRollingOut,
Message: fmt.Sprintf("%d of %d pods ready", sts.Status.ReadyReplicas, *sts.Spec.Replicas),
}, nil
}
if sts.Spec.Replicas != nil && sts.Status.UpdatedReplicas < *sts.Spec.Replicas {
return &HealthStatus{
Health: health,
Status: HealthStatusRollingOut,
Message: fmt.Sprintf("%d of %d pods updated", sts.Status.UpdatedReplicas, *sts.Spec.Replicas),
}, nil
}
if sts.Status.ObservedGeneration == 0 || sts.Generation > sts.Status.ObservedGeneration {
return &HealthStatus{
Health: health,
Status: HealthStatusRollingOut,
}, nil
}
return &HealthStatus{
Ready: true,
Health: health,
Status: HealthStatusRunning,
}, nil
}