[kube-client] fix: handle typed nil discovery results - #43
Merged
Merged
Conversation
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
There was a problem hiding this comment.
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 inapiResourceList()to convert(*apiResourceListResult)(nil)into a truenilinterface value. - Add a regression test reproducing the typed-nil discovery error scenario to ensure
APIResourceListno 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.
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
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Fixes a nil pointer dereference panic in
APIResourceListthat occurred when the discovery backend returned an error with anilresult (e.g. for an API group that isn't yet registered, such ascustom.metrics.k8s.iobefore the metrics adapter is deployed).What this PR does / why we need it
Go's typed nil trap:
apiResourceListUncachedreturns (*apiResourceListResult,error). When it returns(nil, err), the nil is a typed nil — a*apiResourceListResultwith valuenil. When this typed nil is stored in ananyinterface (assingleflight.Dodoes), the interface itself is not nil — it holds a type descriptor. As a result, the existingv == nilguard afterDo()was bypassed, and the subsequent type assertion v.(*apiResourceListResult).lists dereferenced the nil pointer and panicked.Fix: wrap the
singleflight.Dobody to explicitly convert a typed nil result into an untypednilbefore it's stored in theanyinterface, 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.iobefore metrics-adapter is running), causingServerResourcesForGroupVersionto returnnil, err.A regression test (
TestAPIResourceListTypedNilNoPanic) is added that panicked before the fix and passes after.