mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 16:02:31 +08:00
Advisor: Ensure User-Agent header is set for checks (#105122)
This commit is contained in:

committed by
GitHub

parent
0978ec5e91
commit
8ef8185379
@ -14,6 +14,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/provisionedplugins"
|
||||
"github.com/grafana/grafana/pkg/services/ssosettings"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type CheckService interface {
|
||||
@ -30,12 +31,14 @@ type Service struct {
|
||||
managedPlugins managedplugins.Manager
|
||||
provisionedPlugins provisionedplugins.Manager
|
||||
ssoSettingsSvc ssosettings.Service
|
||||
GrafanaVersion string
|
||||
}
|
||||
|
||||
func ProvideService(datasourceSvc datasources.DataSourceService, pluginStore pluginstore.Store,
|
||||
pluginContextProvider *plugincontext.Provider, pluginClient plugins.Client,
|
||||
pluginRepo repo.Service, pluginPreinstall plugininstaller.Preinstall, managedPlugins managedplugins.Manager,
|
||||
provisionedPlugins provisionedplugins.Manager, ssoSettingsSvc ssosettings.Service,
|
||||
provisionedPlugins provisionedplugins.Manager, ssoSettingsSvc ssosettings.Service, settings *setting.Cfg,
|
||||
|
||||
) *Service {
|
||||
return &Service{
|
||||
datasourceSvc: datasourceSvc,
|
||||
@ -47,6 +50,7 @@ func ProvideService(datasourceSvc datasources.DataSourceService, pluginStore plu
|
||||
managedPlugins: managedPlugins,
|
||||
provisionedPlugins: provisionedPlugins,
|
||||
ssoSettingsSvc: ssoSettingsSvc,
|
||||
GrafanaVersion: settings.BuildVersion,
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,6 +62,7 @@ func (s *Service) Checks() []checks.Check {
|
||||
s.pluginContextProvider,
|
||||
s.pluginClient,
|
||||
s.pluginRepo,
|
||||
s.GrafanaVersion,
|
||||
),
|
||||
plugincheck.New(
|
||||
s.pluginStore,
|
||||
@ -65,6 +70,7 @@ func (s *Service) Checks() []checks.Check {
|
||||
s.pluginPreinstall,
|
||||
s.managedPlugins,
|
||||
s.provisionedPlugins,
|
||||
s.GrafanaVersion,
|
||||
),
|
||||
authchecks.New(s.ssoSettingsSvc),
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
sysruntime "runtime"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
@ -30,6 +31,7 @@ type check struct {
|
||||
PluginContextProvider pluginContextProvider
|
||||
PluginClient plugins.Client
|
||||
PluginRepo repo.Service
|
||||
GrafanaVersion string
|
||||
}
|
||||
|
||||
func New(
|
||||
@ -38,6 +40,7 @@ func New(
|
||||
pluginContextProvider pluginContextProvider,
|
||||
pluginClient plugins.Client,
|
||||
pluginRepo repo.Service,
|
||||
grafanaVersion string,
|
||||
) checks.Check {
|
||||
return &check{
|
||||
DatasourceSvc: datasourceSvc,
|
||||
@ -45,6 +48,7 @@ func New(
|
||||
PluginContextProvider: pluginContextProvider,
|
||||
PluginClient: pluginClient,
|
||||
PluginRepo: pluginRepo,
|
||||
GrafanaVersion: grafanaVersion,
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,6 +89,7 @@ func (c *check) Steps() []checks.Step {
|
||||
&missingPluginStep{
|
||||
PluginStore: c.PluginStore,
|
||||
PluginRepo: c.PluginRepo,
|
||||
GrafanaVersion: c.GrafanaVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -203,6 +208,7 @@ func (s *healthCheckStep) Run(ctx context.Context, log logging.Logger, obj *advi
|
||||
type missingPluginStep struct {
|
||||
PluginStore pluginstore.Store
|
||||
PluginRepo repo.Service
|
||||
GrafanaVersion string
|
||||
}
|
||||
|
||||
func (s *missingPluginStep) Title() string {
|
||||
@ -235,7 +241,8 @@ func (s *missingPluginStep) Run(ctx context.Context, log logging.Logger, obj *ad
|
||||
Url: fmt.Sprintf("/connections/datasources/edit/%s", ds.UID),
|
||||
},
|
||||
}
|
||||
_, err := s.PluginRepo.PluginInfo(ctx, ds.Type)
|
||||
compatOpts := repo.NewCompatOpts(s.GrafanaVersion, sysruntime.GOOS, sysruntime.GOARCH)
|
||||
_, err := s.PluginRepo.PluginInfo(ctx, ds.Type, compatOpts)
|
||||
if err == nil {
|
||||
// Plugin is available in the repo
|
||||
links = append(links, advisor.CheckErrorLink{
|
||||
|
@ -260,7 +260,7 @@ type MockPluginRepo struct {
|
||||
exists bool
|
||||
}
|
||||
|
||||
func (m *MockPluginRepo) PluginInfo(context.Context, string) (*repo.PluginInfo, error) {
|
||||
func (m *MockPluginRepo) PluginInfo(context.Context, string, repo.CompatOpts) (*repo.PluginInfo, error) {
|
||||
if !m.exists {
|
||||
return nil, errors.New("plugin not found")
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
advisor "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1"
|
||||
"github.com/grafana/grafana/apps/advisor/pkg/app/checks"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
|
||||
"github.com/grafana/grafana/pkg/plugins/repo"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/managedplugins"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/plugininstaller"
|
||||
@ -30,6 +29,7 @@ func New(
|
||||
pluginPreinstall plugininstaller.Preinstall,
|
||||
managedPlugins managedplugins.Manager,
|
||||
provisionedPlugins provisionedplugins.Manager,
|
||||
grafanaVersion string,
|
||||
) checks.Check {
|
||||
return &check{
|
||||
PluginStore: pluginStore,
|
||||
@ -37,6 +37,7 @@ func New(
|
||||
PluginPreinstall: pluginPreinstall,
|
||||
ManagedPlugins: managedPlugins,
|
||||
ProvisionedPlugins: provisionedPlugins,
|
||||
GrafanaVersion: grafanaVersion,
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,6 +47,7 @@ type check struct {
|
||||
PluginPreinstall plugininstaller.Preinstall
|
||||
ManagedPlugins managedplugins.Manager
|
||||
ProvisionedPlugins provisionedplugins.Manager
|
||||
GrafanaVersion string
|
||||
}
|
||||
|
||||
func (c *check) ID() string {
|
||||
@ -76,12 +78,14 @@ func (c *check) Steps() []checks.Step {
|
||||
PluginPreinstall: c.PluginPreinstall,
|
||||
ManagedPlugins: c.ManagedPlugins,
|
||||
ProvisionedPlugins: c.ProvisionedPlugins,
|
||||
GrafanaVersion: c.GrafanaVersion,
|
||||
},
|
||||
&updateStep{
|
||||
PluginRepo: c.PluginRepo,
|
||||
PluginPreinstall: c.PluginPreinstall,
|
||||
ManagedPlugins: c.ManagedPlugins,
|
||||
ProvisionedPlugins: c.ProvisionedPlugins,
|
||||
GrafanaVersion: c.GrafanaVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -91,6 +95,7 @@ type deprecationStep struct {
|
||||
PluginPreinstall plugininstaller.Preinstall
|
||||
ManagedPlugins managedplugins.Manager
|
||||
ProvisionedPlugins provisionedplugins.Manager
|
||||
GrafanaVersion string
|
||||
provisionedPlugins []string
|
||||
}
|
||||
|
||||
@ -136,7 +141,8 @@ func (s *deprecationStep) Run(ctx context.Context, log logging.Logger, _ *adviso
|
||||
}
|
||||
|
||||
// Check if plugin is deprecated
|
||||
i, err := s.PluginRepo.PluginInfo(ctx, p.ID)
|
||||
compatOpts := repo.NewCompatOpts(s.GrafanaVersion, sysruntime.GOOS, sysruntime.GOARCH)
|
||||
i, err := s.PluginRepo.PluginInfo(ctx, p.ID, compatOpts)
|
||||
if err != nil {
|
||||
// Unable to check deprecation status
|
||||
return nil, nil
|
||||
@ -164,6 +170,7 @@ type updateStep struct {
|
||||
ManagedPlugins managedplugins.Manager
|
||||
ProvisionedPlugins provisionedplugins.Manager
|
||||
provisionedPlugins []string
|
||||
GrafanaVersion string
|
||||
}
|
||||
|
||||
func (s *updateStep) Title() string {
|
||||
@ -207,7 +214,7 @@ func (s *updateStep) Run(ctx context.Context, log logging.Logger, _ *advisor.Che
|
||||
}
|
||||
|
||||
// Check if plugin has a newer version available
|
||||
compatOpts := repo.NewCompatOpts(services.GrafanaVersion, sysruntime.GOOS, sysruntime.GOARCH)
|
||||
compatOpts := repo.NewCompatOpts(s.GrafanaVersion, sysruntime.GOOS, sysruntime.GOARCH)
|
||||
info, err := s.PluginRepo.GetPluginArchiveInfo(ctx, p.ID, "", compatOpts)
|
||||
if err != nil {
|
||||
// Unable to check updates
|
||||
|
@ -163,7 +163,7 @@ func TestRun(t *testing.T) {
|
||||
pluginPreinstall := &mockPluginPreinstall{pinned: tt.pluginPreinstalled}
|
||||
managedPlugins := &mockManagedPlugins{managed: tt.pluginManaged}
|
||||
provisionedPlugins := &mockProvisionedPlugins{provisioned: tt.pluginProvisioned}
|
||||
check := New(pluginStore, pluginRepo, pluginPreinstall, managedPlugins, provisionedPlugins)
|
||||
check := New(pluginStore, pluginRepo, pluginPreinstall, managedPlugins, provisionedPlugins, "12.0.0")
|
||||
|
||||
items, err := check.Items(context.Background())
|
||||
assert.NoError(t, err)
|
||||
@ -199,7 +199,7 @@ type mockPluginRepo struct {
|
||||
pluginArchiveInfo map[string]*repo.PluginArchiveInfo
|
||||
}
|
||||
|
||||
func (m *mockPluginRepo) PluginInfo(ctx context.Context, id string) (*repo.PluginInfo, error) {
|
||||
func (m *mockPluginRepo) PluginInfo(ctx context.Context, id string, compatOpts repo.CompatOpts) (*repo.PluginInfo, error) {
|
||||
return m.pluginInfo[id], nil
|
||||
}
|
||||
|
||||
|
@ -272,7 +272,7 @@ func (r *FakePluginRepo) PluginVersion(ctx context.Context, pluginID, version st
|
||||
return repo.VersionData{}, nil
|
||||
}
|
||||
|
||||
func (r *FakePluginRepo) PluginInfo(ctx context.Context, pluginID string) (*repo.PluginInfo, error) {
|
||||
func (r *FakePluginRepo) PluginInfo(ctx context.Context, pluginID string, compatOpts repo.CompatOpts) (*repo.PluginInfo, error) {
|
||||
return &repo.PluginInfo{}, nil
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ type Service interface {
|
||||
// PluginVersion will return plugin version based on the requested information.
|
||||
PluginVersion(ctx context.Context, pluginID, version string, compatOpts CompatOpts) (VersionData, error)
|
||||
// PluginInfo will return generic plugin information from grafana.com/api/plugins.
|
||||
PluginInfo(ctx context.Context, pluginID string) (*PluginInfo, error)
|
||||
PluginInfo(ctx context.Context, pluginID string, compatOpts CompatOpts) (*PluginInfo, error)
|
||||
}
|
||||
|
||||
type CompatOpts struct {
|
||||
|
@ -132,7 +132,7 @@ func (m *Manager) grafanaCompatiblePluginVersions(ctx context.Context, pluginID
|
||||
return v.Versions, nil
|
||||
}
|
||||
|
||||
func (m *Manager) PluginInfo(ctx context.Context, pluginID string) (*PluginInfo, error) {
|
||||
func (m *Manager) PluginInfo(ctx context.Context, pluginID string, compatOpts CompatOpts) (*PluginInfo, error) {
|
||||
u, err := url.Parse(m.client.grafanaComAPIURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -140,7 +140,7 @@ func (m *Manager) PluginInfo(ctx context.Context, pluginID string) (*PluginInfo,
|
||||
|
||||
u.Path = path.Join(u.Path, pluginID)
|
||||
|
||||
body, err := m.client.SendReq(ctx, u, CompatOpts{})
|
||||
body, err := m.client.SendReq(ctx, u, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ func TestPluginInfo(t *testing.T) {
|
||||
BaseURL: srv.URL,
|
||||
Logger: log.NewTestPrettyLogger(),
|
||||
})
|
||||
pi, err := m.PluginInfo(context.Background(), pluginID)
|
||||
pi, err := m.PluginInfo(context.Background(), pluginID, CompatOpts{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, pi.ID)
|
||||
require.Equal(t, pluginID, pi.Slug)
|
||||
|
Reference in New Issue
Block a user