diff --git a/apps/advisor/pkg/app/app.go b/apps/advisor/pkg/app/app.go index 023b82be21f..f45d71444af 100644 --- a/apps/advisor/pkg/app/app.go +++ b/apps/advisor/pkg/app/app.go @@ -9,12 +9,13 @@ import ( "github.com/grafana/grafana-app-sdk/resource" "github.com/grafana/grafana-app-sdk/simple" advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" + "github.com/grafana/grafana/apps/advisor/pkg/app/checkregistry" "github.com/grafana/grafana/apps/advisor/pkg/app/checks" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/klog/v2" // Trigger registration of checks - _ "github.com/grafana/grafana/apps/advisor/pkg/app/checks/datasource" + _ "github.com/grafana/grafana/apps/advisor/pkg/app/checks/datasourcecheck" ) const ( @@ -24,7 +25,7 @@ const ( func New(cfg app.Config) (app.App, error) { // Read config - advisorConfig, ok := cfg.SpecificConfig.(*checks.AdvisorConfig) + checkRegistry, ok := cfg.SpecificConfig.(*checkregistry.Service) if !ok { return nil, fmt.Errorf("invalid config type") } @@ -37,10 +38,8 @@ func New(cfg app.Config) (app.App, error) { } // Initialize checks - checkFactories := checks.GetFactories() checkMap := map[string]checks.Check{} - for _, r := range checkFactories { - c := r.New(advisorConfig) + for _, c := range checkRegistry.Checks() { checkMap[c.Type()] = c } diff --git a/apps/advisor/pkg/app/checkregistry/checkregistry.go b/apps/advisor/pkg/app/checkregistry/checkregistry.go new file mode 100644 index 00000000000..b49c582f596 --- /dev/null +++ b/apps/advisor/pkg/app/checkregistry/checkregistry.go @@ -0,0 +1,39 @@ +package checkregistry + +import ( + "github.com/grafana/grafana/apps/advisor/pkg/app/checks" + "github.com/grafana/grafana/apps/advisor/pkg/app/checks/datasourcecheck" + "github.com/grafana/grafana/pkg/plugins" + "github.com/grafana/grafana/pkg/registry/apis/datasource" + "github.com/grafana/grafana/pkg/services/datasources" + "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" +) + +type Service struct { + datasourceSvc datasources.DataSourceService + pluginStore pluginstore.Store + pluginContextProvider datasource.PluginContextWrapper + pluginClient plugins.Client +} + +func ProvideService(datasourceSvc datasources.DataSourceService, pluginStore pluginstore.Store, + pluginContextProvider datasource.PluginContextWrapper, pluginClient plugins.Client) *Service { + return &Service{ + datasourceSvc: datasourceSvc, + pluginStore: pluginStore, + pluginContextProvider: pluginContextProvider, + pluginClient: pluginClient, + } +} + +func (s *Service) Checks() []checks.Check { + return []checks.Check{ + datasourcecheck.New( + s.datasourceSvc, + s.pluginStore, + s.pluginContextProvider, + s.pluginClient, + ), + } + +} diff --git a/apps/advisor/pkg/app/checks/datasource/check.go b/apps/advisor/pkg/app/checks/datasourcecheck/check.go similarity index 62% rename from apps/advisor/pkg/app/checks/datasource/check.go rename to apps/advisor/pkg/app/checks/datasourcecheck/check.go index 1efb43037d5..9246b8e06e6 100644 --- a/apps/advisor/pkg/app/checks/datasource/check.go +++ b/apps/advisor/pkg/app/checks/datasourcecheck/check.go @@ -1,42 +1,48 @@ -package datasource +package datasourcecheck import ( "context" "fmt" "github.com/grafana/grafana-plugin-sdk-go/backend" + "k8s.io/klog/v2" + 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/apimachinery/identity" + "github.com/grafana/grafana/pkg/plugins" + "github.com/grafana/grafana/pkg/registry/apis/datasource" "github.com/grafana/grafana/pkg/services/datasources" + "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" "github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/util" - "k8s.io/klog/v2" ) -func init() { - checks.AddFactory(&factory{}) +type Check struct { + DatasourceSvc datasources.DataSourceService + PluginStore pluginstore.Store + PluginContextProvider datasource.PluginContextWrapper + PluginClient plugins.Client } -type factory struct{} - -func (r *factory) New(cfg *checks.AdvisorConfig) checks.Check { - return &check{cfg} -} - -type check struct { - cfg *checks.AdvisorConfig +func New(datasourceSvc datasources.DataSourceService, pluginStore pluginstore.Store, + pluginContextProvider datasource.PluginContextWrapper, pluginClient plugins.Client) *Check { + return &Check{ + DatasourceSvc: datasourceSvc, + PluginStore: pluginStore, + PluginContextProvider: pluginContextProvider, + PluginClient: pluginClient, + } } -func (c *check) Type() string { +func (c *Check) Type() string { return "datasource" } -func (c *check) Run(ctx context.Context, obj *advisor.CheckSpec) (*advisor.CheckV0alpha1StatusReport, error) { +func (c *Check) Run(ctx context.Context, obj *advisor.CheckSpec) (*advisor.CheckV0alpha1StatusReport, error) { // Optionally read the check input encoded in the object // fmt.Println(obj.Data) - dss, err := c.cfg.DatasourceSvc.GetAllDataSources(ctx, &datasources.GetAllDataSourcesQuery{}) + dss, err := c.DatasourceSvc.GetAllDataSources(ctx, &datasources.GetAllDataSourcesQuery{}) if err != nil { return nil, err } @@ -56,7 +62,7 @@ func (c *check) Run(ctx context.Context, obj *advisor.CheckSpec) (*advisor.Check // Health check execution id := &user.SignedInUser{OrgID: int64(1), Login: "admin"} ctx = identity.WithRequester(ctx, id) - pCtx, err := c.cfg.PluginContextProvider.PluginContextForDataSource(ctx, &backend.DataSourceInstanceSettings{ + pCtx, err := c.PluginContextProvider.PluginContextForDataSource(ctx, &backend.DataSourceInstanceSettings{ Type: ds.Type, UID: ds.UID, APIVersion: ds.APIVersion, @@ -69,7 +75,7 @@ func (c *check) Run(ctx context.Context, obj *advisor.CheckSpec) (*advisor.Check PluginContext: pCtx, Headers: map[string]string{}, } - resp, err := c.cfg.PluginClient.CheckHealth(ctx, req) + resp, err := c.PluginClient.CheckHealth(ctx, req) if err != nil { fmt.Println("Error checking health", err) continue diff --git a/apps/advisor/pkg/app/checks/datasource/check_test.go b/apps/advisor/pkg/app/checks/datasourcecheck/check_test.go similarity index 97% rename from apps/advisor/pkg/app/checks/datasource/check_test.go rename to apps/advisor/pkg/app/checks/datasourcecheck/check_test.go index 5ec52db4bf4..d79be7ba7d4 100644 --- a/apps/advisor/pkg/app/checks/datasource/check_test.go +++ b/apps/advisor/pkg/app/checks/datasourcecheck/check_test.go @@ -1,4 +1,4 @@ -package datasource +package datasourcecheck import ( "context" @@ -30,7 +30,7 @@ func TestCheck_Run(t *testing.T) { PluginClient: mockPluginClient, } - check := &check{cfg: cfg} + check := &Check{cfg: cfg} report, err := check.Run(context.Background(), &advisor.CheckSpec{}) @@ -54,7 +54,7 @@ func TestCheck_Run(t *testing.T) { PluginClient: mockPluginClient, } - check := &check{cfg: cfg} + check := &Check{cfg: cfg} report, err := check.Run(context.Background(), &advisor.CheckSpec{}) @@ -79,7 +79,7 @@ func TestCheck_Run(t *testing.T) { PluginClient: mockPluginClient, } - check := &check{cfg: cfg} + check := &Check{cfg: cfg} report, err := check.Run(context.Background(), &advisor.CheckSpec{}) diff --git a/apps/advisor/pkg/app/checks/factory.go b/apps/advisor/pkg/app/checks/factory.go deleted file mode 100644 index 27443638ad2..00000000000 --- a/apps/advisor/pkg/app/checks/factory.go +++ /dev/null @@ -1,13 +0,0 @@ -package checks - -var factories = []Factory{} - -// AddFactory adds a check factory to the list. -func AddFactory(r Factory) { - factories = append(factories, r) -} - -// GetFactories returns the list of check factories. -func GetFactories() []Factory { - return factories -} diff --git a/apps/advisor/pkg/app/checks/ifaces.go b/apps/advisor/pkg/app/checks/ifaces.go index b8b81171c20..008f5656298 100644 --- a/apps/advisor/pkg/app/checks/ifaces.go +++ b/apps/advisor/pkg/app/checks/ifaces.go @@ -6,11 +6,6 @@ import ( advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" ) -// Factory defines the method that a check factory needs to implement. -type Factory interface { - New(cfg *AdvisorConfig) Check -} - // Check defines the methods that a check must implement to be executed. type Check interface { Run(ctx context.Context, obj *advisorv0alpha1.CheckSpec) (*advisorv0alpha1.CheckV0alpha1StatusReport, error) diff --git a/apps/advisor/pkg/app/checks/types.go b/apps/advisor/pkg/app/checks/types.go deleted file mode 100644 index 51e9876dc34..00000000000 --- a/apps/advisor/pkg/app/checks/types.go +++ /dev/null @@ -1,16 +0,0 @@ -package checks - -import ( - "github.com/grafana/grafana/pkg/plugins" - "github.com/grafana/grafana/pkg/registry/apis/datasource" - "github.com/grafana/grafana/pkg/services/datasources" - "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" -) - -// AdvisorConfig contains the list of wire services checks are allowed to use. -type AdvisorConfig struct { - DatasourceSvc datasources.DataSourceService - PluginStore pluginstore.Store - PluginContextProvider datasource.PluginContextWrapper - PluginClient plugins.Client -} diff --git a/pkg/registry/apps/advisor/register.go b/pkg/registry/apps/advisor/register.go index aea4931cc73..90fd34e1914 100644 --- a/pkg/registry/apps/advisor/register.go +++ b/pkg/registry/apps/advisor/register.go @@ -6,12 +6,8 @@ import ( "github.com/grafana/grafana/apps/advisor/pkg/apis" advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" advisorapp "github.com/grafana/grafana/apps/advisor/pkg/app" - "github.com/grafana/grafana/apps/advisor/pkg/app/checks" - "github.com/grafana/grafana/pkg/plugins" - "github.com/grafana/grafana/pkg/registry/apis/datasource" + "github.com/grafana/grafana/apps/advisor/pkg/app/checkregistry" "github.com/grafana/grafana/pkg/services/apiserver/builder/runner" - "github.com/grafana/grafana/pkg/services/datasources" - "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" ) type AdvisorAppProvider struct { @@ -19,21 +15,13 @@ type AdvisorAppProvider struct { } func RegisterApp( - datasourceSvc datasources.DataSourceService, - pluginStore pluginstore.Store, - pluginContextProvider datasource.PluginContextWrapper, - pluginClient plugins.Client, + checkRegistry *checkregistry.Service, ) *AdvisorAppProvider { provider := &AdvisorAppProvider{} appCfg := &runner.AppBuilderConfig{ OpenAPIDefGetter: advisorv0alpha1.GetOpenAPIDefinitions, ManagedKinds: advisorapp.GetKinds(), - CustomConfig: any(&checks.AdvisorConfig{ - DatasourceSvc: datasourceSvc, - PluginStore: pluginStore, - PluginContextProvider: pluginContextProvider, - PluginClient: pluginClient, - }), + CustomConfig: any(checkRegistry), } provider.Provider = simple.NewAppProvider(apis.LocalManifest(), appCfg, advisorapp.New) return provider diff --git a/pkg/registry/apps/wireset.go b/pkg/registry/apps/wireset.go index b4da6459d3f..079373e7094 100644 --- a/pkg/registry/apps/wireset.go +++ b/pkg/registry/apps/wireset.go @@ -3,6 +3,7 @@ package appregistry import ( "github.com/google/wire" + "github.com/grafana/grafana/apps/advisor/pkg/app/checkregistry" "github.com/grafana/grafana/pkg/registry/apps/advisor" "github.com/grafana/grafana/pkg/registry/apps/investigation" "github.com/grafana/grafana/pkg/registry/apps/playlist" @@ -12,5 +13,6 @@ var WireSet = wire.NewSet( ProvideRegistryServiceSink, playlist.RegisterApp, investigation.RegisterApp, + checkregistry.ProvideService, advisor.RegisterApp, )