grafana/pkg/registry/apps/apps.go

119 lines
4.0 KiB
Go
Raw Permalink Normal View History

package appregistry
import (
"context"
"slices"
"k8s.io/client-go/rest"
2025-01-21 19:31:33 +08:00
"github.com/grafana/grafana-app-sdk/app"
2025-07-22 01:32:15 +08:00
appsdkapiserver "github.com/grafana/grafana-app-sdk/k8s/apiserver"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/registry"
2025-01-21 19:31:33 +08:00
"github.com/grafana/grafana/pkg/registry/apps/advisor"
"github.com/grafana/grafana/pkg/registry/apps/alerting/notifications"
"github.com/grafana/grafana/pkg/registry/apps/alerting/rules"
"github.com/grafana/grafana/pkg/registry/apps/correlations"
"github.com/grafana/grafana/pkg/registry/apps/investigations"
2024-11-05 03:18:49 +08:00
"github.com/grafana/grafana/pkg/registry/apps/playlist"
2025-08-07 01:09:10 +08:00
"github.com/grafana/grafana/pkg/registry/apps/plugins"
2025-08-04 20:12:12 +08:00
"github.com/grafana/grafana/pkg/registry/apps/shorturl"
"github.com/grafana/grafana/pkg/services/apiserver"
"github.com/grafana/grafana/pkg/services/apiserver/builder"
"github.com/grafana/grafana/pkg/services/apiserver/builder/runner"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
)
2025-07-22 01:32:15 +08:00
// ProvideAppInstallers returns a list of app installers that can be used to install apps.
// This is the pattern that should be used to provide app installers in the app registry.
func ProvideAppInstallers(
2025-08-04 20:12:12 +08:00
features featuremgmt.FeatureToggles,
2025-07-22 01:32:15 +08:00
playlistAppInstaller *playlist.PlaylistAppInstaller,
2025-08-07 01:09:10 +08:00
pluginsApplInstaller *plugins.PluginsAppInstaller,
2025-08-04 20:12:12 +08:00
shorturlAppInstaller *shorturl.ShortURLAppInstaller,
rulesAppInstaller *rules.AlertingRulesAppInstaller,
correlationsAppInstaller *correlations.CorrelationsAppInstaller,
alertingNotificationAppInstaller *notifications.AlertingNotificationsAppInstaller,
2025-07-22 01:32:15 +08:00
) []appsdkapiserver.AppInstaller {
installers := []appsdkapiserver.AppInstaller{
playlistAppInstaller,
pluginsApplInstaller,
}
2025-08-04 20:12:12 +08:00
if features.IsEnabledGlobally(featuremgmt.FlagKubernetesShortURLs) {
installers = append(installers, shorturlAppInstaller)
}
if features.IsEnabledGlobally(featuremgmt.FlagKubernetesAlertingRules) && rulesAppInstaller != nil {
installers = append(installers, rulesAppInstaller)
}
if features.IsEnabledGlobally(featuremgmt.FlagKubernetesCorrelations) {
installers = append(installers, correlationsAppInstaller)
}
if alertingNotificationAppInstaller != nil {
installers = append(installers, alertingNotificationAppInstaller)
}
2025-08-04 20:12:12 +08:00
return installers
2025-07-22 01:32:15 +08:00
}
var (
_ registry.BackgroundService = (*Service)(nil)
)
type Service struct {
runner *runner.APIGroupRunner
log log.Logger
}
2025-07-22 01:32:15 +08:00
// ProvideBuilderRunners adapts apps to the APIGroupBuilder interface.
// deprecated: Use ProvideAppInstallers instead.
func ProvideBuilderRunners(
registrar builder.APIRegistrar,
restConfigProvider apiserver.RestConfigProvider,
features featuremgmt.FeatureToggles,
investigationAppProvider *investigations.InvestigationsAppProvider,
2025-01-21 19:31:33 +08:00
advisorAppProvider *advisor.AdvisorAppProvider,
grafanaCfg *setting.Cfg,
) (*Service, error) {
cfgWrapper := func(ctx context.Context) (*rest.Config, error) {
cfg, err := restConfigProvider.GetRestConfig(ctx)
if err != nil {
return nil, err
}
cfg.APIPath = "/apis"
return cfg, nil
}
cfg := runner.RunnerConfig{
RestConfigGetter: cfgWrapper,
APIRegistrar: registrar,
}
logger := log.New("app-registry")
var apiGroupRunner *runner.APIGroupRunner
var err error
2025-07-22 01:32:15 +08:00
providers := []app.Provider{}
if features.IsEnabledGlobally(featuremgmt.FlagInvestigationsBackend) {
logger.Debug("Investigations backend is enabled")
2025-01-21 19:31:33 +08:00
providers = append(providers, investigationAppProvider)
}
if features.IsEnabledGlobally(featuremgmt.FlagGrafanaAdvisor) &&
!slices.Contains(grafanaCfg.DisablePlugins, "grafana-advisor-app") {
2025-01-21 19:31:33 +08:00
providers = append(providers, advisorAppProvider)
}
apiGroupRunner, err = runner.NewAPIGroupRunner(cfg, providers...)
if err != nil {
return nil, err
}
return &Service{runner: apiGroupRunner, log: logger}, nil
}
func (s *Service) Run(ctx context.Context) error {
s.log.Debug("initializing app registry")
if err := s.runner.Init(ctx); err != nil {
return err
}
s.log.Info("app registry initialized")
return s.runner.Run(ctx)
}