From afe464bc209fa73227311e1039543f6d8771ab02 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 26 Sep 2024 16:26:45 +0300 Subject: [PATCH] K8s/Storage: add a reusable generic storage builder (#93778) --- pkg/apiserver/registry/generic/storage.go | 31 +++++++++++ .../notifications/receiver/storage.go | 18 ++----- .../notifications/template_group/storage.go | 20 ++----- .../notifications/timeinterval/storage.go | 20 ++----- pkg/registry/apis/dashboard/legacy_storage.go | 29 ++-------- pkg/registry/apis/dashboard/register.go | 7 +-- pkg/registry/apis/dashboard/storage.go | 35 ------------ pkg/registry/apis/folders/register.go | 3 +- pkg/registry/apis/folders/storage.go | 40 -------------- pkg/registry/apis/peakq/register.go | 8 +-- pkg/registry/apis/peakq/storage.go | 41 -------------- pkg/registry/apis/playlist/storage.go | 54 ++++++------------- pkg/registry/apis/service/register.go | 8 +-- pkg/registry/apis/service/storage.go | 41 -------------- 14 files changed, 73 insertions(+), 282 deletions(-) create mode 100644 pkg/apiserver/registry/generic/storage.go delete mode 100644 pkg/registry/apis/dashboard/storage.go delete mode 100644 pkg/registry/apis/folders/storage.go delete mode 100644 pkg/registry/apis/peakq/storage.go delete mode 100644 pkg/registry/apis/service/storage.go diff --git a/pkg/apiserver/registry/generic/storage.go b/pkg/apiserver/registry/generic/storage.go new file mode 100644 index 00000000000..945d51fbd58 --- /dev/null +++ b/pkg/apiserver/registry/generic/storage.go @@ -0,0 +1,31 @@ +package generic + +import ( + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apiserver/pkg/registry/generic" + "k8s.io/apiserver/pkg/registry/generic/registry" + + "github.com/grafana/grafana/pkg/apimachinery/utils" +) + +func NewRegistryStore(scheme *runtime.Scheme, resourceInfo utils.ResourceInfo, optsGetter generic.RESTOptionsGetter) (*registry.Store, error) { + strategy := NewStrategy(scheme, resourceInfo.GroupVersion()) + store := ®istry.Store{ + NewFunc: resourceInfo.NewFunc, + NewListFunc: resourceInfo.NewListFunc, + KeyRootFunc: KeyRootFunc(resourceInfo.GroupResource()), + KeyFunc: NamespaceKeyFunc(resourceInfo.GroupResource()), + PredicateFunc: Matcher, + DefaultQualifiedResource: resourceInfo.GroupResource(), + SingularQualifiedResource: resourceInfo.SingularGroupResource(), + TableConvertor: resourceInfo.TableConverter(), + CreateStrategy: strategy, + UpdateStrategy: strategy, + DeleteStrategy: strategy, + } + options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs} + if err := store.CompleteWithOptions(options); err != nil { + return nil, err + } + return store, nil +} diff --git a/pkg/registry/apis/alerting/notifications/receiver/storage.go b/pkg/registry/apis/alerting/notifications/receiver/storage.go index 5a2f5804e9b..36b8932604a 100644 --- a/pkg/registry/apis/alerting/notifications/receiver/storage.go +++ b/pkg/registry/apis/alerting/notifications/receiver/storage.go @@ -43,23 +43,11 @@ func NewStorage( metadata: metadata, } if optsGetter != nil && dualWriteBuilder != nil { - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - s := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - PredicateFunc: Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - TableConvertor: legacyStore.tableConverter, - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} - if err := s.CompleteWithOptions(options); err != nil { + store, err := grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) + if err != nil { return nil, err } - return dualWriteBuilder(resourceInfo.GroupResource(), legacyStore, storage{Store: s}) + return dualWriteBuilder(resourceInfo.GroupResource(), legacyStore, store) } return legacyStore, nil } diff --git a/pkg/registry/apis/alerting/notifications/template_group/storage.go b/pkg/registry/apis/alerting/notifications/template_group/storage.go index e1f6a9f6886..49c14ea2863 100644 --- a/pkg/registry/apis/alerting/notifications/template_group/storage.go +++ b/pkg/registry/apis/alerting/notifications/template_group/storage.go @@ -41,25 +41,11 @@ func NewStorage( tableConverter: resourceInfo.TableConverter(), } if optsGetter != nil && dualWriteBuilder != nil { - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - s := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - TableConvertor: legacyStore.tableConverter, - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs} - if err := s.CompleteWithOptions(options); err != nil { + store, err := grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) + if err != nil { return nil, err } - return dualWriteBuilder(resourceInfo.GroupResource(), legacyStore, storage{Store: s}) + return dualWriteBuilder(resourceInfo.GroupResource(), legacyStore, store) } return legacyStore, nil } diff --git a/pkg/registry/apis/alerting/notifications/timeinterval/storage.go b/pkg/registry/apis/alerting/notifications/timeinterval/storage.go index 3bf20b03ddd..63e1943faf8 100644 --- a/pkg/registry/apis/alerting/notifications/timeinterval/storage.go +++ b/pkg/registry/apis/alerting/notifications/timeinterval/storage.go @@ -41,25 +41,11 @@ func NewStorage( tableConverter: resourceInfo.TableConverter(), } if optsGetter != nil && dualWriteBuilder != nil { - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - s := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - TableConvertor: legacyStore.tableConverter, - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs} - if err := s.CompleteWithOptions(options); err != nil { + store, err := grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) + if err != nil { return nil, err } - return dualWriteBuilder(resourceInfo.GroupResource(), legacyStore, storage{Store: s}) + return dualWriteBuilder(resourceInfo.GroupResource(), legacyStore, store) } return legacyStore, nil } diff --git a/pkg/registry/apis/dashboard/legacy_storage.go b/pkg/registry/apis/dashboard/legacy_storage.go index 6d3c29eb876..f8607e2a75c 100644 --- a/pkg/registry/apis/dashboard/legacy_storage.go +++ b/pkg/registry/apis/dashboard/legacy_storage.go @@ -1,16 +1,16 @@ package dashboard import ( + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apiserver/pkg/registry/generic" + "k8s.io/apiserver/pkg/registry/rest" + "github.com/grafana/grafana/pkg/apimachinery/utils" grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" "github.com/grafana/grafana/pkg/registry/apis/dashboard/legacy" "github.com/grafana/grafana/pkg/storage/unified/apistore" "github.com/grafana/grafana/pkg/storage/unified/resource" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apiserver/pkg/registry/generic" - genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" - "k8s.io/apiserver/pkg/registry/rest" ) type dashboardStorage struct { @@ -46,24 +46,5 @@ func (s *dashboardStorage) newStore(scheme *runtime.Scheme, defaultOptsGetter ge defaultOpts.StorageConfig.Config, ) - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - store := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: grafanaregistry.Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - TableConvertor: s.tableConverter, - } - - options := &generic.StoreOptions{RESTOptions: optsGetter} - if err := store.CompleteWithOptions(options); err != nil { - return nil, err - } - return store, err + return grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) } diff --git a/pkg/registry/apis/dashboard/register.go b/pkg/registry/apis/dashboard/register.go index 10a5826a392..ae57adfe147 100644 --- a/pkg/registry/apis/dashboard/register.go +++ b/pkg/registry/apis/dashboard/register.go @@ -140,15 +140,10 @@ func (b *DashboardsAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver // Dual writes if a RESTOptionsGetter is provided if optsGetter != nil && dualWriteBuilder != nil { - store, err := newStorage(scheme) + store, err := grafanaregistry.NewRegistryStore(scheme, dash, optsGetter) if err != nil { return err } - - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} - if err := store.CompleteWithOptions(options); err != nil { - return err - } storage[dash.StoragePath()], err = dualWriteBuilder(dash.GroupResource(), legacyStore, store) if err != nil { return err diff --git a/pkg/registry/apis/dashboard/storage.go b/pkg/registry/apis/dashboard/storage.go deleted file mode 100644 index 91ae97525aa..00000000000 --- a/pkg/registry/apis/dashboard/storage.go +++ /dev/null @@ -1,35 +0,0 @@ -package dashboard - -import ( - dashboard "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1" - grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" - grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" - "k8s.io/apimachinery/pkg/runtime" - genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" -) - -var _ grafanarest.Storage = (*storage)(nil) - -type storage struct { - *genericregistry.Store -} - -func newStorage(scheme *runtime.Scheme) (*storage, error) { - resourceInfo := dashboard.DashboardResourceInfo - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - store := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: grafanaregistry.Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - TableConvertor: resourceInfo.TableConverter(), - } - - return &storage{Store: store}, nil -} diff --git a/pkg/registry/apis/folders/register.go b/pkg/registry/apis/folders/register.go index 60c6a278ece..618a4c509d8 100644 --- a/pkg/registry/apis/folders/register.go +++ b/pkg/registry/apis/folders/register.go @@ -16,6 +16,7 @@ import ( "github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/apis/folder/v0alpha1" + grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" "github.com/grafana/grafana/pkg/services/accesscontrol" "github.com/grafana/grafana/pkg/services/apiserver/builder" @@ -109,7 +110,7 @@ func (b *FolderAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.API // enable dual writer if optsGetter != nil && dualWriteBuilder != nil { - store, err := newStorage(scheme, optsGetter, legacyStore) + store, err := grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) if err != nil { return err } diff --git a/pkg/registry/apis/folders/storage.go b/pkg/registry/apis/folders/storage.go deleted file mode 100644 index 21f98e93842..00000000000 --- a/pkg/registry/apis/folders/storage.go +++ /dev/null @@ -1,40 +0,0 @@ -package folders - -import ( - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apiserver/pkg/registry/generic" - genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" - - grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" - grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" -) - -var _ grafanarest.Storage = (*storage)(nil) - -type storage struct { - *genericregistry.Store -} - -func newStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter, legacy *legacyStorage) (*storage, error) { - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - - store := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: grafanaregistry.Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - TableConvertor: legacy.tableConverter, - - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} - if err := store.CompleteWithOptions(options); err != nil { - return nil, err - } - return &storage{Store: store}, nil -} diff --git a/pkg/registry/apis/peakq/register.go b/pkg/registry/apis/peakq/register.go index 31f41d8c379..bb2d38c356d 100644 --- a/pkg/registry/apis/peakq/register.go +++ b/pkg/registry/apis/peakq/register.go @@ -1,6 +1,7 @@ package peakq import ( + "github.com/prometheus/client_golang/prometheus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -12,9 +13,8 @@ import ( "k8s.io/kube-openapi/pkg/spec3" "k8s.io/kube-openapi/pkg/validation/spec" - "github.com/prometheus/client_golang/prometheus" - peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1" + grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" "github.com/grafana/grafana/pkg/services/apiserver/builder" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -68,10 +68,12 @@ func (b *PeakQAPIBuilder) InstallSchema(scheme *runtime.Scheme) error { func (b *PeakQAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupInfo, scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter, _ grafanarest.DualWriteBuilder) error { resourceInfo := peakq.QueryTemplateResourceInfo storage := map[string]rest.Storage{} - peakqStorage, err := newStorage(scheme, optsGetter) + + peakqStorage, err := grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) if err != nil { return err } + storage[resourceInfo.StoragePath()] = peakqStorage storage[resourceInfo.StoragePath("render")] = &renderREST{ getter: peakqStorage, diff --git a/pkg/registry/apis/peakq/storage.go b/pkg/registry/apis/peakq/storage.go deleted file mode 100644 index 3953ba3e360..00000000000 --- a/pkg/registry/apis/peakq/storage.go +++ /dev/null @@ -1,41 +0,0 @@ -package peakq - -import ( - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apiserver/pkg/registry/generic" - genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" - - peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1" - grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" - grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" -) - -var _ grafanarest.Storage = (*storage)(nil) - -type storage struct { - *genericregistry.Store -} - -func newStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) { - resourceInfo := peakq.QueryTemplateResourceInfo - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - - store := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: grafanaregistry.Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - TableConvertor: resourceInfo.TableConverter(), - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} - if err := store.CompleteWithOptions(options); err != nil { - return nil, err - } - return &storage{Store: store}, nil -} diff --git a/pkg/registry/apis/playlist/storage.go b/pkg/registry/apis/playlist/storage.go index ecf75910373..bd5a03b14bb 100644 --- a/pkg/registry/apis/playlist/storage.go +++ b/pkg/registry/apis/playlist/storage.go @@ -4,52 +4,28 @@ import ( "strings" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/registry/generic" genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" playlist "github.com/grafana/grafana/apps/playlist/apis/playlist/v0alpha1" + "github.com/grafana/grafana/pkg/apimachinery/utils" grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" - grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" ) -var _ grafanarest.Storage = (*storage)(nil) - -type storage struct { - *genericregistry.Store -} - -func newStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter, legacy *legacyStorage) (*storage, error) { - gr := schema.GroupResource{ - Group: playlist.PlaylistKind().Group(), - Resource: playlist.PlaylistKind().Plural(), - } - singularGR := schema.GroupResource{ - Group: playlist.PlaylistKind().Group(), - Resource: strings.ToLower(playlist.PlaylistKind().Kind()), - } - strategy := grafanaregistry.NewStrategy(scheme, gr.WithVersion(playlist.PlaylistKind().Version()).GroupVersion()) - store := &genericregistry.Store{ - NewFunc: func() runtime.Object { - return playlist.PlaylistKind().ZeroValue() +func newStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter, legacy *legacyStorage) (*genericregistry.Store, error) { + kind := playlist.PlaylistKind() + singular := strings.ToLower(kind.Kind()) // ??? + resourceInfo := utils.NewResourceInfo( + kind.Group(), kind.Version(), + kind.GroupVersionResource().Resource, singular, + kind.Kind(), + func() runtime.Object { + return kind.ZeroValue() }, - NewListFunc: func() runtime.Object { - return playlist.PlaylistKind().ZeroListValue() + func() runtime.Object { + return kind.ZeroListValue() }, - KeyRootFunc: grafanaregistry.KeyRootFunc(gr), - KeyFunc: grafanaregistry.NamespaceKeyFunc(gr), - PredicateFunc: grafanaregistry.Matcher, - DefaultQualifiedResource: gr, - SingularQualifiedResource: singularGR, - TableConvertor: legacy.tableConverter, - - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} - if err := store.CompleteWithOptions(options); err != nil { - return nil, err - } - return &storage{Store: store}, nil + utils.TableColumns{}, // will use default columns... TODO? legacy.tableConverter., + ) + return grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) } diff --git a/pkg/registry/apis/service/register.go b/pkg/registry/apis/service/register.go index 17763fa64e9..39138660134 100644 --- a/pkg/registry/apis/service/register.go +++ b/pkg/registry/apis/service/register.go @@ -1,6 +1,7 @@ package service import ( + "github.com/prometheus/client_golang/prometheus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -10,9 +11,8 @@ import ( genericapiserver "k8s.io/apiserver/pkg/server" "k8s.io/kube-openapi/pkg/common" - "github.com/prometheus/client_golang/prometheus" - service "github.com/grafana/grafana/pkg/apis/service/v0alpha1" + grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" "github.com/grafana/grafana/pkg/services/apiserver/builder" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -73,10 +73,12 @@ func (b *ServiceAPIBuilder) InstallSchema(scheme *runtime.Scheme) error { func (b *ServiceAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupInfo, scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter, _ grafanarest.DualWriteBuilder) error { resourceInfo := service.ExternalNameResourceInfo storage := map[string]rest.Storage{} - serviceStorage, err := newStorage(scheme, optsGetter) + + serviceStorage, err := grafanaregistry.NewRegistryStore(scheme, resourceInfo, optsGetter) if err != nil { return err } + storage[resourceInfo.StoragePath()] = serviceStorage apiGroupInfo.VersionedResourcesStorageMap[service.VERSION] = storage return nil diff --git a/pkg/registry/apis/service/storage.go b/pkg/registry/apis/service/storage.go deleted file mode 100644 index 2ed49bb53f6..00000000000 --- a/pkg/registry/apis/service/storage.go +++ /dev/null @@ -1,41 +0,0 @@ -package service - -import ( - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apiserver/pkg/registry/generic" - genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" - - service "github.com/grafana/grafana/pkg/apis/service/v0alpha1" - grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" - grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" -) - -var _ grafanarest.Storage = (*storage)(nil) - -type storage struct { - *genericregistry.Store -} - -func newStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) { - resourceInfo := service.ExternalNameResourceInfo - strategy := grafanaregistry.NewStrategy(scheme, resourceInfo.GroupVersion()) - - store := &genericregistry.Store{ - NewFunc: resourceInfo.NewFunc, - NewListFunc: resourceInfo.NewListFunc, - KeyRootFunc: grafanaregistry.KeyRootFunc(resourceInfo.GroupResource()), - KeyFunc: grafanaregistry.NamespaceKeyFunc(resourceInfo.GroupResource()), - PredicateFunc: grafanaregistry.Matcher, - DefaultQualifiedResource: resourceInfo.GroupResource(), - SingularQualifiedResource: resourceInfo.SingularGroupResource(), - TableConvertor: resourceInfo.TableConverter(), - CreateStrategy: strategy, - UpdateStrategy: strategy, - DeleteStrategy: strategy, - } - options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: grafanaregistry.GetAttrs} - if err := store.CompleteWithOptions(options); err != nil { - return nil, err - } - return &storage{Store: store}, nil -}