Skip to content

Commit b306353

Browse files
Fix empty errors in logs when a reconciled resource is not ready
`NotReadyError` ensures that its error message is not empty. The initial issue was that when the app droplet is not set, the error gets its reson set to `DropletNotAssigned`, but the reason is not reflected in the error message. This commit includes the reason, and ensures that the message is at least `resource not ready` While being here, do not requeue reconcile when the app droplet is not set in its spec. It makes no sense to requeue the reconcile event as it is expected that the droplet is set on the spec externally. When that happens the app would be reconciled anyway fixes #3862
1 parent f67e778 commit b306353

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

controllers/controllers/workloads/apps/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (r *Reconciler) ReconcileResource(ctx context.Context, cfApp *korifiv1alpha
146146
cfApp.Status.VCAPServicesSecretName = secretName
147147

148148
if cfApp.Spec.CurrentDropletRef.Name == "" {
149-
return ctrl.Result{}, k8s.NewNotReadyError().WithReason("DropletNotAssigned")
149+
return ctrl.Result{}, k8s.NewNotReadyError().WithReason("DropletNotAssigned").WithNoRequeue()
150150
}
151151

152152
droplet, err := r.getDroplet(ctx, cfApp)

kpack-image-builder/controllers/builderinfo_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ var _ = Describe("BuilderInfoReconciler", func() {
171171
g.Expect(readyCondition).NotTo(BeNil())
172172
g.Expect(readyCondition.Status).To(Equal(metav1.ConditionFalse))
173173
g.Expect(readyCondition.Reason).To(Equal("ClusterBuilderNotReady"))
174-
g.Expect(readyCondition.Message).To(Equal(fmt.Sprintf("ClusterBuilder %q is not ready: something happened", clusterBuilderName)))
174+
g.Expect(readyCondition.Message).To(ContainSubstring(fmt.Sprintf("ClusterBuilder %q is not ready: something happened", clusterBuilderName)))
175175
g.Expect(readyCondition.ObservedGeneration).To(Equal(info.Generation))
176176
}).Should(Succeed())
177177
})

tools/k8s/reconcile.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ func (r *PatchingReconciler[T]) Reconcile(ctx context.Context, req ctrl.Request)
7373

7474
var notReadyErr NotReadyError
7575
if errors.As(delegateErr, &notReadyErr) {
76-
reason := notReadyErr.reason
77-
if reason == "" {
78-
reason = "Unknown"
79-
}
80-
81-
readyConditionBuilder.WithReason(reason).WithMessage(notReadyErr.message)
76+
readyConditionBuilder.WithReason(tools.IfZero(notReadyErr.reason, "Unknown")).WithMessage(notReadyErr.message)
8277

8378
if notReadyErr.noRequeue {
8479
result = ctrl.Result{}
@@ -118,16 +113,21 @@ type NotReadyError struct {
118113
}
119114

120115
func (e NotReadyError) Error() string {
121-
if e.cause == nil {
122-
return e.message
116+
composedMessage := "resource not ready"
117+
118+
if e.reason != "" {
119+
composedMessage = composedMessage + ": " + e.reason
120+
}
121+
122+
if e.message != "" {
123+
composedMessage = composedMessage + ": " + e.message
123124
}
124125

125-
message := e.message
126-
if message != "" {
127-
message = message + ": "
126+
if e.cause != nil {
127+
composedMessage = composedMessage + ": " + e.cause.Error()
128128
}
129129

130-
return fmt.Sprintf("%s%s", message, e.cause.Error())
130+
return composedMessage
131131
}
132132

133133
func NewNotReadyError() NotReadyError {

tools/k8s/reconcile_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,14 @@ var _ = Describe("Reconcile", func() {
233233
HasType(Equal(korifiv1alpha1.StatusConditionReady)),
234234
HasStatus(Equal(metav1.ConditionFalse)),
235235
HasReason(Equal("Unknown")),
236-
HasMessage(BeEmpty()),
236+
HasMessage(ContainSubstring("resource not ready")),
237237
)))
238238
})
239239

240+
It("returns a non-empty error", func() {
241+
Expect(err.Error()).To(ContainSubstring("resource not ready"))
242+
})
243+
240244
When("reason is specified", func() {
241245
BeforeEach(func() {
242246
objectReconciler.reconcileResourceError = k8s.NewNotReadyError().WithReason("TestReason")
@@ -270,7 +274,7 @@ var _ = Describe("Reconcile", func() {
270274
Expect(updatedOrg.Status.Conditions).To(ContainElement(SatisfyAll(
271275
HasType(Equal(korifiv1alpha1.StatusConditionReady)),
272276
HasStatus(Equal(metav1.ConditionFalse)),
273-
HasMessage(Equal("TestMessage")),
277+
HasMessage(ContainSubstring("TestMessage")),
274278
)))
275279
})
276280
})
@@ -289,7 +293,7 @@ var _ = Describe("Reconcile", func() {
289293
Expect(updatedOrg.Status.Conditions).To(ContainElement(SatisfyAll(
290294
HasType(Equal(korifiv1alpha1.StatusConditionReady)),
291295
HasStatus(Equal(metav1.ConditionFalse)),
292-
HasMessage(Equal("test-err")),
296+
HasMessage(ContainSubstring(("test-err"))),
293297
)))
294298
})
295299
})
@@ -310,7 +314,7 @@ var _ = Describe("Reconcile", func() {
310314
Expect(updatedOrg.Status.Conditions).To(ContainElement(SatisfyAll(
311315
HasType(Equal(korifiv1alpha1.StatusConditionReady)),
312316
HasStatus(Equal(metav1.ConditionFalse)),
313-
HasMessage(Equal("TestMessage: test-err")),
317+
HasMessage(ContainSubstring("TestMessage: test-err")),
314318
)))
315319
})
316320
})

0 commit comments

Comments
 (0)