-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathregistry.go
More file actions
77 lines (67 loc) · 2.56 KB
/
registry.go
File metadata and controls
77 lines (67 loc) · 2.56 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
package healthchecks
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// HealthCheckFunc is a function that determines if a Kubernetes resource is healthy/ready.
// It returns true if the resource is ready, false otherwise.
type HealthCheckFunc func(obj *unstructured.Unstructured) bool
// registry holds the mapping from GroupVersionKind to health check functions
var registry = make(map[schema.GroupVersionKind]HealthCheckFunc)
// RegisterHealthCheck registers a health check function for a specific GroupVersionKind
func RegisterHealthCheck(gvk schema.GroupVersionKind, fn HealthCheckFunc) {
registry[gvk] = fn
}
// GetHealthCheck retrieves the health check function for a specific GroupVersionKind
// Returns nil if no health check is registered for the GVK
func GetHealthCheck(gvk schema.GroupVersionKind) HealthCheckFunc {
return registry[gvk]
}
// alwaysReady is a health check function for resources that are considered ready
// if they exist in the observed state (e.g., ConfigMap, Secret, ServiceAccount, Namespace).
// These resources don't have meaningful status conditions to check.
func alwaysReady(obj *unstructured.Unstructured) bool {
return true
}
// convertFromUnstructured converts an unstructured object to a typed Kubernetes object
func convertFromUnstructured(obj *unstructured.Unstructured, target interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, target)
}
// getInt64Field extracts an int64 value from a field, handling multiple numeric types
func getInt64Field(obj map[string]interface{}, path ...string) (int64, bool) {
val, found, err := unstructured.NestedFieldNoCopy(obj, path...)
if err != nil || !found || val == nil {
return 0, false
}
switch v := val.(type) {
case int64:
return v, true
case int:
return int64(v), true
case float64:
return int64(v), true
default:
return 0, false
}
}
func init() {
// Register all standard Kubernetes resource health checks
registerConfigMapHealthCheck()
registerCronJobHealthCheck()
registerDaemonSetHealthCheck()
registerDeploymentHealthCheck()
registerHorizontalPodAutoscalerHealthCheck()
registerIngressHealthCheck()
registerJobHealthCheck()
registerNamespaceHealthCheck()
registerPersistentVolumeClaimHealthCheck()
registerPodHealthCheck()
registerReplicaSetHealthCheck()
registerRoleHealthCheck()
registerRoleBindingHealthCheck()
registerSecretHealthCheck()
registerServiceHealthCheck()
registerServiceAccountHealthCheck()
registerStatefulSetHealthCheck()
}