forked from kubernetes-sigs/kubebuilder-declarative-pattern
/
health.go
141 lines (124 loc) · 4.09 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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package applyset
import (
"encoding/json"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
)
// isHealthy reports whether the object should be considered "healthy"
// TODO: Replace with kstatus library
func isHealthy(u *unstructured.Unstructured) bool {
// Check if the resource is scheduled for deletion
deletionTimestamp := u.GetDeletionTimestamp()
if deletionTimestamp != nil {
klog.Infof("object %s is scheduled for deletion", humanName(u))
return false
}
gvk := u.GroupVersionKind()
switch gvk {
case schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ConfigMap"},
schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ServiceAccount"}:
// No ready signal; assume ready
return true
case schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"},
schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"},
schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "Role"},
schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "RoleBinding"}:
// No ready signal; assume ready
return true
case schema.GroupVersionKind{Group: "scheduling.k8s.io", Version: "v1", Kind: "PriorityClass"}:
// No ready signal; assume ready
return true
case schema.GroupVersionKind{Group: "storage.k8s.io", Version: "v1", Kind: "StorageClass"}:
// No ready signal; assume ready
return true
}
ready := true
statusConditions, found, err := unstructured.NestedFieldNoCopy(u.Object, "status", "conditions")
if err != nil || !found {
klog.Infof("status conditions not found for %s", humanName(u))
return true
}
statusConditionsList, ok := statusConditions.([]interface{})
if !ok {
klog.Warningf("expected status.conditions to be list, got %T", statusConditions)
return true
}
for i := range statusConditionsList {
condition := statusConditionsList[i]
conditionMap, ok := condition.(map[string]interface{})
if !ok {
klog.Warningf("expected status.conditions[%d] to be map, got %T", i, condition)
return true
}
conditionType := ""
conditionStatus := ""
for k, v := range conditionMap {
switch k {
case "type":
s, ok := v.(string)
if !ok {
klog.Warningf("expected status.conditions[].type to be string, got %T", v)
} else {
conditionType = s
}
case "status":
s, ok := v.(string)
if !ok {
klog.Warningf("expected status.conditions[].status to be string, got %T", v)
} else {
conditionStatus = s
}
}
}
// TODO: Check conditionType?
switch conditionStatus {
case "True":
// ready
case "False":
j, _ := json.Marshal(condition)
klog.Infof("status.conditions indicates object %s is not ready: %v", humanName(u), string(j))
ready = false
case "":
klog.Warningf("ignoring status.conditions[] type %q with unknown status %q", conditionType, conditionStatus)
}
}
if !ready {
klog.Infof("object %s is not ready", humanName(u))
}
return ready
}
// humanName returns an identifier for the object suitable for printing in log messages
func humanName(u *unstructured.Unstructured) string {
gvk := u.GroupVersionKind()
var s strings.Builder
s.WriteString(gvk.Kind)
if gvk.Group != "" {
s.WriteString(".")
s.WriteString(gvk.Group)
}
s.WriteString(":")
namespace := u.GetNamespace()
name := u.GetName()
if namespace != "" {
s.WriteString(namespace)
s.WriteString("/")
s.WriteString(name)
} else {
s.WriteString(name)
}
return s.String()
}