Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/reconciler/managed/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,20 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (resu
tracker.SetLastHandledReconcileAt(reconcileRequestToken)
}
}
return r.client.Status().Update(ctx, managed)

// An external client that ignores its context can overrun the
// reconcile deadline. Persist the status anyway, with a context of its
// own, so the user can see what happened instead of a resource with no
// conditions.
statusCtx := ctx
if ctx.Err() != nil {
var cancelStatus context.CancelFunc

statusCtx, cancelStatus = context.WithTimeout(context.WithoutCancel(ctx), reconcileGracePeriod)
defer cancelStatus()
}

return r.client.Status().Update(statusCtx, managed)
}

// Check if the ManagementPolicies is set to a non-default value while the
Expand Down
55 changes: 55 additions & 0 deletions pkg/reconciler/managed/reconciler_modern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3404,3 +3404,58 @@ func TestReconcilePropagationFinalizersDoNotDelayDelete(t *testing.T) {
})
}
}

func TestModernReconcilerWritesStatusAfterContextEnds(t *testing.T) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
errBoom := errors.New("boom")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var got resource.Managed

m := &fake.Manager{
Client: &test.MockClient{
MockGet: modernManagedMockGetFn(nil, 42),
MockStatusUpdate: test.MockSubResourceUpdateFn(func(ctx context.Context, obj client.Object, _ ...client.SubResourceUpdateOption) error {
// A real client fails the request when its context has ended.
if err := ctx.Err(); err != nil {
return err
}

got = obj.(resource.Managed)

return nil
}),
},
Scheme: fake.SchemeWith(&fake.ModernManaged{}),
}

r := NewReconciler(m, resource.ManagedKind(fake.GVK(&fake.ModernManaged{})),
WithInitializers(),
WithExternalConnector(ExternalConnectorFn(func(_ context.Context, _ resource.Managed) (ExternalClient, error) {
return &ExternalClientFns{
// An external client that overruns the reconcile deadline.
ObserveFn: func(_ context.Context, _ resource.Managed) (ExternalObservation, error) {
cancel()
return ExternalObservation{}, errBoom
},
DisconnectFn: func(_ context.Context) error { return nil },
}, nil
})),
)

if _, err := r.Reconcile(ctx, reconcile.Request{}); err != nil {
t.Fatalf("r.Reconcile(...): want nil error, got %v", err)
}

if got == nil {
t.Fatal("the status of the managed resource was not written after the reconcile context ended")
}

want := newModernManaged(42)
want.SetConditions(xpv2.ReconcileError(errors.Wrap(errBoom, errReconcileObserve)).WithObservedGeneration(42))

if diff := cmp.Diff(want, got, test.EquateConditions()); diff != "" {
t.Errorf("\n-want, +got:\n%s", diff)
}
}