diff --git a/pkg/registry/generic/options.go b/pkg/registry/generic/options.go index 72b582c3b..d675a258f 100644 --- a/pkg/registry/generic/options.go +++ b/pkg/registry/generic/options.go @@ -26,9 +26,9 @@ import ( "k8s.io/client-go/tools/cache" ) -// RESTOptions is set of configuration options to generic registries. +// RESTOptions is set of resource-specific configuration options to generic registries. type RESTOptions struct { - StorageConfig *storagebackend.Config + StorageConfig *storagebackend.ConfigForResource Decorator StorageDecorator EnableGarbageCollection bool diff --git a/pkg/registry/generic/registry/dryrun_test.go b/pkg/registry/generic/registry/dryrun_test.go index f725b5c87..93bb34aae 100644 --- a/pkg/registry/generic/registry/dryrun_test.go +++ b/pkg/registry/generic/registry/dryrun_test.go @@ -27,6 +27,7 @@ import ( "k8s.io/apimachinery/pkg/api/apitesting" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" examplev1 "k8s.io/apiserver/pkg/apis/example/v1" "k8s.io/apiserver/pkg/registry/rest" @@ -38,7 +39,7 @@ import ( func NewDryRunnableTestStorage(t *testing.T) (DryRunnableStorage, func()) { server, sc := etcd3testing.NewUnsecuredEtcd3TestClientServer(t) sc.Codec = apitesting.TestStorageCodec(codecs, examplev1.SchemeGroupVersion) - s, destroy, err := factory.Create(*sc, nil) + s, destroy, err := factory.Create(*sc.ForResource(schema.GroupResource{Resource: "pods"}), nil) if err != nil { t.Fatalf("Error creating storage: %v", err) } diff --git a/pkg/registry/generic/registry/storage_factory.go b/pkg/registry/generic/registry/storage_factory.go index f2fa59723..6a4426ee6 100644 --- a/pkg/registry/generic/registry/storage_factory.go +++ b/pkg/registry/generic/registry/storage_factory.go @@ -36,7 +36,7 @@ import ( // Creates a cacher based given storageConfig. func StorageWithCacher() generic.StorageDecorator { return func( - storageConfig *storagebackend.Config, + storageConfig *storagebackend.ConfigForResource, resourcePrefix string, keyFunc func(obj runtime.Object) (string, error), newFunc func() runtime.Object, diff --git a/pkg/registry/generic/registry/store_test.go b/pkg/registry/generic/registry/store_test.go index f946a3894..f3ec927f4 100644 --- a/pkg/registry/generic/registry/store_test.go +++ b/pkg/registry/generic/registry/store_test.go @@ -2230,7 +2230,7 @@ func newTestGenericStoreRegistry(t *testing.T, scheme *runtime.Scheme, hasCacheE newListFunc := func() runtime.Object { return &example.PodList{} } sc.Codec = apitesting.TestStorageCodec(codecs, examplev1.SchemeGroupVersion) - s, dFunc, err := factory.Create(*sc, newFunc) + s, dFunc, err := factory.Create(*sc.ForResource(schema.GroupResource{Resource: "pods"}), newFunc) if err != nil { t.Fatalf("Error creating storage: %v", err) } diff --git a/pkg/registry/generic/storage_decorator.go b/pkg/registry/generic/storage_decorator.go index e0ca2df04..715aa1047 100644 --- a/pkg/registry/generic/storage_decorator.go +++ b/pkg/registry/generic/storage_decorator.go @@ -27,7 +27,7 @@ import ( // StorageDecorator is a function signature for producing a storage.Interface // and an associated DestroyFunc from given parameters. type StorageDecorator func( - config *storagebackend.Config, + config *storagebackend.ConfigForResource, resourcePrefix string, keyFunc func(obj runtime.Object) (string, error), newFunc func() runtime.Object, @@ -39,7 +39,7 @@ type StorageDecorator func( // UndecoratedStorage returns the given a new storage from the given config // without any decoration. func UndecoratedStorage( - config *storagebackend.Config, + config *storagebackend.ConfigForResource, resourcePrefix string, keyFunc func(obj runtime.Object) (string, error), newFunc func() runtime.Object, @@ -53,6 +53,6 @@ func UndecoratedStorage( // NewRawStorage creates the low level kv storage. This is a work-around for current // two layer of same storage interface. // TODO: Once cacher is enabled on all registries (event registry is special), we will remove this method. -func NewRawStorage(config *storagebackend.Config, newFunc func() runtime.Object) (storage.Interface, factory.DestroyFunc, error) { +func NewRawStorage(config *storagebackend.ConfigForResource, newFunc func() runtime.Object) (storage.Interface, factory.DestroyFunc, error) { return factory.Create(*config, newFunc) } diff --git a/pkg/server/options/etcd.go b/pkg/server/options/etcd.go index faab00719..dfadadbca 100644 --- a/pkg/server/options/etcd.go +++ b/pkg/server/options/etcd.go @@ -255,7 +255,7 @@ type SimpleRestOptionsFactory struct { func (f *SimpleRestOptionsFactory) GetRESTOptions(resource schema.GroupResource) (generic.RESTOptions, error) { ret := generic.RESTOptions{ - StorageConfig: &f.Options.StorageConfig, + StorageConfig: f.Options.StorageConfig.ForResource(resource), Decorator: generic.UndecoratedStorage, EnableGarbageCollection: f.Options.EnableGarbageCollection, DeleteCollectionWorkers: f.Options.DeleteCollectionWorkers, diff --git a/pkg/server/storage/storage_factory.go b/pkg/server/storage/storage_factory.go index 689b51322..3b8c71de1 100644 --- a/pkg/server/storage/storage_factory.go +++ b/pkg/server/storage/storage_factory.go @@ -46,7 +46,7 @@ type Backend struct { type StorageFactory interface { // New finds the storage destination for the given group and resource. It will // return an error if the group has no storage destination configured. - NewConfig(groupResource schema.GroupResource) (*storagebackend.Config, error) + NewConfig(groupResource schema.GroupResource) (*storagebackend.ConfigForResource, error) // ResourcePrefix returns the overridden resource prefix for the GroupResource // This allows for cohabitation of resources with different native types and provides @@ -250,7 +250,7 @@ func (s *DefaultStorageFactory) getStorageGroupResource(groupResource schema.Gro // New finds the storage destination for the given group and resource. It will // return an error if the group has no storage destination configured. -func (s *DefaultStorageFactory) NewConfig(groupResource schema.GroupResource) (*storagebackend.Config, error) { +func (s *DefaultStorageFactory) NewConfig(groupResource schema.GroupResource) (*storagebackend.ConfigForResource, error) { chosenStorageResource := s.getStorageGroupResource(groupResource) // operate on copy @@ -284,7 +284,7 @@ func (s *DefaultStorageFactory) NewConfig(groupResource schema.GroupResource) (* } klog.V(3).Infof("storing %v in %v, reading as %v from %#v", groupResource, codecConfig.StorageVersion, codecConfig.MemoryVersion, codecConfig.Config) - return &storageConfig, nil + return storageConfig.ForResource(groupResource), nil } // Backends returns all backends for all registered storage destinations. diff --git a/pkg/storage/storagebackend/config.go b/pkg/storage/storagebackend/config.go index df08e4077..aa4163877 100644 --- a/pkg/storage/storagebackend/config.go +++ b/pkg/storage/storagebackend/config.go @@ -21,6 +21,7 @@ import ( "go.opentelemetry.io/otel/trace" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/server/egressselector" "k8s.io/apiserver/pkg/storage/etcd3" "k8s.io/apiserver/pkg/storage/value" @@ -91,6 +92,23 @@ type Config struct { StorageObjectCountTracker flowcontrolrequest.StorageObjectCountTracker } +// ConfigForResource is a Config specialized to a particular `schema.GroupResource` +type ConfigForResource struct { + // Config is the resource-independent configuration + Config + + // GroupResource is the relevant one + GroupResource schema.GroupResource +} + +// ForResource specializes to the given resource +func (config *Config) ForResource(resource schema.GroupResource) *ConfigForResource { + return &ConfigForResource{ + Config: *config, + GroupResource: resource, + } +} + func NewDefaultConfig(prefix string, codec runtime.Codec) *Config { return &Config{ Paging: true, diff --git a/pkg/storage/storagebackend/factory/etcd3.go b/pkg/storage/storagebackend/factory/etcd3.go index 74ebea655..1d0b103c7 100644 --- a/pkg/storage/storagebackend/factory/etcd3.go +++ b/pkg/storage/storagebackend/factory/etcd3.go @@ -244,7 +244,7 @@ func startCompactorOnce(c storagebackend.TransportConfig, interval time.Duration }, nil } -func newETCD3Storage(c storagebackend.Config, newFunc func() runtime.Object) (storage.Interface, DestroyFunc, error) { +func newETCD3Storage(c storagebackend.ConfigForResource, newFunc func() runtime.Object) (storage.Interface, DestroyFunc, error) { stopCompactor, err := startCompactorOnce(c.Transport, c.CompactionInterval) if err != nil { return nil, nil, err diff --git a/pkg/storage/storagebackend/factory/factory.go b/pkg/storage/storagebackend/factory/factory.go index fb1e2d289..68c45a18f 100644 --- a/pkg/storage/storagebackend/factory/factory.go +++ b/pkg/storage/storagebackend/factory/factory.go @@ -28,7 +28,7 @@ import ( type DestroyFunc func() // Create creates a storage backend based on given config. -func Create(c storagebackend.Config, newFunc func() runtime.Object) (storage.Interface, DestroyFunc, error) { +func Create(c storagebackend.ConfigForResource, newFunc func() runtime.Object) (storage.Interface, DestroyFunc, error) { switch c.Type { case storagebackend.StorageTypeETCD2: return nil, nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type) diff --git a/pkg/storage/storagebackend/factory/tls_test.go b/pkg/storage/storagebackend/factory/tls_test.go index 6e831c496..86d9cd1e9 100644 --- a/pkg/storage/storagebackend/factory/tls_test.go +++ b/pkg/storage/storagebackend/factory/tls_test.go @@ -29,6 +29,7 @@ import ( apitesting "k8s.io/apimachinery/pkg/api/apitesting" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apiserver/pkg/apis/example" @@ -78,7 +79,7 @@ func TestTLSConnection(t *testing.T) { }, Codec: codec, } - storage, destroyFunc, err := newETCD3Storage(cfg, nil) + storage, destroyFunc, err := newETCD3Storage(*cfg.ForResource(schema.GroupResource{Resource: "pods"}), nil) defer destroyFunc() if err != nil { t.Fatal(err)