Skip to content

fix(substrate): degrade gracefully when ate.dev CRDs are absent#2023

Open
QuentinBisson wants to merge 7 commits into
kagent-dev:mainfrom
QuentinBisson:fix/substrate-status-no-match-error
Open

fix(substrate): degrade gracefully when ate.dev CRDs are absent#2023
QuentinBisson wants to merge 7 commits into
kagent-dev:mainfrom
QuentinBisson:fix/substrate-status-no-match-error

Conversation

@QuentinBisson

Copy link
Copy Markdown
Contributor

What

GET /api/substrate/status returns 500 on clusters that don't have the
agent-substrate CRDs installed (ate.dev/v1alpha1 WorkerPool / ActorTemplate).

listSubstrateCRs calls KubeClient.List on both types unconditionally. When the
CRDs are absent the REST mapper returns *meta.NoKindMatchError, which propagates as
HTTP 500. Since substrate is optional and Enabled is already gated on AteClient != nil, the correct behaviour is to return an empty list.

Fix

Guard both List calls with meta.IsNoMatchError and return nil, nil, nil (empty
result, no error) so the handler responds 200 with Enabled: false and empty slices.

A regression test (TestHandleGetSubstrateStatus_NoCRDs) covers the no-CRD path using
a stub client that returns *meta.NoKindMatchError, asserting HTTP 200 with
Enabled: false and empty resource slices.

Reported in giantswarm/giantswarm#36845.

GET /api/substrate/status returned 500 on clusters without the ate.dev CRDs
installed because listSubstrateCRs listed WorkerPool/ActorTemplate
unconditionally, propagating the REST mapper NoKindMatchError as a server
error.

Gate the CRD listing loop on AteClient != nil. When substrate is not
configured (the common case), there is nothing to list and no CRD calls
are made. When substrate is configured, a missing CRD is a legitimate
misconfiguration and the error is surfaced as before.

Reported in giantswarm/giantswarm#36845.

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
@QuentinBisson QuentinBisson force-pushed the fix/substrate-status-no-match-error branch from a96374e to d32223c Compare June 15, 2026 22:04
@QuentinBisson QuentinBisson marked this pull request as ready for review June 15, 2026 22:06
Copilot AI review requested due to automatic review settings June 15, 2026 22:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates the substrate status endpoint to avoid listing Kubernetes substrate CRs when the substrate isn’t configured (i.e., AteClient is nil), and adds a regression test for that behavior.

Changes:

  • Guard Kubernetes CR listing behind h.AteClient != nil in HandleGetSubstrateStatus.
  • Add a new test asserting the “substrate not configured” response shape.
  • Introduce a minimal client.Client stub intended to simulate missing CRDs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
go/core/internal/httpserver/handlers/substrate.go Skips listing substrate CRs unless AteClient is configured.
go/core/internal/httpserver/handlers/substrate_test.go Adds a “not configured” test and a kube client stub for simulating missing CRDs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +28 to +49
// noMatchKubeClient is a minimal client.Client stub whose List always returns
// a *meta.NoKindMatchError, simulating a cluster where the ate.dev CRDs are absent.
type noMatchKubeClient struct {
client.Client
}

func (noMatchKubeClient) List(_ context.Context, _ client.ObjectList, _ ...client.ListOption) error {
return &apimeta.NoKindMatchError{}
}

// TestHandleGetSubstrateStatus_SubstrateNotConfigured verifies that when AteClient is nil
// (substrate not configured), the endpoint returns 200 with Enabled:false and empty slices
// without making any CRD List calls.
func TestHandleGetSubstrateStatus_SubstrateNotConfigured(t *testing.T) {
t.Parallel()

scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()

base := &handlers.Base{KubeClient: fakeClient, Authorizer: &auth.NoopAuthorizer{}}
h := handlers.NewSubstrateHandler(base, nil)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines 65 to 78
if h.AteClient != nil {
for _, ns := range namespaces {
wpEntries, tmplEntries, err := h.listSubstrateCRs(r.Context(), ns)
if err != nil {
log.Error(err, "list substrate CRs", "namespace", ns)
w.RespondWithError(errors.NewInternalServerError("Failed to list substrate resources from Kubernetes", err))
return
}
resp.WorkerPools = append(resp.WorkerPools, wpEntries...)
resp.ActorTemplates = append(resp.ActorTemplates, tmplEntries...)
}
resp.WorkerPools = append(resp.WorkerPools, wpEntries...)
resp.ActorTemplates = append(resp.ActorTemplates, tmplEntries...)
}

if h.AteClient != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +65 to 68
}


