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
9 changed files with 104 additions and 12 deletions

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)
})
}