Skip to content

Commit dbcffa3

Browse files
authored
fix bug in status updater that could accidentally hide reconciliation errors (#54)
1 parent fa65fd0 commit dbcffa3

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pkg/controller/status_updater.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,23 @@ func defaultPhaseUpdateFunc[Obj client.Object, PhType ~string, ConType comparabl
177177
// The object is expected to be a pointer to a struct with the status field.
178178
// If the 'Object' field in the ReconcileResult is nil, the status update becomes a no-op.
179179
func (s *statusUpdater[Obj, PhType, ConType]) UpdateStatus(ctx context.Context, c client.Client, rr ReconcileResult[Obj, ConType]) (ctrl.Result, error) {
180+
errs := errors.NewReasonableErrorList(rr.ReconcileError)
180181
if IsNil(rr.Object) {
181-
return rr.Result, nil
182+
return rr.Result, errs.Aggregate()
182183
}
183184
if s.fieldNames[STATUS_FIELD] == "" {
184-
return rr.Result, nil
185+
return rr.Result, errs.Aggregate()
185186
}
186187
if IsNil(rr.OldObject) || IsSameObject(rr.OldObject, rr.Object) {
187188
// create old object based on given one
188189
rr.OldObject = rr.Object.DeepCopyObject().(Obj)
189190
}
190191
status := GetField(rr.Object, s.fieldNames[STATUS_FIELD], true)
191192
if IsNil(status) {
192-
return rr.Result, fmt.Errorf("unable to get pointer to status field '%s' of object %T", s.fieldNames[STATUS_FIELD], rr.Object)
193+
errs.Append(errors.WithReason(fmt.Errorf("unable to get pointer to status field '%s' of object %T", s.fieldNames[STATUS_FIELD], rr.Object), "InternalError"))
194+
return rr.Result, errs.Aggregate()
193195
}
194196

195-
errs := errors.NewReasonableErrorList(rr.ReconcileError)
196197
now := time.Now()
197198
if s.fieldNames[STATUS_FIELD_LAST_RECONCILE_TIME] != "" {
198199
SetField(status, s.fieldNames[STATUS_FIELD_LAST_RECONCILE_TIME], metav1.NewTime(now))

pkg/controller/status_updater_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ var _ = Describe("Status Updater", func() {
6464
))
6565
})
6666

67+
It("should not hide a reconciliation error if the object is nil", func() {
68+
env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build()
69+
rr := controller.ReconcileResult[*CustomObject, ConditionStatus]{
70+
ReconcileError: errors.WithReason(fmt.Errorf("test error"), "TestError"),
71+
}
72+
su := preconfiguredStatusUpdaterBuilder().Build()
73+
_, err := su.UpdateStatus(env.Ctx, env.Client(), rr)
74+
Expect(err).To(HaveOccurred())
75+
})
76+
6777
It("should update an existing status", func() {
6878
env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build()
6979
obj := &CustomObject{}

0 commit comments

Comments
 (0)