Skip to content

Commit 4b2b362

Browse files
timonwongporridge
andauthored
fix: error being silenced during apply (#395)
* fix: error being silenced during apply Signed-off-by: Tianpeng Wang <tpwang@alauda.io> * add test Signed-off-by: Tianpeng Wang <tpwang@alauda.io> * Apply suggestions from code review Co-authored-by: Marcin Owsiany <marcin@owsiany.pl> * fix lint Signed-off-by: Tianpeng Wang <tpwang@alauda.io> * check error status message Signed-off-by: Tianpeng Wang <tpwang@alauda.io> --------- Signed-off-by: Tianpeng Wang <tpwang@alauda.io> Co-authored-by: Marcin Owsiany <marcin@owsiany.pl>
1 parent 1017ef8 commit 4b2b362

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

pkg/reconciler/reconciler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,13 +556,13 @@ type ControllerSetupFunc func(c ControllerSetup) error
556556
// - Deployed - a release for this CR is deployed (but not necessarily ready).
557557
// - ReleaseFailed - an installation or upgrade failed.
558558
// - Irreconcilable - an error occurred during reconciliation
559-
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
559+
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, err error) {
560560
log := r.log.WithValues(strings.ToLower(r.gvk.Kind), req.NamespacedName)
561561
log.V(1).Info("Reconciliation triggered")
562562

563563
obj := &unstructured.Unstructured{}
564564
obj.SetGroupVersionKind(*r.gvk)
565-
err := r.client.Get(ctx, req.NamespacedName, obj)
565+
err = r.client.Get(ctx, req.NamespacedName, obj)
566566
if apierrors.IsNotFound(err) {
567567
log.V(1).Info("Resource %s/%s not found, nothing to do", req.NamespacedName.Namespace, req.NamespacedName.Name)
568568
return ctrl.Result{}, nil

pkg/reconciler/reconciler_test.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,15 @@ var _ = Describe("Reconciler", func() {
738738
Patch: func(ctx context.Context, _ client.WithWatch, fakeObj client.Object, patch client.Patch, opts ...client.PatchOption) error {
739739
return mgr.GetClient().Patch(ctx, fakeObj, patch, opts...)
740740
},
741-
SubResource: func(_ client.WithWatch, subresource string) client.SubResourceClient {
742-
return mgr.GetClient().SubResource(subresource)
741+
SubResourceUpdate: func(ctx context.Context, _ client.Client, subResourceName string, obj client.Object, opts ...client.SubResourceUpdateOption) error {
742+
err := mgr.GetClient().SubResource(subResourceName).Update(ctx, obj, opts...)
743+
if err != nil {
744+
if apierrors.IsConflict(err) {
745+
// workaround https://github.com/kubernetes/kubernetes/issues/124347
746+
return apierrors.NewNotFound(gvk.GroupVersion().WithResource("testapps").GroupResource(), obj.GetName())
747+
}
748+
}
749+
return nil
743750
},
744751
}).WithStatusSubresource(obj).Build()
745752
r.client = cl
@@ -752,6 +759,44 @@ var _ = Describe("Reconciler", func() {
752759
})
753760
})
754761
})
762+
When("errors that were not 'NotFound' occurred while applying CR status", func() {
763+
It("should propagate apply errors to reconciler", func() {
764+
By("configuring a client that will error during apply", func() {
765+
cl := fake.NewClientBuilder().WithObjects(obj).WithInterceptorFuncs(interceptor.Funcs{
766+
Create: func(ctx context.Context, _ client.WithWatch, fakeObj client.Object, opts ...client.CreateOption) error {
767+
return mgr.GetClient().Create(ctx, fakeObj, opts...)
768+
},
769+
Delete: func(ctx context.Context, _ client.WithWatch, fakeObj client.Object, opts ...client.DeleteOption) error {
770+
return mgr.GetClient().Delete(ctx, fakeObj, opts...)
771+
},
772+
DeleteAllOf: func(ctx context.Context, _ client.WithWatch, fakeObj client.Object, opts ...client.DeleteAllOfOption) error {
773+
return mgr.GetClient().DeleteAllOf(ctx, fakeObj, opts...)
774+
},
775+
Update: func(ctx context.Context, _ client.WithWatch, fakeObj client.Object, opts ...client.UpdateOption) error {
776+
return mgr.GetClient().Update(ctx, fakeObj, opts...)
777+
},
778+
Patch: func(ctx context.Context, _ client.WithWatch, fakeObj client.Object, patch client.Patch, opts ...client.PatchOption) error {
779+
return mgr.GetClient().Patch(ctx, fakeObj, patch, opts...)
780+
},
781+
SubResourceUpdate: func(_ context.Context, _ client.Client, _ string, _ client.Object, _ ...client.SubResourceUpdateOption) error {
782+
return apierrors.NewBadRequest("XXXInvalidXXX")
783+
},
784+
}).WithStatusSubresource(obj).Build()
785+
r.client = cl
786+
})
787+
788+
By("propagating the error from status update", func() {
789+
res, err := r.Reconcile(ctx, req)
790+
Expect(res).To(Equal(reconcile.Result{}))
791+
Expect(err).To(HaveOccurred())
792+
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
793+
794+
var statusErr apierrors.APIStatus
795+
Expect(errors.As(err, &statusErr)).To(BeTrue())
796+
Expect(statusErr.Status().Message).To(Equal("XXXInvalidXXX"))
797+
})
798+
})
799+
})
755800
When("CR is deleted, release is not present, but uninstall finalizer exists", func() {
756801
It("removes the finalizer", func() {
757802
By("adding the uninstall finalizer and deleting the CR", func() {

0 commit comments

Comments
 (0)