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

@ -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 (
"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

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

@ -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"
)
// 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)

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

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

Loading…
Cancel
Save