type stubAteControl struct {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

QuentinBisson and others added 3 commits June 15, 2026 23:09
- Merge the two consecutive AteClient != nil blocks into one
- Use noMatchKubeClient in the not-configured test so the test
  would fail if the gate were removed (the error would propagate
  as 500 rather than the guarded nil path)
- Remove extra blank line before stubAteControl

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
EItanya
EItanya previously approved these changes Jun 17, 2026
@EItanya

EItanya commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

We can merge this as is, but I think we should probably come up with a better long-term solution here. If a user doesn't have substrate installed, why is this even being called at all?

@QuentinBisson

Copy link
Copy Markdown
Contributor Author

Sure but then kagent either needs to detect it or have a flag to disable substrate :)

@EItanya

EItanya commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Sure but then kagent either needs to detect it or have a flag to disable substrate :)

Maybe we could do a discovery client check at the beginning for the CRDs and use that as the marker?

@QuentinBisson

QuentinBisson commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

You're right. We can use the dynamic RESTMapper instead as that reloads on NoMatch. so if I just guard the List with meta.IsNoMatchError the endpoint picks up substrate installed after
kagent starts with no restart, and I avoid a second call that can disagree with the List.

@QuentinBisson

QuentinBisson commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

I updated the PR to:

  • Guard both KubeClient.List calls with apimeta.IsNoMatchError (return empty + found=false, no error).
  • Restore listSubstrateCRs to run unconditionally (it was incorrectly gated on AteClient != nil).
  • Derive Enabled from what actually answered the request: crdsPresent || AteClient != nil. This keeps the two axes (CRDs installed / ate-api gRPC reachable) independent.

Three additional test cases cover the matrix: no CRDs+no client, no CRDs+client set, CRDs present+no client.

… NoMatchError

CRD presence (WorkerPool/ActorTemplate) and gRPC client availability (AteClient)
are independent axes. Gating listSubstrateCRs on AteClient != nil caused two bugs:

- CRDs present but ate-api not deployed: WorkerPools/ActorTemplates silently omitted
  even though the CRs exist, Enabled reported false.
- CRDs absent but AteClient set: List calls reached the API server and 500'd instead
  of degrading gracefully.

Guard each KubeClient.List with meta.IsNoMatchError: on NoMatch return empty + found=false
with no error. controller-runtime's dynamic RESTMapper reloads on NoMatch automatically,
so substrate installed after kagent boots is picked up on the next request without a
restart.

Compute Enabled from observed reality: crdsPresent || AteClient != nil.

Three new cases covered by tests:
- NoCRDs + no gRPC client  -> 200, Enabled:false
- NoCRDs + gRPC client set -> 200, Enabled:true, actors populated
- CRDs present + no gRPC   -> 200, Enabled:true, WorkerPools populated

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
@QuentinBisson QuentinBisson force-pushed the fix/substrate-status-no-match-error branch from 690b3a4 to 149a2d7 Compare June 17, 2026 21:38
@EItanya

EItanya commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Sorry, I should've been more clear. I actually liked your first implementation. What I meant was to have a check on startup, I think this is a bit overcomplicated for the situation.

Let's start with the original path and then we can improve later

Revert 149a2d7. The IsNoMatchError guard is overcomplicated for this
use case. Keeping the original approach (gate CRD listing on AteClient
!= nil) and will improve with a startup CRD discovery check separately.

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
@QuentinBisson

Copy link
Copy Markdown
Contributor Author

I reverted the new commit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants