Skip to content

[kube-client] fix: handle typed nil discovery results - #43

Merged
ldmonster merged 5 commits into
masterfrom
fix/typed-nil-panic-in-api-resource-list
Jun 30, 2026
Merged

ldmonster merged 5 commits into
masterfrom
fix/typed-nil-panic-in-api-resource-list

Conversation

@fuldaxxx

@fuldaxxx fuldaxxx commented Jun 29, 2026

Copy link
Copy Markdown
Member

Overview

Fixes a nil pointer dereference panic in APIResourceList that occurred when the discovery backend returned an error with a nil result (e.g. for an API group that isn't yet registered, such as custom.metrics.k8s.io before the metrics adapter is deployed).

What this PR does / why we need it

Go's typed nil trap: apiResourceListUncached returns (*apiResourceListResult, error). When it returns (nil, err), the nil is a typed nil — a *apiResourceListResult with value nil. When this typed nil is stored in an any interface (as singleflight.Do does), the interface itself is not nil — it holds a type descriptor. As a result, the existing v == nil guard after Do() was bypassed, and the subsequent type assertion v.(*apiResourceListResult).lists dereferenced the nil pointer and panicked.

Fix: wrap the singleflight.Do body to explicitly convert a typed nil result into an untyped nil before it's stored in the any interface, so the guard works as intended.

Reproduces in practice when a Kubernetes cluster has API groups registered in discovery but no server backing them (e.g. custom.metrics.k8s.io before metrics-adapter is running), causing ServerResourcesForGroupVersion to return nil, err.

A regression test (TestAPIResourceListTypedNilNoPanic) is added that panicked before the fix and passes after.

Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
@fuldaxxx
fuldaxxx requested a review from ldmonster June 29, 2026 16:26
@fuldaxxx fuldaxxx self-assigned this Jun 29, 2026
@fuldaxxx fuldaxxx added bug Something isn't working go Pull requests that update Go code labels Jun 29, 2026
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
@fuldaxxx
fuldaxxx marked this pull request as ready for review June 29, 2026 16:38
@ldmonster
ldmonster requested a review from Copilot June 29, 2026 16:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a nil-pointer panic in the client’s discovery path when singleflight.Do() returns an any containing a typed-nil *apiResourceListResult, by ensuring typed-nil results are converted to an untyped nil before being stored in the interface.

Changes:

  • Wrap singleflight.Do() callback in apiResourceList() to convert (*apiResourceListResult)(nil) into a true nil interface value.
  • Add a regression test reproducing the typed-nil discovery error scenario to ensure APIResourceList no longer panics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
client/client.go Converts typed-nil discovery results to untyped nil before returning from singleflight.Do() to prevent nil-deref panics.
client/client_concurrent_test.go Adds a regression test for the typed-nil discovery failure case to verify APIResourceList doesn’t panic.

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

Comment thread client/client_concurrent_test.go Outdated
fuldaxxx added 3 commits June 29, 2026 19:57
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
@ldmonster
ldmonster merged commit 0e2c912 into master Jun 30, 2026
3 checks passed
@ldmonster
ldmonster deleted the fix/typed-nil-panic-in-api-resource-list branch June 30, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants