Skip to content

Commit a8a1382

Browse files
authored
Merge pull request #403 from barney-s/propagate-health-error
propagate error that occurs during health computation
2 parents 2a9fe22 + 4dbcbb2 commit a8a1382

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

applylib/applyset/applyset.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
kubectlapply "sigs.k8s.io/kubebuilder-declarative-pattern/applylib/forked/github.com/kubernetes/kubectl/pkg/cmd/apply"
3737
)
3838

39-
type ComputeHealthCallback func(*unstructured.Unstructured) (bool, string)
39+
type ComputeHealthCallback func(*unstructured.Unstructured) (bool, string, error)
4040

4141
// ApplySet is a set of objects that we want to apply to the cluster.
4242
//
@@ -270,8 +270,8 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
270270
tracker.lastApplied = lastApplied
271271
results.applySuccess(gvk, nn)
272272
message := ""
273-
tracker.isHealthy, message = a.computeHealth(lastApplied)
274-
results.reportHealth(gvk, nn, tracker.isHealthy, message)
273+
tracker.isHealthy, message, err = a.computeHealth(lastApplied)
274+
results.reportHealth(gvk, nn, tracker.isHealthy, message, err)
275275
}
276276

277277
// 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
@@ -25,27 +25,27 @@ import (
2525
)
2626

2727
// IsHealthy reports whether the object should be considered "healthy"
28-
func IsHealthy(u *unstructured.Unstructured) (bool, string) {
28+
func IsHealthy(u *unstructured.Unstructured) (bool, string, error) {
2929
result, err := status.Compute(u)
3030
if err != nil {
3131
klog.Infof("unable to compute condition for %s", humanName(u))
32-
return false, result.Message
32+
return false, result.Message, err
3333
}
3434
switch result.Status {
3535
case status.InProgressStatus:
36-
return false, result.Message
36+
return false, result.Message, nil
3737
case status.FailedStatus:
38-
return false, result.Message
38+
return false, result.Message, nil
3939
case status.TerminatingStatus:
40-
return false, result.Message
40+
return false, result.Message, nil
4141
case status.UnknownStatus:
4242
klog.Warningf("unknown status for %s", humanName(u))
43-
return false, result.Message
43+
return false, result.Message, nil
4444
case status.CurrentStatus:
45-
return true, result.Message
45+
return true, result.Message, nil
4646
default:
4747
klog.Warningf("unknown status value %s", result.Status)
48-
return false, result.Message
48+
return false, result.Message, nil
4949
}
5050
}
5151

applylib/applyset/results.go

+45-17
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ import (
2222
"k8s.io/klog/v2"
2323
)
2424

25+
type HealthInfo struct {
26+
IsHealthy bool
27+
Message string
28+
Error error
29+
}
30+
31+
type ApplyInfo struct {
32+
IsPruned bool
33+
Message string
34+
Error error
35+
}
36+
2537
type ObjectStatus struct {
2638
GVK schema.GroupVersionKind
2739
NameNamespace types.NamespacedName
28-
IsHealthy bool
29-
IsPruned bool
30-
Message string
31-
Error error
40+
Apply ApplyInfo
41+
Health HealthInfo
3242
}
3343

3444
// ApplyResults contains the results of an Apply operation.
@@ -75,10 +85,14 @@ func (r *ApplyResults) applyError(gvk schema.GroupVersionKind, nn types.Namespac
7585
r.Objects = append(r.Objects, ObjectStatus{
7686
GVK: gvk,
7787
NameNamespace: nn,
78-
IsHealthy: false,
79-
IsPruned: false,
80-
Message: "Apply Error",
81-
Error: err,
88+
Health: HealthInfo{
89+
IsHealthy: false,
90+
},
91+
Apply: ApplyInfo{
92+
IsPruned: false,
93+
Message: "Apply Error",
94+
Error: err,
95+
},
8296
})
8397
klog.Warningf("error from apply on %s %s: %v", gvk, nn, err)
8498
}
@@ -93,10 +107,14 @@ func (r *ApplyResults) pruneError(gvk schema.GroupVersionKind, nn types.Namespac
93107
r.Objects = append(r.Objects, ObjectStatus{
94108
GVK: gvk,
95109
NameNamespace: nn,
96-
IsHealthy: true,
97-
IsPruned: true,
98-
Message: "Prune Error",
99-
Error: err,
110+
Health: HealthInfo{
111+
IsHealthy: true,
112+
},
113+
Apply: ApplyInfo{
114+
IsPruned: true,
115+
Message: "Prune Error",
116+
Error: err,
117+
},
100118
})
101119
r.pruneFailCount++
102120
klog.Warningf("error from pruning on %s %s: %v", gvk, nn, err)
@@ -107,19 +125,29 @@ func (r *ApplyResults) pruneSuccess(gvk schema.GroupVersionKind, nn types.Namesp
107125
r.Objects = append(r.Objects, ObjectStatus{
108126
GVK: gvk,
109127
NameNamespace: nn,
110-
IsPruned: true,
128+
Health: HealthInfo{
129+
IsHealthy: true,
130+
},
131+
Apply: ApplyInfo{
132+
IsPruned: true,
133+
},
111134
})
112135
r.pruneSuccessCount++
113136
}
114137

115138
// reportHealth records the health of an object.
116-
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string) {
139+
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string, err error) {
117140
r.Objects = append(r.Objects, ObjectStatus{
118141
GVK: gvk,
119142
NameNamespace: nn,
120-
IsHealthy: isHealthy,
121-
IsPruned: false,
122-
Message: message,
143+
Health: HealthInfo{
144+
IsHealthy: isHealthy,
145+
Message: message,
146+
Error: err,
147+
},
148+
Apply: ApplyInfo{
149+
IsPruned: false,
150+
},
123151
})
124152
if isHealthy {
125153
r.healthyCount++

0 commit comments

Comments
 (0)