Plugins: Support changing plugin IDs (hardcoded) (#67867)

This commit is contained in:
Ryan McKinley 2023-06-02 10:46:12 -07:00 committed by GitHub
parent 4fdccef7b3
commit 422684d8b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 104 additions and 12 deletions

View File

@ -58,6 +58,7 @@ export interface PluginMeta<T extends KeyValue = {}> {
info: PluginMetaInfo;
includes?: PluginInclude[];
state?: PluginState;
alias?: string;
// System.load & relative URLS
module: string;

View File

@ -77,6 +77,7 @@ func (hs *HTTPServer) GetDataSources(c *contextmodel.ReqContext) response.Respon
if plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), ds.Type); exists {
dsItem.TypeLogoUrl = plugin.Info.Logos.Small
dsItem.TypeName = plugin.Name
dsItem.Type = plugin.ID // may be from an alias
} else {
dsItem.TypeLogoUrl = "public/img/icn-datasource.svg"
}
@ -730,6 +731,15 @@ func (hs *HTTPServer) convertModelToDtos(ctx context.Context, ds *datasources.Da
ReadOnly: ds.ReadOnly,
}
if hs.pluginStore != nil {
if plugin, exists := hs.pluginStore.Plugin(ctx, ds.Type); exists {
dto.TypeLogoUrl = plugin.Info.Logos.Small
dto.Type = plugin.ID // may be from an alias
} else {
dto.TypeLogoUrl = "public/img/icn-datasource.svg"
}
}
secrets, err := hs.DataSourcesService.DecryptedValues(ctx, ds)
if err == nil {
for k, v := range secrets {

View File

@ -71,6 +71,7 @@ func (hs *HTTPServer) getFrontendSettings(c *contextmodel.ReqContext) (*dtos.Fro
panels[panel.ID] = plugins.PanelDTO{
ID: panel.ID,
Name: panel.Name,
Alias: panel.Alias,
Info: panel.Info,
Module: panel.Module,
BaseURL: panel.BaseURL,
@ -318,6 +319,7 @@ func (hs *HTTPServer) getFSDataSources(c *contextmodel.ReqContext, availablePlug
continue
}
plugin := ap.Plugin
dsDTO.Type = plugin.ID
dsDTO.Preload = plugin.Preload
dsDTO.Module = plugin.Module
dsDTO.PluginMeta = &plugins.PluginMetaDTO{
@ -479,10 +481,15 @@ type availablePluginDTO struct {
type AvailablePlugins map[plugins.Type]map[string]*availablePluginDTO
func (ap AvailablePlugins) Get(pluginType plugins.Type, pluginID string) (*availablePluginDTO, bool) {
if _, exists := ap[pluginType][pluginID]; exists {
return ap[pluginType][pluginID], true
p, exists := ap[pluginType][pluginID]
if exists {
return p, true
}
for _, p = range ap[pluginType] {
if p.Plugin.ID == pluginID || p.Plugin.Alias == pluginID {
return p, true
}
}
return nil, false
}

View File

@ -138,6 +138,14 @@ func (l *Loader) loadPlugins(ctx context.Context, src plugins.PluginSource, foun
// clear plugin error if a pre-existing error has since been resolved
delete(l.errs, plugin.ID)
// Hardcoded alias changes
switch plugin.ID {
case "grafana-pyroscope": // rebranding
plugin.Alias = "phlare"
case "debug": // panel plugin used for testing
plugin.Alias = "debugX"
}
// verify module.js exists for SystemJS to load.
// CDN plugins can be loaded with plugin.json only, so do not warn for those.
if !plugin.IsRenderer() && !plugin.IsCorePlugin() {

View File

@ -10,6 +10,7 @@ import (
type InMemory struct {
store map[string]*plugins.Plugin
alias map[string]*plugins.Plugin
mu sync.RWMutex
}
@ -20,6 +21,7 @@ func ProvideService() *InMemory {
func NewInMemory() *InMemory {
return &InMemory{
store: make(map[string]*plugins.Plugin),
alias: make(map[string]*plugins.Plugin),
}
}
@ -46,18 +48,25 @@ func (i *InMemory) Add(_ context.Context, p *plugins.Plugin) error {
i.mu.Lock()
i.store[p.ID] = p
if p.Alias != "" {
i.alias[p.Alias] = p
}
i.mu.Unlock()
return nil
}
func (i *InMemory) Remove(_ context.Context, pluginID string) error {
if !i.isRegistered(pluginID) {
p, ok := i.plugin(pluginID)
if !ok {
return fmt.Errorf("plugin %s is not registered", pluginID)
}
i.mu.Lock()
delete(i.store, pluginID)
if p != nil && p.Alias != "" {
delete(i.alias, p.Alias)
}
i.mu.Unlock()
return nil
@ -69,13 +78,18 @@ func (i *InMemory) plugin(pluginID string) (*plugins.Plugin, bool) {
p, exists := i.store[pluginID]
if !exists {
return nil, false
p, exists = i.alias[pluginID]
if !exists {
return nil, false
}
}
return p, true
}
func (i *InMemory) isRegistered(pluginID string) bool {
_, exists := i.plugin(pluginID)
return exists
p, exists := i.plugin(pluginID)
// This may have matched based on an alias
return exists && p.ID == pluginID
}

View File

@ -15,9 +15,7 @@ const pluginID = "test-ds"
func TestInMemory(t *testing.T) {
t.Run("Test mix of registry operations", func(t *testing.T) {
i := &InMemory{
store: map[string]*plugins.Plugin{},
}
i := NewInMemory()
ctx := context.Background()
p, exists := i.Plugin(ctx, pluginID)
@ -272,3 +270,45 @@ func TestInMemory_Remove(t *testing.T) {
})
}
}
func TestAliasSupport(t *testing.T) {
t.Run("Test alias operations", func(t *testing.T) {
i := NewInMemory()
ctx := context.Background()
pluginIdNew := "plugin-new"
pluginIdOld := "plugin-old"
p, exists := i.Plugin(ctx, pluginIdNew)
require.False(t, exists)
require.Nil(t, p)
pluginNew := &plugins.Plugin{
JSONData: plugins.JSONData{
ID: pluginIdNew,
},
Alias: pluginIdOld, // TODO: move to JSONData
}
err := i.Add(ctx, pluginNew)
require.NoError(t, err)
// Can lookup by the new ID
found, exists := i.Plugin(ctx, pluginIdNew)
require.True(t, exists)
require.Equal(t, pluginNew, found)
// Can lookup by the old ID
found, exists = i.Plugin(ctx, pluginIdOld)
require.True(t, exists)
require.Equal(t, pluginNew, found)
// Register the old plugin and look it up
pluginOld := &plugins.Plugin{JSONData: plugins.JSONData{
ID: pluginIdOld,
}}
require.NoError(t, i.Add(ctx, pluginOld))
found, exists = i.Plugin(ctx, pluginIdOld)
require.True(t, exists)
require.Equal(t, pluginOld, found)
})
}

View File

@ -244,6 +244,7 @@ type DataSourceDTO struct {
type PanelDTO struct {
ID string `json:"id"`
Name string `json:"name"`
Alias string `json:"alias,omitempty"`
Info Info `json:"info"`
HideFromList bool `json:"hideFromList"`
Sort int `json:"sort"`

View File

@ -57,6 +57,9 @@ type Plugin struct {
SecretsManager secretsmanagerplugin.SecretsManagerPlugin
client backendplugin.Plugin
log log.Logger
// This will be moved to plugin.json when we have general support in gcom
Alias string `json:"alias,omitempty"`
}
type PluginDTO struct {
@ -84,6 +87,9 @@ type PluginDTO struct {
BaseURL string
AngularDetected bool
// This will be moved to plugin.json when we have general support in gcom
Alias string `json:"alias,omitempty"`
}
func (p PluginDTO) SupportsStreaming() bool {
@ -155,7 +161,9 @@ func ReadPluginJSON(reader io.Reader) (JSONData, error) {
return JSONData{}, err
}
if plugin.ID == "grafana-piechart-panel" {
// Hardcoded changes
switch plugin.ID {
case "grafana-piechart-panel":
plugin.Name = "Pie Chart (old)"
}

View File

@ -14,13 +14,16 @@ export function importPanelPlugin(id: string): Promise<PanelPlugin> {
return loaded;
}
const meta = config.panels[id];
const meta = config.panels[id] || Object.values(config.panels).find((p) => p.alias === id);
if (!meta) {
throw new Error(`Plugin ${id} not found`);
}
promiseCache[id] = getPanelPlugin(meta);
if (id !== meta.type) {
promiseCache[meta.type] = promiseCache[id];
}
return promiseCache[id];
}