restmapper + discovery: early return in context-aware APIs

The rationale for explicit cancellation checks is that these functions have
side effects that can be avoided when checking upfront. These side effects can
still occur when cancellation happens while the functions are executing, but
"already canceled" is common enough that it deserves special handling.

Kubernetes-commit: c3f63fc258b73e487da7ca84c40be43fc4c5f4dd
This commit is contained in:
Patrick Ohly
2026-06-30 10:20:31 +02:00
committed by Kubernetes Publisher
parent 0ff074ca35
commit 07eaa8b369
4 changed files with 24 additions and 0 deletions

View File

@@ -169,6 +169,10 @@ func (d *memCacheClient) GroupsAndMaybeResources() (*metav1.APIGroupList, map[sc
// to resources. The returned groups will never be nil, but the resources map can be nil
// if there are no cached resources.
func (d *memCacheClient) GroupsAndMaybeResourcesWithContext(ctx context.Context) (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList, map[schema.GroupVersion]error, error) {
if err := ctx.Err(); err != nil {
return nil, nil, nil, err
}
d.lock.Lock()
defer d.lock.Unlock()

View File

@@ -607,6 +607,10 @@ func (d *DiscoveryClient) GroupsAndMaybeResourcesWithContext(ctx context.Context
map[schema.GroupVersion]*metav1.APIResourceList,
map[schema.GroupVersion]error,
error) {
if err := ctx.Err(); err != nil {
return nil, nil, nil, err
}
// Legacy group ordered first (there is only one -- core/v1 group). Returned groups must
// be non-nil, but it could be empty. Returned resources, apiResources map could be nil.
groups, resources, failedGVs, err := d.downloadLegacy(ctx)
@@ -887,6 +891,10 @@ func ServerGroupsAndResources(d DiscoveryInterface) ([]*metav1.APIGroup, []*meta
}
func ServerGroupsAndResourcesWithContext(ctx context.Context, d DiscoveryInterfaceWithContext) ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
if err := ctx.Err(); err != nil {
return nil, nil, err
}
var sgs *metav1.APIGroupList
var resources []*metav1.APIResourceList
var failedGVs map[schema.GroupVersion]error
@@ -960,6 +968,10 @@ func ServerPreferredResources(d DiscoveryInterface) ([]*metav1.APIResourceList,
// ServerPreferredResourcesWithContext uses the provided discovery interface to look up preferred resources
func ServerPreferredResourcesWithContext(ctx context.Context, d DiscoveryInterfaceWithContext) ([]*metav1.APIResourceList, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
var serverGroupList *metav1.APIGroupList
var failedGroups map[schema.GroupVersion]error
var groupVersionResources map[schema.GroupVersion]*metav1.APIResourceList

View File

@@ -70,6 +70,10 @@ func (c *Client) Paths() (map[string]openapi.GroupVersion, error) {
}
func (c *Client) PathsWithContext(ctx context.Context) (map[string]openapi.GroupVersionWithContext, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
c.once.Do(func() {
uncached, err := c.delegate.PathsWithContext(ctx)
if err != nil {

View File

@@ -55,6 +55,10 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) {
}
func (g *groupversion) SchemaWithContext(ctx context.Context, contentType string) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
g.lock.Lock()
defer g.lock.Unlock()