Plugins: Move alias support to plugin json (but still hardcoded) (#75129)
This commit is contained in:
@@ -29,7 +29,6 @@ func DefaultConstructFunc(signatureCalculator plugins.SignatureCalculator, asset
|
||||
// DefaultDecorateFuncs are the default DecorateFuncs used for the Decorate step of the Bootstrap stage.
|
||||
func DefaultDecorateFuncs(cfg *config.Cfg) []DecorateFunc {
|
||||
return []DecorateFunc{
|
||||
AliasDecorateFunc,
|
||||
AppDefaultNavURLDecorateFunc,
|
||||
AppChildDecorateFunc(cfg),
|
||||
}
|
||||
@@ -79,19 +78,6 @@ func (c *DefaultConstructor) Construct(ctx context.Context, src plugins.PluginSo
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// AliasDecorateFunc is a DecorateFunc that sets the alias for the plugin.
|
||||
func AliasDecorateFunc(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
switch p.ID {
|
||||
case "grafana-pyroscope-datasource": // rebranding
|
||||
p.Alias = "phlare"
|
||||
case "grafana-testdata-datasource":
|
||||
p.Alias = "testdata"
|
||||
case "debug": // panel plugin used for testing
|
||||
p.Alias = "debugX"
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// AppDefaultNavURLDecorateFunc is a DecorateFunc that sets the default nav URL for app plugins.
|
||||
func AppDefaultNavURLDecorateFunc(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
if p.IsApp() {
|
||||
|
||||
@@ -48,8 +48,8 @@ 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
|
||||
for _, a := range p.AliasIDs {
|
||||
i.alias[a] = p
|
||||
}
|
||||
i.mu.Unlock()
|
||||
|
||||
@@ -64,8 +64,10 @@ func (i *InMemory) Remove(_ context.Context, pluginID string) error {
|
||||
|
||||
i.mu.Lock()
|
||||
delete(i.store, pluginID)
|
||||
if p != nil && p.Alias != "" {
|
||||
delete(i.alias, p.Alias)
|
||||
if p != nil {
|
||||
for _, a := range p.AliasIDs {
|
||||
delete(i.alias, a)
|
||||
}
|
||||
}
|
||||
i.mu.Unlock()
|
||||
|
||||
|
||||
@@ -278,6 +278,7 @@ func TestAliasSupport(t *testing.T) {
|
||||
|
||||
pluginIdNew := "plugin-new"
|
||||
pluginIdOld := "plugin-old"
|
||||
pluginIdOld2 := "plugin-old2"
|
||||
|
||||
p, exists := i.Plugin(ctx, pluginIdNew)
|
||||
require.False(t, exists)
|
||||
@@ -285,9 +286,9 @@ func TestAliasSupport(t *testing.T) {
|
||||
|
||||
pluginNew := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: pluginIdNew,
|
||||
ID: pluginIdNew,
|
||||
AliasIDs: []string{pluginIdOld, pluginIdOld2},
|
||||
},
|
||||
Alias: pluginIdOld, // TODO: move to JSONData
|
||||
}
|
||||
err := i.Add(ctx, pluginNew)
|
||||
require.NoError(t, err)
|
||||
@@ -302,6 +303,11 @@ func TestAliasSupport(t *testing.T) {
|
||||
require.True(t, exists)
|
||||
require.Equal(t, pluginNew, found)
|
||||
|
||||
// Can lookup by the other old ID
|
||||
found, exists = i.Plugin(ctx, pluginIdOld2)
|
||||
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,
|
||||
|
||||
+12
-12
@@ -243,18 +243,18 @@ 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"`
|
||||
SkipDataQuery bool `json:"skipDataQuery"`
|
||||
ReleaseState string `json:"state"`
|
||||
BaseURL string `json:"baseUrl"`
|
||||
Signature string `json:"signature"`
|
||||
Module string `json:"module"`
|
||||
AngularDetected bool `json:"angularDetected"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
AliasIDs []string `json:"aliasIds,omitempty"`
|
||||
Info Info `json:"info"`
|
||||
HideFromList bool `json:"hideFromList"`
|
||||
Sort int `json:"sort"`
|
||||
SkipDataQuery bool `json:"skipDataQuery"`
|
||||
ReleaseState string `json:"state"`
|
||||
BaseURL string `json:"baseUrl"`
|
||||
Signature string `json:"signature"`
|
||||
Module string `json:"module"`
|
||||
AngularDetected bool `json:"angularDetected"`
|
||||
}
|
||||
|
||||
type AppDTO struct {
|
||||
|
||||
@@ -18,6 +18,10 @@ schemas: [{
|
||||
id: string & strings.MinRunes(1)
|
||||
id: =~"^([0-9a-z]+\\-([0-9a-z]+\\-)?(\(strings.Join([ for t in _types {t}], "|"))))|(alertGroups|alertlist|annolist|barchart|bargauge|candlestick|canvas|dashlist|debug|datagrid|gauge|geomap|gettingstarted|graph|heatmap|histogram|icon|live|logs|news|nodeGraph|piechart|pluginlist|stat|state-timeline|status-history|table|table-old|text|timeseries|trend|traces|welcome|xychart|alertmanager|cloudwatch|dashboard|elasticsearch|grafana|grafana-azure-monitor-datasource|graphite|influxdb|jaeger|loki|mixed|mssql|mysql|opentsdb|postgres|prometheus|stackdriver|tempo|grafana-testdata-datasource|zipkin|phlare|parca)$"
|
||||
|
||||
// An alias is useful when migrating from one plugin id to another (rebranding etc)
|
||||
// This should be used sparingly, and is currently only supported though a hardcoded checklist
|
||||
aliasIDs?: [...string]
|
||||
|
||||
// Human-readable name of the plugin that is shown to the user in
|
||||
// the UI.
|
||||
name: string
|
||||
|
||||
@@ -278,6 +278,10 @@ type PluginDef struct {
|
||||
// For data source plugins, if the plugin supports alerting. Requires `backend` to be set to `true`.
|
||||
Alerting *bool `json:"alerting,omitempty"`
|
||||
|
||||
// An alias is useful when migrating from one plugin id to another (rebranding etc)
|
||||
// This should be used sparingly, and is currently only supported though a hardcoded checklist
|
||||
AliasIDs []string `json:"aliasIDs,omitempty"`
|
||||
|
||||
// For data source plugins, if the plugin supports annotation
|
||||
// queries.
|
||||
Annotations *bool `json:"annotations,omitempty"`
|
||||
|
||||
+15
-9
@@ -29,6 +29,7 @@ var (
|
||||
ErrPluginFileRead = errors.New("file could not be read")
|
||||
ErrUninstallInvalidPluginDir = errors.New("cannot recognize as plugin folder")
|
||||
ErrInvalidPluginJSON = errors.New("did not find valid type or id properties in plugin.json")
|
||||
ErrUnsupportedAlias = errors.New("can not set alias in plugin.json")
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
@@ -64,9 +65,6 @@ type Plugin struct {
|
||||
log log.Logger
|
||||
|
||||
mu sync.Mutex
|
||||
|
||||
// This will be moved to plugin.json when we have general support in gcom
|
||||
Alias string `json:"alias,omitempty"`
|
||||
}
|
||||
|
||||
// JSONData represents the plugin's plugin.json
|
||||
@@ -75,7 +73,7 @@ type JSONData struct {
|
||||
ID string `json:"id"`
|
||||
Type Type `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Alias string `json:"alias,omitempty"`
|
||||
AliasIDs []string `json:"aliasIDs,omitempty"`
|
||||
Info Info `json:"info"`
|
||||
Dependencies Dependencies `json:"dependencies"`
|
||||
Includes []*Includes `json:"includes"`
|
||||
@@ -130,12 +128,20 @@ func ReadPluginJSON(reader io.Reader) (JSONData, error) {
|
||||
switch plugin.ID {
|
||||
case "grafana-piechart-panel":
|
||||
plugin.Name = "Pie Chart (old)"
|
||||
case "grafana-pyroscope-datasource": // rebranding
|
||||
plugin.Alias = "phlare"
|
||||
case "grafana-pyroscope-datasource":
|
||||
fallthrough
|
||||
case "grafana-testdata-datasource":
|
||||
plugin.Alias = "testdata"
|
||||
case "debug": // panel plugin used for testing
|
||||
plugin.Alias = "debugX"
|
||||
fallthrough
|
||||
case "annolist":
|
||||
fallthrough
|
||||
case "debug":
|
||||
if len(plugin.AliasIDs) == 0 {
|
||||
return plugin, fmt.Errorf("expected alias to be set")
|
||||
}
|
||||
default: // TODO: when gcom validates the alias, this condition can be removed
|
||||
if len(plugin.AliasIDs) > 0 {
|
||||
return plugin, ErrUnsupportedAlias
|
||||
}
|
||||
}
|
||||
|
||||
if len(plugin.Dependencies.Plugins) == 0 {
|
||||
|
||||
@@ -107,14 +107,15 @@ func Test_ReadPluginJSON(t *testing.T) {
|
||||
pluginJSON: func(t *testing.T) io.ReadCloser {
|
||||
pJSON := `{
|
||||
"id": "grafana-pyroscope-datasource",
|
||||
"type": "datasource"
|
||||
"type": "datasource",
|
||||
"aliasIDs": ["phlare"]
|
||||
}`
|
||||
return io.NopCloser(strings.NewReader(pJSON))
|
||||
},
|
||||
expected: JSONData{
|
||||
ID: "grafana-pyroscope-datasource",
|
||||
Alias: "phlare", // Hardcoded from the parser
|
||||
Type: TypeDataSource,
|
||||
ID: "grafana-pyroscope-datasource",
|
||||
AliasIDs: []string{"phlare"}, // Hardcoded from the parser
|
||||
Type: TypeDataSource,
|
||||
Dependencies: Dependencies{
|
||||
GrafanaDependency: "",
|
||||
GrafanaVersion: "*",
|
||||
@@ -122,6 +123,24 @@ func Test_ReadPluginJSON(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "do not allow alias except for our hardcoded set",
|
||||
pluginJSON: func(t *testing.T) io.ReadCloser {
|
||||
pJSON := `{
|
||||
"id": "my-custom-app",
|
||||
"type": "app",
|
||||
"aliasIDs": ["phlare"]
|
||||
}`
|
||||
return io.NopCloser(strings.NewReader(pJSON))
|
||||
},
|
||||
err: ErrUnsupportedAlias,
|
||||
expected: JSONData{
|
||||
ID: "my-custom-app",
|
||||
AliasIDs: []string{"phlare"}, // Hardcoded from the parser
|
||||
Type: "app",
|
||||
Dependencies: Dependencies{},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user