advisor registry

advisor-registry
Will Browne 6 months ago
parent 86e466009a
commit ce808e35f8
  1. 9
      apps/advisor/pkg/app/app.go
  2. 39
      apps/advisor/pkg/app/checkregistry/checkregistry.go
  3. 42
      apps/advisor/pkg/app/checks/datasourcecheck/check.go
  4. 8
      apps/advisor/pkg/app/checks/datasourcecheck/check_test.go
  5. 13
      apps/advisor/pkg/app/checks/factory.go
  6. 5
      apps/advisor/pkg/app/checks/ifaces.go
  7. 16
      apps/advisor/pkg/app/checks/types.go
  8. 18
      pkg/registry/apps/advisor/register.go
  9. 2
      pkg/registry/apps/wireset.go

@ -9,12 +9,13 @@ import (
"github.com/grafana/grafana-app-sdk/resource" "github.com/grafana/grafana-app-sdk/resource"
"github.com/grafana/grafana-app-sdk/simple" "github.com/grafana/grafana-app-sdk/simple"
advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" 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" "github.com/grafana/grafana/apps/advisor/pkg/app/checks"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2" "k8s.io/klog/v2"
// Trigger registration of checks // 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 ( const (
@ -24,7 +25,7 @@ const (
func New(cfg app.Config) (app.App, error) { func New(cfg app.Config) (app.App, error) {
// Read config // Read config
advisorConfig, ok := cfg.SpecificConfig.(*checks.AdvisorConfig) checkRegistry, ok := cfg.SpecificConfig.(*checkregistry.Service)
if !ok { if !ok {
return nil, fmt.Errorf("invalid config type") return nil, fmt.Errorf("invalid config type")
} }
@ -37,10 +38,8 @@ func New(cfg app.Config) (app.App, error) {
} }
// Initialize checks // Initialize checks
checkFactories := checks.GetFactories()
checkMap := map[string]checks.Check{} checkMap := map[string]checks.Check{}
for _, r := range checkFactories { for _, c := range checkRegistry.Checks() {
c := r.New(advisorConfig)
checkMap[c.Type()] = c checkMap[c.Type()] = c
} }

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

@ -1,42 +1,48 @@
package datasource package datasourcecheck
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend"
"k8s.io/klog/v2"
advisor "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" 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/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/datasources"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
"github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
"k8s.io/klog/v2"
) )
func init() { type Check struct {
checks.AddFactory(&factory{}) DatasourceSvc datasources.DataSourceService
PluginStore pluginstore.Store
PluginContextProvider datasource.PluginContextWrapper
PluginClient plugins.Client
} }
type factory struct{} func New(datasourceSvc datasources.DataSourceService, pluginStore pluginstore.Store,
pluginContextProvider datasource.PluginContextWrapper, pluginClient plugins.Client) *Check {
func (r *factory) New(cfg *checks.AdvisorConfig) checks.Check { return &Check{
return &check{cfg} DatasourceSvc: datasourceSvc,
} PluginStore: pluginStore,
PluginContextProvider: pluginContextProvider,
type check struct { PluginClient: pluginClient,
cfg *checks.AdvisorConfig }
} }
func (c *check) Type() string { func (c *Check) Type() string {
return "datasource" 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 // Optionally read the check input encoded in the object
// fmt.Println(obj.Data) // fmt.Println(obj.Data)
dss, err := c.cfg.DatasourceSvc.GetAllDataSources(ctx, &datasources.GetAllDataSourcesQuery{}) dss, err := c.DatasourceSvc.GetAllDataSources(ctx, &datasources.GetAllDataSourcesQuery{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -56,7 +62,7 @@ func (c *check) Run(ctx context.Context, obj *advisor.CheckSpec) (*advisor.Check
// Health check execution // Health check execution
id := &user.SignedInUser{OrgID: int64(1), Login: "admin"} id := &user.SignedInUser{OrgID: int64(1), Login: "admin"}
ctx = identity.WithRequester(ctx, id) 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, Type: ds.Type,
UID: ds.UID, UID: ds.UID,
APIVersion: ds.APIVersion, APIVersion: ds.APIVersion,
@ -69,7 +75,7 @@ func (c *check) Run(ctx context.Context, obj *advisor.CheckSpec) (*advisor.Check
PluginContext: pCtx, PluginContext: pCtx,
Headers: map[string]string{}, Headers: map[string]string{},
} }
resp, err := c.cfg.PluginClient.CheckHealth(ctx, req) resp, err := c.PluginClient.CheckHealth(ctx, req)
if err != nil { if err != nil {
fmt.Println("Error checking health", err) fmt.Println("Error checking health", err)
continue continue

@ -1,4 +1,4 @@
package datasource package datasourcecheck
import ( import (
"context" "context"
@ -30,7 +30,7 @@ func TestCheck_Run(t *testing.T) {
PluginClient: mockPluginClient, PluginClient: mockPluginClient,
} }
check := &check{cfg: cfg} check := &Check{cfg: cfg}
report, err := check.Run(context.Background(), &advisor.CheckSpec{}) report, err := check.Run(context.Background(), &advisor.CheckSpec{})
@ -54,7 +54,7 @@ func TestCheck_Run(t *testing.T) {
PluginClient: mockPluginClient, PluginClient: mockPluginClient,
} }
check := &check{cfg: cfg} check := &Check{cfg: cfg}
report, err := check.Run(context.Background(), &advisor.CheckSpec{}) report, err := check.Run(context.Background(), &advisor.CheckSpec{})
@ -79,7 +79,7 @@ func TestCheck_Run(t *testing.T) {
PluginClient: mockPluginClient, PluginClient: mockPluginClient,
} }
check := &check{cfg: cfg} check := &Check{cfg: cfg}
report, err := check.Run(context.Background(), &advisor.CheckSpec{}) report, err := check.Run(context.Background(), &advisor.CheckSpec{})

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

@ -6,11 +6,6 @@ import (
advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" 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. // Check defines the methods that a check must implement to be executed.
type Check interface { type Check interface {
Run(ctx context.Context, obj *advisorv0alpha1.CheckSpec) (*advisorv0alpha1.CheckV0alpha1StatusReport, error) Run(ctx context.Context, obj *advisorv0alpha1.CheckSpec) (*advisorv0alpha1.CheckV0alpha1StatusReport, error)

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

@ -6,12 +6,8 @@ import (
"github.com/grafana/grafana/apps/advisor/pkg/apis" "github.com/grafana/grafana/apps/advisor/pkg/apis"
advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1" advisorv0alpha1 "github.com/grafana/grafana/apps/advisor/pkg/apis/advisor/v0alpha1"
advisorapp "github.com/grafana/grafana/apps/advisor/pkg/app" advisorapp "github.com/grafana/grafana/apps/advisor/pkg/app"
"github.com/grafana/grafana/apps/advisor/pkg/app/checks" "github.com/grafana/grafana/apps/advisor/pkg/app/checkregistry"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/registry/apis/datasource"
"github.com/grafana/grafana/pkg/services/apiserver/builder/runner" "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 { type AdvisorAppProvider struct {
@ -19,21 +15,13 @@ type AdvisorAppProvider struct {
} }
func RegisterApp( func RegisterApp(
datasourceSvc datasources.DataSourceService, checkRegistry *checkregistry.Service,
pluginStore pluginstore.Store,
pluginContextProvider datasource.PluginContextWrapper,
pluginClient plugins.Client,
) *AdvisorAppProvider { ) *AdvisorAppProvider {
provider := &AdvisorAppProvider{} provider := &AdvisorAppProvider{}
appCfg := &runner.AppBuilderConfig{ appCfg := &runner.AppBuilderConfig{
OpenAPIDefGetter: advisorv0alpha1.GetOpenAPIDefinitions, OpenAPIDefGetter: advisorv0alpha1.GetOpenAPIDefinitions,
ManagedKinds: advisorapp.GetKinds(), ManagedKinds: advisorapp.GetKinds(),
CustomConfig: any(&checks.AdvisorConfig{ CustomConfig: any(checkRegistry),
DatasourceSvc: datasourceSvc,
PluginStore: pluginStore,
PluginContextProvider: pluginContextProvider,
PluginClient: pluginClient,
}),
} }
provider.Provider = simple.NewAppProvider(apis.LocalManifest(), appCfg, advisorapp.New) provider.Provider = simple.NewAppProvider(apis.LocalManifest(), appCfg, advisorapp.New)
return provider return provider

@ -3,6 +3,7 @@ package appregistry
import ( import (
"github.com/google/wire" "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/advisor"
"github.com/grafana/grafana/pkg/registry/apps/investigation" "github.com/grafana/grafana/pkg/registry/apps/investigation"
"github.com/grafana/grafana/pkg/registry/apps/playlist" "github.com/grafana/grafana/pkg/registry/apps/playlist"
@ -12,5 +13,6 @@ var WireSet = wire.NewSet(
ProvideRegistryServiceSink, ProvideRegistryServiceSink,
playlist.RegisterApp, playlist.RegisterApp,
investigation.RegisterApp, investigation.RegisterApp,
checkregistry.ProvideService,
advisor.RegisterApp, advisor.RegisterApp,
) )

Loading…
Cancel
Save