Skip to content

Commit 4abaa68

Browse files
authored
Merge pull request #309 from yuwenma/applyset-health
Leverage kstatus for applyset healthcheck
2 parents 285081a + cc55694 commit 4abaa68

File tree

3 files changed

+265
-86
lines changed

3 files changed

+265
-86
lines changed

applylib/applyset/health.go

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -17,105 +17,34 @@ limitations under the License.
1717
package applyset
1818

1919
import (
20-
"encoding/json"
2120
"strings"
2221

2322
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24-
"k8s.io/apimachinery/pkg/runtime/schema"
2523
"k8s.io/klog/v2"
24+
"sigs.k8s.io/kustomize/kstatus/status"
2625
)
2726

2827
// isHealthy reports whether the object should be considered "healthy"
2928
// TODO: Replace with kstatus library
3029
func isHealthy(u *unstructured.Unstructured) bool {
31-
// Check if the resource is scheduled for deletion
32-
deletionTimestamp := u.GetDeletionTimestamp()
33-
if deletionTimestamp != nil {
34-
klog.Infof("object %s is scheduled for deletion", humanName(u))
30+
result, err := status.Compute(u)
31+
if err != nil {
32+
klog.Infof("unable to compute condition for %s", humanName(u))
3533
return false
3634
}
37-
38-
gvk := u.GroupVersionKind()
39-
switch gvk {
40-
case schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ConfigMap"},
41-
schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ServiceAccount"}:
42-
// No ready signal; assume ready
43-
return true
44-
case schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"},
45-
schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"},
46-
schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "Role"},
47-
schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "RoleBinding"}:
48-
// No ready signal; assume ready
49-
return true
50-
case schema.GroupVersionKind{Group: "scheduling.k8s.io", Version: "v1", Kind: "PriorityClass"}:
51-
// No ready signal; assume ready
52-
return true
53-
case schema.GroupVersionKind{Group: "storage.k8s.io", Version: "v1", Kind: "StorageClass"}:
54-
// No ready signal; assume ready
55-
return true
56-
}
57-
58-
ready := true
59-
statusConditions, found, err := unstructured.NestedFieldNoCopy(u.Object, "status", "conditions")
60-
if err != nil || !found {
61-
klog.Infof("status conditions not found for %s", humanName(u))
62-
return true
63-
}
64-
65-
statusConditionsList, ok := statusConditions.([]interface{})
66-
if !ok {
67-
klog.Warningf("expected status.conditions to be list, got %T", statusConditions)
35+
switch result.Status {
36+
case status.InProgressStatus:
37+
return false
38+
case status.FailedStatus:
39+
return false
40+
case status.TerminatingStatus:
41+
return false
42+
case status.UnknownStatus:
43+
klog.Warningf("unknown status for %s", humanName(u))
44+
return false
45+
default: // status.CurrentStatus:
6846
return true
6947
}
70-
for i := range statusConditionsList {
71-
condition := statusConditionsList[i]
72-
conditionMap, ok := condition.(map[string]interface{})
73-
if !ok {
74-
klog.Warningf("expected status.conditions[%d] to be map, got %T", i, condition)
75-
return true
76-
}
77-
78-
conditionType := ""
79-
conditionStatus := ""
80-
for k, v := range conditionMap {
81-
switch k {
82-
case "type":
83-
s, ok := v.(string)
84-
if !ok {
85-
klog.Warningf("expected status.conditions[].type to be string, got %T", v)
86-
} else {
87-
conditionType = s
88-
}
89-
case "status":
90-
s, ok := v.(string)
91-
if !ok {
92-
klog.Warningf("expected status.conditions[].status to be string, got %T", v)
93-
} else {
94-
conditionStatus = s
95-
}
96-
}
97-
}
98-
99-
// TODO: Check conditionType?
100-
101-
switch conditionStatus {
102-
case "True":
103-
// ready
104-
105-
case "False":
106-
j, _ := json.Marshal(condition)
107-
klog.Infof("status.conditions indicates object %s is not ready: %v", humanName(u), string(j))
108-
ready = false
109-
110-
case "":
111-
klog.Warningf("ignoring status.conditions[] type %q with unknown status %q", conditionType, conditionStatus)
112-
}
113-
}
114-
115-
if !ready {
116-
klog.Infof("object %s is not ready", humanName(u))
117-
}
118-
return ready
11948
}
12049

12150
// humanName returns an identifier for the object suitable for printing in log messages

applylib/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
k8s.io/klog/v2 v2.90.1
1515
sigs.k8s.io/controller-runtime v0.14.1
1616
sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver v0.0.0-20221021151406-9bd3fb842119
17+
sigs.k8s.io/kustomize/kstatus v0.0.2-0.20200509233124-065f70705d4d
1718
sigs.k8s.io/yaml v1.3.0
1819
)
1920

0 commit comments

Comments
 (0)