K8s: Add resource type helper to avoid so many hardcoded names (#79344)

This commit is contained in:
Ryan McKinley
2023-12-11 22:03:48 +02:00
committed by GitHub
parent 5147bdeb4b
commit f69516bf47
11 changed files with 190 additions and 71 deletions
+7 -6
View File
@@ -32,18 +32,19 @@ type dummyStorage struct {
}
func newDummyStorage(gv schema.GroupVersion, scheme *runtime.Scheme, names ...string) *dummyStorage {
var resourceInfo = example.DummyResourceInfo
strategy := grafanaregistry.NewStrategy(scheme)
store := &genericregistry.Store{
NewFunc: func() runtime.Object { return &example.DummyResource{} }, // getter not supported
NewListFunc: func() runtime.Object { return &example.DummyResourceList{} }, // both list and get return the same thing
NewFunc: resourceInfo.NewFunc,
NewListFunc: resourceInfo.NewListFunc,
PredicateFunc: grafanaregistry.Matcher,
DefaultQualifiedResource: gv.WithResource("dummy").GroupResource(),
SingularQualifiedResource: gv.WithResource("dummy").GroupResource(),
DefaultQualifiedResource: resourceInfo.GroupResource(),
SingularQualifiedResource: resourceInfo.SingularGroupResource(),
TableConvertor: rest.NewDefaultTableConvertor(resourceInfo.GroupResource()),
CreateStrategy: strategy,
UpdateStrategy: strategy,
DeleteStrategy: strategy,
}
store.TableConvertor = rest.NewDefaultTableConvertor(store.DefaultQualifiedResource)
return &dummyStorage{
store: store,
@@ -63,7 +64,7 @@ func (s *dummyStorage) NamespaceScoped() bool {
}
func (s *dummyStorage) GetSingularName() string {
return "dummy"
return example.DummyResourceInfo.GetSingularName()
}
func (s *dummyStorage) NewList() runtime.Object {
+6 -11
View File
@@ -23,11 +23,6 @@ import (
grafanaapiserver "github.com/grafana/grafana/pkg/services/grafana-apiserver"
)
// GroupName is the group name for this API.
const GroupName = "example.grafana.app"
const VersionID = "v0alpha1" //
const APIVersion = GroupName + "/" + VersionID
var _ grafanaapiserver.APIGroupBuilder = (*TestingAPIBuilder)(nil)
// This is used just so wire has something unique to return
@@ -38,7 +33,7 @@ type TestingAPIBuilder struct {
func NewTestingAPIBuilder() *TestingAPIBuilder {
return &TestingAPIBuilder{
gv: schema.GroupVersion{Group: GroupName, Version: VersionID},
gv: schema.GroupVersion{Group: example.GROUP, Version: example.VERSION},
}
}
@@ -89,13 +84,13 @@ func (b *TestingAPIBuilder) GetAPIGroupInfo(
optsGetter generic.RESTOptionsGetter,
) (*genericapiserver.APIGroupInfo, error) {
b.codecs = codecs
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(GroupName, scheme, metav1.ParameterCodec, codecs)
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(b.gv.Group, scheme, metav1.ParameterCodec, codecs)
storage := map[string]rest.Storage{}
storage["runtime"] = newDeploymentInfoStorage(b.gv, scheme)
storage["dummy"] = newDummyStorage(b.gv, scheme, "test1", "test2", "test3")
storage["dummy/sub"] = &dummySubresourceREST{}
apiGroupInfo.VersionedResourcesStorageMap[VersionID] = storage
storage[example.RuntimeResourceInfo.StoragePath()] = newDeploymentInfoStorage(b.gv, scheme)
storage[example.DummyResourceInfo.StoragePath()] = newDummyStorage(b.gv, scheme, "test1", "test2", "test3")
storage[example.DummyResourceInfo.StoragePath("sub")] = &dummySubresourceREST{}
apiGroupInfo.VersionedResourcesStorageMap[b.gv.Version] = storage
return &apiGroupInfo, nil
}
+8 -10
View File
@@ -29,26 +29,24 @@ type staticStorage struct {
}
func newDeploymentInfoStorage(gv schema.GroupVersion, scheme *runtime.Scheme) *staticStorage {
var resourceInfo = example.RuntimeResourceInfo
strategy := grafanaregistry.NewStrategy(scheme)
store := &genericregistry.Store{
NewFunc: func() runtime.Object { return &example.RuntimeInfo{} }, // getter not supported
NewListFunc: func() runtime.Object { return &example.RuntimeInfo{} }, // both list and get return the same thing
NewFunc: resourceInfo.NewFunc,
NewListFunc: resourceInfo.NewListFunc,
PredicateFunc: grafanaregistry.Matcher,
DefaultQualifiedResource: gv.WithResource("runtime").GroupResource(),
SingularQualifiedResource: gv.WithResource("runtime").GroupResource(),
DefaultQualifiedResource: resourceInfo.GroupResource(),
SingularQualifiedResource: resourceInfo.SingularGroupResource(),
TableConvertor: rest.NewDefaultTableConvertor(resourceInfo.GroupResource()),
CreateStrategy: strategy,
UpdateStrategy: strategy,
DeleteStrategy: strategy,
}
store.TableConvertor = rest.NewDefaultTableConvertor(store.DefaultQualifiedResource)
return &staticStorage{
Store: store,
info: example.RuntimeInfo{
TypeMeta: metav1.TypeMeta{
APIVersion: APIVersion,
Kind: "DeploymentInfo",
},
TypeMeta: example.RuntimeResourceInfo.TypeMeta(),
BuildVersion: setting.BuildVersion,
BuildCommit: setting.BuildCommit,
BuildBranch: setting.BuildBranch,
@@ -72,7 +70,7 @@ func (s *staticStorage) NamespaceScoped() bool {
}
func (s *staticStorage) GetSingularName() string {
return "runtime"
return example.RuntimeResourceInfo.GetSingularName()
}
func (s *staticStorage) NewList() runtime.Object {