Skip to content

Commit a248ed1

Browse files
authored
Merge pull request #393 from barney-s/improve-results
Expose more health info as well as counts in Apply results
2 parents 53ff542 + 3774c11 commit a248ed1

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
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/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) {
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, result.Message
3434
}
3535
switch result.Status {
3636
case status.InProgressStatus:
37-
return false
37+
return false, result.Message
3838
case status.FailedStatus:
39-
return false
39+
return false, result.Message
4040
case status.TerminatingStatus:
41-
return false
41+
return false, result.Message
4242
case status.UnknownStatus:
4343
klog.Warningf("unknown status for %s", humanName(u))
44-
return false
44+
return false, result.Message
4545
case status.CurrentStatus:
46-
return true
46+
return true, result.Message
4747
default:
4848
klog.Warningf("unknown status value %s", result.Status)
49-
return false
49+
return false, result.Message
5050
}
5151
}
5252

applylib/applyset/results.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ import (
2222
"k8s.io/klog/v2"
2323
)
2424

25+
type ObjectStatus struct {
26+
GVK schema.GroupVersionKind
27+
NameNamespace types.NamespacedName
28+
IsHealthy bool
29+
IsPruned bool
30+
Message string
31+
Error error
32+
}
33+
2534
// ApplyResults contains the results of an Apply operation.
2635
type ApplyResults struct {
2736
total int
@@ -31,6 +40,7 @@ type ApplyResults struct {
3140
pruneFailCount int
3241
healthyCount int
3342
unhealthyCount int
43+
Objects []ObjectStatus
3444
}
3545

3646
// AllApplied is true if the desired state has been successfully applied for all objects.
@@ -62,6 +72,14 @@ func (r *ApplyResults) checkInvariants() {
6272
// applyError records that the apply of an object failed with an error.
6373
func (r *ApplyResults) applyError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
6474
r.applyFailCount++
75+
r.Objects = append(r.Objects, ObjectStatus{
76+
GVK: gvk,
77+
NameNamespace: nn,
78+
IsHealthy: false,
79+
IsPruned: false,
80+
Message: "Apply Error",
81+
Error: err,
82+
})
6583
klog.Warningf("error from apply on %s %s: %v", gvk, nn, err)
6684
}
6785

@@ -72,17 +90,37 @@ func (r *ApplyResults) applySuccess(gvk schema.GroupVersionKind, nn types.Namesp
7290

7391
// pruneError records that the prune of an object failed with an error.
7492
func (r *ApplyResults) pruneError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
93+
r.Objects = append(r.Objects, ObjectStatus{
94+
GVK: gvk,
95+
NameNamespace: nn,
96+
IsHealthy: true,
97+
IsPruned: true,
98+
Message: "Prune Error",
99+
Error: err,
100+
})
75101
r.pruneFailCount++
76102
klog.Warningf("error from pruning on %s %s: %v", gvk, nn, err)
77103
}
78104

79105
// pruneSuccess records that an object was pruned and this succeeded.
80106
func (r *ApplyResults) pruneSuccess(gvk schema.GroupVersionKind, nn types.NamespacedName) {
107+
r.Objects = append(r.Objects, ObjectStatus{
108+
GVK: gvk,
109+
NameNamespace: nn,
110+
IsPruned: true,
111+
})
81112
r.pruneSuccessCount++
82113
}
83114

84115
// reportHealth records the health of an object.
85-
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool) {
116+
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string) {
117+
r.Objects = append(r.Objects, ObjectStatus{
118+
GVK: gvk,
119+
NameNamespace: nn,
120+
IsHealthy: isHealthy,
121+
IsPruned: false,
122+
Message: message,
123+
})
86124
if isHealthy {
87125
r.healthyCount++
88126
} else {

0 commit comments

Comments
 (0)