Skip to content

Commit ac88472

Browse files
committed
Expose more health info as well as counts in Apply results
1 parent b758057 commit ac88472

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

applylib/applyset/applyset.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
257257
visitedUids.Insert(lastApplied.GetUID())
258258
tracker.lastApplied = lastApplied
259259
results.applySuccess(gvk, nn)
260-
tracker.isHealthy = isHealthy(lastApplied)
261-
results.reportHealth(gvk, nn, tracker.isHealthy)
260+
message := ""
261+
status := ""
262+
tracker.isHealthy, status, message = isHealthy(lastApplied)
263+
results.reportHealth(gvk, nn, tracker.isHealthy, status, message)
262264
}
263265

264266
// We want to be more cautions on pruning and only do it if all manifests are applied.

applylib/applyset/health.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ import (
2626

2727
// isHealthy reports whether the object should be considered "healthy"
2828
// TODO: Replace with kstatus library
29-
func isHealthy(u *unstructured.Unstructured) bool {
29+
func isHealthy(u *unstructured.Unstructured) (bool , string, string) {
3030
result, err := status.Compute(u)
3131
if err != nil {
3232
klog.Infof("unable to compute condition for %s", humanName(u))
33-
return false
33+
return false, string(result.Status), result.Message
3434
}
3535
switch result.Status {
3636
case status.InProgressStatus:
37-
return false
37+
return false, string(result.Status), result.Message
3838
case status.FailedStatus:
39-
return false
39+
return false, string(result.Status), result.Message
4040
case status.TerminatingStatus:
41-
return false
41+
return false, string(result.Status), result.Message
4242
case status.UnknownStatus:
4343
klog.Warningf("unknown status for %s", humanName(u))
44-
return false
44+
return false, string(result.Status), result.Message
4545
case status.CurrentStatus:
46-
return true
46+
return true, string(result.Status), result.Message
4747
default:
4848
klog.Warningf("unknown status value %s", result.Status)
49-
return false
49+
return false, string(result.Status), result.Message
5050
}
5151
}
5252

applylib/applyset/results.go

+34-18
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,86 @@ import (
2222
"k8s.io/klog/v2"
2323
)
2424

25+
type ObjectStatus struct {
26+
GVK schema.GroupVersionKind
27+
NameNamespace types.NamespacedName
28+
IsHealthy bool
29+
Status string
30+
Message string
31+
}
32+
2533
// ApplyResults contains the results of an Apply operation.
2634
type ApplyResults struct {
27-
total int
28-
applySuccessCount int
29-
applyFailCount int
30-
pruneSuccessCount int
31-
pruneFailCount int
32-
healthyCount int
33-
unhealthyCount int
35+
Total int
36+
ApplySuccessCount int
37+
ApplyFailCount int
38+
PruneSuccessCount int
39+
PruneFailCount int
40+
HealthyCount int
41+
UnhealthyCount int
42+
Objects []ObjectStatus
3443
}
3544

3645
// AllApplied is true if the desired state has been successfully applied for all objects.
3746
// Note: you likely also want to check AllHealthy, if you want to be sure the objects are "ready".
3847
func (r *ApplyResults) AllApplied() bool {
3948
r.checkInvariants()
4049

41-
return r.applyFailCount == 0 && r.pruneFailCount == 0
50+
return r.ApplyFailCount == 0 && r.PruneFailCount == 0
4251
}
4352

4453
// AllHealthy is true if all the objects have been applied and have converged to a "ready" state.
4554
// Note that this is only meaningful if AllApplied is true.
4655
func (r *ApplyResults) AllHealthy() bool {
4756
r.checkInvariants()
4857

49-
return r.unhealthyCount == 0
58+
return r.UnhealthyCount == 0
5059
}
5160

5261
// checkInvariants is an internal function that warns if the object doesn't match the expected invariants.
5362
func (r *ApplyResults) checkInvariants() {
54-
if r.total != (r.applySuccessCount + r.applyFailCount) {
63+
if r.Total != (r.ApplySuccessCount + r.ApplyFailCount) {
5564
klog.Warningf("consistency error (apply counts): %#v", r)
56-
} else if r.total != (r.healthyCount + r.unhealthyCount) {
65+
} else if r.Total != (r.HealthyCount + r.UnhealthyCount) {
5766
// This "invariant" only holds when all objects could be applied
5867
klog.Warningf("consistency error (healthy counts): %#v", r)
5968
}
6069
}
6170

6271
// applyError records that the apply of an object failed with an error.
6372
func (r *ApplyResults) applyError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
64-
r.applyFailCount++
73+
r.ApplyFailCount++
6574
klog.Warningf("error from apply on %s %s: %v", gvk, nn, err)
6675
}
6776

6877
// applySuccess records that an object was applied and this succeeded.
6978
func (r *ApplyResults) applySuccess(gvk schema.GroupVersionKind, nn types.NamespacedName) {
70-
r.applySuccessCount++
79+
r.ApplySuccessCount++
7180
}
7281

7382
// pruneError records that the prune of an object failed with an error.
7483
func (r *ApplyResults) pruneError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
75-
r.pruneFailCount++
84+
r.PruneFailCount++
7685
klog.Warningf("error from pruning on %s %s: %v", gvk, nn, err)
7786
}
7887

7988
// pruneSuccess records that an object was pruned and this succeeded.
8089
func (r *ApplyResults) pruneSuccess(gvk schema.GroupVersionKind, nn types.NamespacedName) {
81-
r.pruneSuccessCount++
90+
r.PruneSuccessCount++
8291
}
8392

8493
// reportHealth records the health of an object.
85-
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool) {
94+
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, status, message string) {
95+
r.Objects = append(r.Objects, ObjectStatus{
96+
GVK: gvk,
97+
NameNamespace: nn,
98+
IsHealthy: isHealthy,
99+
Message: message,
100+
Status: status,
101+
})
86102
if isHealthy {
87-
r.healthyCount++
103+
r.HealthyCount++
88104
} else {
89-
r.unhealthyCount++
105+
r.UnhealthyCount++
90106
}
91107
}

0 commit comments

Comments
 (0)