Plugins: Support MT app registration (#113348)

This commit is contained in:
Todd Treece
2025-12-02 09:59:46 -05:00
committed by GitHub
parent 77e13f7ef8
commit bdf529c545
22 changed files with 385 additions and 240 deletions
+81 -2
View File
@@ -5,17 +5,22 @@ import (
"fmt"
"github.com/grafana/grafana-app-sdk/app"
"github.com/grafana/grafana-app-sdk/k8s"
appsdkapiserver "github.com/grafana/grafana-app-sdk/k8s/apiserver"
"github.com/grafana/grafana-app-sdk/operator"
"github.com/grafana/grafana-app-sdk/simple"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/klog/v2"
pluginsappapis "github.com/grafana/grafana/apps/plugins/pkg/apis"
pluginsv0alpha1 "github.com/grafana/grafana/apps/plugins/pkg/apis/plugins/v0alpha1"
"github.com/grafana/grafana/apps/plugins/pkg/app/meta"
)
func New(cfg app.Config) (app.App, error) {
cfg.KubeConfig.APIPath = "apis"
specificConfig, ok := cfg.SpecificConfig.(*PluginAppConfig)
if !ok {
return nil, fmt.Errorf("invalid config type")
@@ -60,3 +65,77 @@ func New(cfg app.Config) (app.App, error) {
type PluginAppConfig struct {
MetaProviderManager *meta.ProviderManager
}
func ProvideAppInstaller(
metaProviderManager *meta.ProviderManager,
) (appsdkapiserver.AppInstaller, error) {
specificConfig := &PluginAppConfig{
MetaProviderManager: metaProviderManager,
}
provider := simple.NewAppProvider(pluginsappapis.LocalManifest(), specificConfig, New)
appConfig := app.Config{
KubeConfig: restclient.Config{}, // this will be overridden by the installer's InitializeApp method
ManifestData: *pluginsappapis.LocalManifest().ManifestData,
SpecificConfig: specificConfig,
}
defaultInstaller, err := appsdkapiserver.NewDefaultAppInstaller(provider, appConfig, pluginsappapis.NewGoTypeAssociator())
if err != nil {
return nil, err
}
appInstaller := &pluginAppInstaller{
AppInstaller: defaultInstaller,
metaManager: metaProviderManager,
ready: make(chan struct{}),
}
return appInstaller, nil
}
type pluginAppInstaller struct {
appsdkapiserver.AppInstaller
metaManager *meta.ProviderManager
// restConfig is set during InitializeApp and used by the client factory
restConfig *restclient.Config
ready chan struct{}
}
func (p *pluginAppInstaller) InitializeApp(restConfig restclient.Config) error {
if p.restConfig == nil {
p.restConfig = &restConfig
close(p.ready)
}
return p.AppInstaller.InitializeApp(restConfig)
}
func (p *pluginAppInstaller) InstallAPIs(
server appsdkapiserver.GenericAPIServer,
restOptsGetter generic.RESTOptionsGetter,
) error {
// Create a client factory function that will be called lazily when the client is needed.
// This uses the rest config from the app, which is set during InitializeApp.
clientFactory := func(ctx context.Context) (*pluginsv0alpha1.PluginClient, error) {
<-p.ready
if p.restConfig == nil {
return nil, fmt.Errorf("rest config not yet initialized, app must be initialized before client can be created")
}
clientGenerator := k8s.NewClientRegistry(*p.restConfig, k8s.DefaultClientConfig())
client, err := pluginsv0alpha1.NewPluginClientFromGenerator(clientGenerator)
if err != nil {
return nil, fmt.Errorf("failed to create plugin client: %w", err)
}
return client, nil
}
pluginMetaGVR := pluginsv0alpha1.PluginMetaKind().GroupVersionResource()
replacedStorage := map[schema.GroupVersionResource]rest.Storage{
pluginMetaGVR: NewPluginMetaStorage(p.metaManager, clientFactory),
}
wrappedServer := &customStorageWrapper{
wrapped: server,
replace: replacedStorage,
}
return p.AppInstaller.InstallAPIs(wrappedServer, restOptsGetter)
}
-5
View File
@@ -16,8 +16,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/rest"
claims "github.com/grafana/authlib/types"
pluginsv0alpha1 "github.com/grafana/grafana/apps/plugins/pkg/apis/plugins/v0alpha1"
"github.com/grafana/grafana/apps/plugins/pkg/app/meta"
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
@@ -40,14 +38,12 @@ type PluginMetaStorage struct {
clientOnce sync.Once
gr schema.GroupResource
namespacer claims.NamespaceFormatter
tableConverter rest.TableConvertor
}
func NewPluginMetaStorage(
metaManager *meta.ProviderManager,
clientFactory func(context.Context) (*pluginsv0alpha1.PluginClient, error),
namespacer claims.NamespaceFormatter,
) *PluginMetaStorage {
gr := schema.GroupResource{
Group: pluginsv0alpha1.APIGroup,
@@ -58,7 +54,6 @@ func NewPluginMetaStorage(
metaManager: metaManager,
clientFactory: clientFactory,
gr: gr,
namespacer: namespacer,
tableConverter: rest.NewDefaultTableConvertor(gr),
}
}
+39
View File
@@ -0,0 +1,39 @@
package app
import (
"fmt"
restful "github.com/emicklei/go-restful/v3"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/rest"
genericserver "k8s.io/apiserver/pkg/server"
appsdkapiserver "github.com/grafana/grafana-app-sdk/k8s/apiserver"
)
var _ appsdkapiserver.GenericAPIServer = (*customStorageWrapper)(nil)
type customStorageWrapper struct {
wrapped appsdkapiserver.GenericAPIServer
replace map[schema.GroupVersionResource]rest.Storage
}
func (c *customStorageWrapper) InstallAPIGroup(
apiGroupInfo *genericserver.APIGroupInfo,
) error {
if apiGroupInfo == nil || apiGroupInfo.VersionedResourcesStorageMap == nil {
return fmt.Errorf("apiGroupInfo cannot be nil")
}
for gvr, storage := range c.replace {
if _, ok := apiGroupInfo.VersionedResourcesStorageMap[gvr.Version]; !ok {
apiGroupInfo.VersionedResourcesStorageMap[gvr.Version] = map[string]rest.Storage{}
}
apiGroupInfo.VersionedResourcesStorageMap[gvr.Version][gvr.Resource] = storage
}
return c.wrapped.InstallAPIGroup(apiGroupInfo)
}
// RegisteredWebServices implements apiserver.GenericAPIServer.
func (c *customStorageWrapper) RegisteredWebServices() []*restful.WebService {
return []*restful.WebService{}
}