mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s: Add subresource to the example apiserver (#78030)
This commit is contained in:
118
pkg/registry/apis/example/dummy_storage.go
Normal file
118
pkg/registry/apis/example/dummy_storage.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
example "github.com/grafana/grafana/pkg/apis/example/v0alpha1"
|
||||
grafanarequest "github.com/grafana/grafana/pkg/services/grafana-apiserver/endpoints/request"
|
||||
grafanaregistry "github.com/grafana/grafana/pkg/services/grafana-apiserver/registry/generic"
|
||||
)
|
||||
|
||||
var (
|
||||
_ rest.Storage = (*dummyStorage)(nil)
|
||||
_ rest.Scoper = (*dummyStorage)(nil)
|
||||
_ rest.SingularNameProvider = (*dummyStorage)(nil)
|
||||
_ rest.Getter = (*dummyStorage)(nil)
|
||||
_ rest.Lister = (*dummyStorage)(nil)
|
||||
)
|
||||
|
||||
type dummyStorage struct {
|
||||
store *genericregistry.Store
|
||||
names []string
|
||||
creationTimestamp metav1.Time
|
||||
}
|
||||
|
||||
func newDummyStorage(gv schema.GroupVersion, scheme *runtime.Scheme, names ...string) *dummyStorage {
|
||||
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
|
||||
PredicateFunc: grafanaregistry.Matcher,
|
||||
DefaultQualifiedResource: gv.WithResource("dummy").GroupResource(),
|
||||
SingularQualifiedResource: gv.WithResource("dummy").GroupResource(),
|
||||
CreateStrategy: strategy,
|
||||
UpdateStrategy: strategy,
|
||||
DeleteStrategy: strategy,
|
||||
}
|
||||
store.TableConvertor = rest.NewDefaultTableConvertor(store.DefaultQualifiedResource)
|
||||
|
||||
return &dummyStorage{
|
||||
store: store,
|
||||
names: names,
|
||||
creationTimestamp: metav1.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *dummyStorage) New() runtime.Object {
|
||||
return s.store.New()
|
||||
}
|
||||
|
||||
func (s *dummyStorage) Destroy() {}
|
||||
|
||||
func (s *dummyStorage) NamespaceScoped() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *dummyStorage) GetSingularName() string {
|
||||
return "dummy"
|
||||
}
|
||||
|
||||
func (s *dummyStorage) NewList() runtime.Object {
|
||||
return s.store.NewListFunc()
|
||||
}
|
||||
|
||||
func (s *dummyStorage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||
return s.store.TableConvertor.ConvertToTable(ctx, object, tableOptions)
|
||||
}
|
||||
|
||||
func (s *dummyStorage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
||||
info, err := grafanarequest.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
idx := slices.Index(s.names, name)
|
||||
if idx < 0 {
|
||||
return nil, fmt.Errorf("dummy not found")
|
||||
}
|
||||
|
||||
return &example.DummyResource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: info.Value,
|
||||
CreationTimestamp: s.creationTimestamp,
|
||||
ResourceVersion: "1",
|
||||
},
|
||||
Spec: fmt.Sprintf("dummy: %s", name),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *dummyStorage) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
||||
info, err := grafanarequest.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &example.DummyResourceList{}
|
||||
for _, name := range s.names {
|
||||
res.Items = append(res.Items, example.DummyResource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: info.Value,
|
||||
CreationTimestamp: s.creationTimestamp,
|
||||
ResourceVersion: "1",
|
||||
},
|
||||
Spec: fmt.Sprintf("dummy: %s", name),
|
||||
})
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -33,27 +33,50 @@ var _ grafanaapiserver.APIGroupBuilder = (*TestingAPIBuilder)(nil)
|
||||
// This is used just so wire has something unique to return
|
||||
type TestingAPIBuilder struct {
|
||||
codecs serializer.CodecFactory
|
||||
gv schema.GroupVersion
|
||||
}
|
||||
|
||||
func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration grafanaapiserver.APIRegistrar) *TestingAPIBuilder {
|
||||
if !features.IsEnabled(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
|
||||
return nil // skip registration unless opting into experimental apis
|
||||
}
|
||||
builder := &TestingAPIBuilder{}
|
||||
builder := &TestingAPIBuilder{
|
||||
gv: schema.GroupVersion{Group: GroupName, Version: VersionID},
|
||||
}
|
||||
apiregistration.RegisterAPI(builder)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (b *TestingAPIBuilder) GetGroupVersion() schema.GroupVersion {
|
||||
return SchemeGroupVersion
|
||||
return b.gv
|
||||
}
|
||||
|
||||
func addKnownTypes(scheme *runtime.Scheme, gv schema.GroupVersion) {
|
||||
scheme.AddKnownTypes(gv,
|
||||
&example.RuntimeInfo{},
|
||||
&example.DummyResource{},
|
||||
&example.DummyResourceList{},
|
||||
&example.DummySubresource{},
|
||||
)
|
||||
}
|
||||
|
||||
func (b *TestingAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
||||
err := AddToScheme(scheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return scheme.SetVersionPriority(SchemeGroupVersion)
|
||||
addKnownTypes(scheme, b.gv)
|
||||
|
||||
// Link this version to the internal representation.
|
||||
// This is used for server-side-apply (PATCH), and avoids the error:
|
||||
// "no kind is registered for the type"
|
||||
addKnownTypes(scheme, schema.GroupVersion{
|
||||
Group: b.gv.Group,
|
||||
Version: runtime.APIVersionInternal,
|
||||
})
|
||||
|
||||
// If multiple versions exist, then register conversions from zz_generated.conversion.go
|
||||
// if err := playlist.RegisterConversions(scheme); err != nil {
|
||||
// return err
|
||||
// }
|
||||
metav1.AddToGroupVersion(scheme, b.gv)
|
||||
return scheme.SetVersionPriority(b.gv)
|
||||
}
|
||||
|
||||
func (b *TestingAPIBuilder) GetAPIGroupInfo(
|
||||
@@ -61,11 +84,14 @@ func (b *TestingAPIBuilder) GetAPIGroupInfo(
|
||||
codecs serializer.CodecFactory, // pointer?
|
||||
optsGetter generic.RESTOptionsGetter,
|
||||
) (*genericapiserver.APIGroupInfo, error) {
|
||||
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(GroupName, scheme, metav1.ParameterCodec, codecs)
|
||||
storage := map[string]rest.Storage{}
|
||||
storage["runtime"] = newDeploymentInfoStorage()
|
||||
apiGroupInfo.VersionedResourcesStorageMap[VersionID] = storage
|
||||
b.codecs = codecs
|
||||
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(GroupName, 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
|
||||
return &apiGroupInfo, nil
|
||||
}
|
||||
|
||||
@@ -170,29 +196,3 @@ func (b *TestingAPIBuilder) GetAPIRoutes() *grafanaapiserver.APIRoutes {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// SchemeGroupVersion is group version used to register these objects
|
||||
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: VersionID}
|
||||
|
||||
// Resource takes an unqualified resource and returns a Group qualified GroupResource
|
||||
func Resource(resource string) schema.GroupResource {
|
||||
return SchemeGroupVersion.WithResource(resource).GroupResource()
|
||||
}
|
||||
|
||||
var (
|
||||
// SchemeBuilder points to a list of functions added to Scheme.
|
||||
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
|
||||
localSchemeBuilder = &SchemeBuilder
|
||||
// AddToScheme is a common registration function for mapping packaged scoped group & version keys to a scheme.
|
||||
AddToScheme = localSchemeBuilder.AddToScheme
|
||||
)
|
||||
|
||||
// Adds the list of known types to the given scheme.
|
||||
func addKnownTypes(scheme *runtime.Scheme) error {
|
||||
scheme.AddKnownTypes(SchemeGroupVersion,
|
||||
&example.RuntimeInfo{},
|
||||
)
|
||||
|
||||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
|
||||
return scheme.SetVersionPriority(SchemeGroupVersion)
|
||||
}
|
||||
|
||||
@@ -7,25 +7,43 @@ import (
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
example "github.com/grafana/grafana/pkg/apis/example/v0alpha1"
|
||||
grafanaregistry "github.com/grafana/grafana/pkg/services/grafana-apiserver/registry/generic"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
_ rest.Storage = (*staticStorage)(nil)
|
||||
_ rest.Scoper = (*staticStorage)(nil)
|
||||
_ rest.SingularNameProvider = (*staticStorage)(nil)
|
||||
_ rest.Lister = (*staticStorage)(nil)
|
||||
_ rest.Storage = (*staticStorage)(nil)
|
||||
)
|
||||
|
||||
type staticStorage struct {
|
||||
info example.RuntimeInfo
|
||||
Store *genericregistry.Store
|
||||
info example.RuntimeInfo
|
||||
}
|
||||
|
||||
func newDeploymentInfoStorage() *staticStorage {
|
||||
func newDeploymentInfoStorage(gv schema.GroupVersion, scheme *runtime.Scheme) *staticStorage {
|
||||
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
|
||||
PredicateFunc: grafanaregistry.Matcher,
|
||||
DefaultQualifiedResource: gv.WithResource("runtime").GroupResource(),
|
||||
SingularQualifiedResource: gv.WithResource("runtime").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,
|
||||
@@ -44,7 +62,7 @@ func newDeploymentInfoStorage() *staticStorage {
|
||||
}
|
||||
|
||||
func (s *staticStorage) New() runtime.Object {
|
||||
return &example.RuntimeInfo{}
|
||||
return s.Store.New()
|
||||
}
|
||||
|
||||
func (s *staticStorage) Destroy() {}
|
||||
@@ -58,11 +76,11 @@ func (s *staticStorage) GetSingularName() string {
|
||||
}
|
||||
|
||||
func (s *staticStorage) NewList() runtime.Object {
|
||||
return &example.RuntimeInfo{}
|
||||
return s.Store.NewListFunc()
|
||||
}
|
||||
|
||||
func (s *staticStorage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||
return rest.NewDefaultTableConvertor(Resource("runtime")).ConvertToTable(ctx, object, tableOptions)
|
||||
return s.Store.TableConvertor.ConvertToTable(ctx, object, tableOptions)
|
||||
}
|
||||
|
||||
func (s *staticStorage) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
||||
|
||||
54
pkg/registry/apis/example/subresource.go
Normal file
54
pkg/registry/apis/example/subresource.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
example "github.com/grafana/grafana/pkg/apis/example/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/infra/appcontext"
|
||||
"github.com/grafana/grafana/pkg/services/grafana-apiserver/endpoints/request"
|
||||
)
|
||||
|
||||
type dummySubresourceREST struct{}
|
||||
|
||||
var _ = rest.Connecter(&dummySubresourceREST{})
|
||||
|
||||
func (r *dummySubresourceREST) New() runtime.Object {
|
||||
return &example.DummySubresource{}
|
||||
}
|
||||
|
||||
func (r *dummySubresourceREST) Destroy() {
|
||||
}
|
||||
|
||||
func (r *dummySubresourceREST) ConnectMethods() []string {
|
||||
return []string{"GET"}
|
||||
}
|
||||
|
||||
func (r *dummySubresourceREST) NewConnectOptions() (runtime.Object, bool, string) {
|
||||
return nil, false, "" // true means you can use the trailing path as a variable
|
||||
}
|
||||
|
||||
func (r *dummySubresourceREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
||||
info, err := request.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user, err := appcontext.User(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// This response object format is negotiated by k8s
|
||||
dummy := &example.DummySubresource{
|
||||
Info: fmt.Sprintf("%s/%s", info.Value, user.Login),
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
responder.Object(http.StatusOK, dummy)
|
||||
}), nil
|
||||
}
|
||||
Reference in New Issue
Block a user