From 61e57488fe52bb0b22bd1e25def686bbafc48846 Mon Sep 17 00:00:00 2001 From: Fabio Bertinatto Date: Tue, 11 Mar 2025 15:08:56 -0300 Subject: [PATCH] UPSTREAM: 130422: Propagate error when creating CustomResourceStorage instead of panic'ing --- .../pkg/apiserver/customresource_handler.go | 5 ++++- .../pkg/registry/customresource/etcd.go | 6 +++--- .../pkg/registry/customresource/etcd_test.go | 5 ++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go index 1fbe09821b73f..1b549610aa700 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go @@ -812,7 +812,7 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd return nil, fmt.Errorf("the server could not properly serve the list kind") } - storages[v.Name] = customresource.NewStorage( + storages[v.Name], err = customresource.NewStorage( resource.GroupResource(), singularResource.GroupResource(), kind, @@ -841,6 +841,9 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd table, replicasPathInCustomResource, ) + if err != nil { + return nil, err + } clusterScoped := crd.Spec.Scope == apiextensionsv1.ClusterScoped diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd.go index ecff5533df461..edc89bb5f833f 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd.go @@ -41,7 +41,7 @@ type CustomResourceStorage struct { Scale *ScaleREST } -func NewStorage(resource schema.GroupResource, singularResource schema.GroupResource, kind, listKind schema.GroupVersionKind, strategy customResourceStrategy, optsGetter generic.RESTOptionsGetter, categories []string, tableConvertor rest.TableConvertor, replicasPathMapping managedfields.ResourcePathMappings) CustomResourceStorage { +func NewStorage(resource schema.GroupResource, singularResource schema.GroupResource, kind, listKind schema.GroupVersionKind, strategy customResourceStrategy, optsGetter generic.RESTOptionsGetter, categories []string, tableConvertor rest.TableConvertor, replicasPathMapping managedfields.ResourcePathMappings) (CustomResourceStorage, error) { var storage CustomResourceStorage store := &genericregistry.Store{ NewFunc: func() runtime.Object { @@ -69,7 +69,7 @@ func NewStorage(resource schema.GroupResource, singularResource schema.GroupReso } options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: strategy.GetAttrs} if err := store.CompleteWithOptions(options); err != nil { - panic(err) // TODO: Propagate error up + return storage, fmt.Errorf("failed to update store with options: %w", err) } storage.CustomResource = &REST{store, categories} @@ -97,7 +97,7 @@ func NewStorage(resource schema.GroupResource, singularResource schema.GroupReso } } - return storage + return storage, nil } // REST implements a RESTStorage for API services against etcd diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd_test.go index 2ccdea5233f28..c64fc4bbf9458 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd_test.go @@ -92,7 +92,7 @@ func newStorage(t *testing.T) (customresource.CustomResourceStorage, *etcd3testi } table, _ := tableconvertor.New(headers, schema.GroupVersionKind{Group: "mygroup.example.com", Version: "v1beta1", Kind: "NoxuItemList"}) - storage := customresource.NewStorage( + storage, err := customresource.NewStorage( groupResource, groupResource, kind, @@ -113,6 +113,9 @@ func newStorage(t *testing.T) (customresource.CustomResourceStorage, *etcd3testi table, managedfields.ResourcePathMappings{}, ) + if err != nil { + t.Errorf("unexpected error: %v", err) + } return storage, server }