Skip to content

Commit 5f0e9dd

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

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

applylib/applyset/applyset.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ 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+
tracker.isHealthy, message = isHealthy(lastApplied)
262+
results.reportHealth(gvk, nn, tracker.isHealthy, message)
262263
}
263264

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

applylib/applyset/results.go

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

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

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

41-
return r.applyFailCount == 0 && r.pruneFailCount == 0
49+
return r.ApplyFailCount == 0 && r.PruneFailCount == 0
4250
}
4351

4452
// AllHealthy is true if all the objects have been applied and have converged to a "ready" state.
4553
// Note that this is only meaningful if AllApplied is true.
4654
func (r *ApplyResults) AllHealthy() bool {
4755
r.checkInvariants()
4856

49-
return r.unhealthyCount == 0
57+
return r.UnhealthyCount == 0
5058
}
5159

5260
// checkInvariants is an internal function that warns if the object doesn't match the expected invariants.
5361
func (r *ApplyResults) checkInvariants() {
54-
if r.total != (r.applySuccessCount + r.applyFailCount) {
62+
if r.Total != (r.ApplySuccessCount + r.ApplyFailCount) {
5563
klog.Warningf("consistency error (apply counts): %#v", r)
56-
} else if r.total != (r.healthyCount + r.unhealthyCount) {
64+
} else if r.Total != (r.HealthyCount + r.UnhealthyCount) {
5765
// This "invariant" only holds when all objects could be applied
5866
klog.Warningf("consistency error (healthy counts): %#v", r)
5967
}
6068
}
6169

6270
// applyError records that the apply of an object failed with an error.
6371
func (r *ApplyResults) applyError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
64-
r.applyFailCount++
72+
r.ApplyFailCount++
6573
klog.Warningf("error from apply on %s %s: %v", gvk, nn, err)
6674
}
6775

6876
// applySuccess records that an object was applied and this succeeded.
6977
func (r *ApplyResults) applySuccess(gvk schema.GroupVersionKind, nn types.NamespacedName) {
70-
r.applySuccessCount++
78+
r.ApplySuccessCount++
7179
}
7280

7381
// pruneError records that the prune of an object failed with an error.
7482
func (r *ApplyResults) pruneError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
75-
r.pruneFailCount++
83+
r.PruneFailCount++
7684
klog.Warningf("error from pruning on %s %s: %v", gvk, nn, err)
7785
}
7886

7987
// pruneSuccess records that an object was pruned and this succeeded.
8088
func (r *ApplyResults) pruneSuccess(gvk schema.GroupVersionKind, nn types.NamespacedName) {
81-
r.pruneSuccessCount++
89+
r.PruneSuccessCount++
8290
}
8391

8492
// reportHealth records the health of an object.
85-
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool) {
93+
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string) {
94+
r.Objects = append(r.Objects, ObjectStatus{
95+
GVK: gvk,
96+
NameNamespace: nn,
97+
IsHealthy: isHealthy,
98+
Message: message,
99+
})
86100
if isHealthy {
87-
r.healthyCount++
101+
r.HealthyCount++
88102
} else {
89-
r.unhealthyCount++
103+
r.UnhealthyCount++
90104
}
91105
}

0 commit comments

Comments
 (0)