Chore: refactor grafana-apiserver a bit (#74177)
This commit is contained in:
+5
-27
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats/statscollector"
|
||||
"github.com/grafana/grafana/pkg/modules"
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/provisioning"
|
||||
@@ -39,15 +38,14 @@ type Options struct {
|
||||
func New(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistry accesscontrol.RoleRegistry,
|
||||
provisioningService provisioning.ProvisioningService, backgroundServiceProvider registry.BackgroundServiceRegistry,
|
||||
usageStatsProvidersRegistry registry.UsageStatsProvidersRegistry, statsCollectorService *statscollector.Service,
|
||||
moduleService modules.Engine,
|
||||
) (*Server, error) {
|
||||
statsCollectorService.RegisterProviders(usageStatsProvidersRegistry.GetServices())
|
||||
s, err := newServer(opts, cfg, httpServer, roleRegistry, provisioningService, backgroundServiceProvider, moduleService)
|
||||
s, err := newServer(opts, cfg, httpServer, roleRegistry, provisioningService, backgroundServiceProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.init(); err != nil {
|
||||
if err := s.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -56,7 +54,6 @@ func New(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistr
|
||||
|
||||
func newServer(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistry accesscontrol.RoleRegistry,
|
||||
provisioningService provisioning.ProvisioningService, backgroundServiceProvider registry.BackgroundServiceRegistry,
|
||||
moduleService modules.Engine,
|
||||
) (*Server, error) {
|
||||
rootCtx, shutdownFn := context.WithCancel(context.Background())
|
||||
childRoutines, childCtx := errgroup.WithContext(rootCtx)
|
||||
@@ -76,7 +73,6 @@ func newServer(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleR
|
||||
commit: opts.Commit,
|
||||
buildBranch: opts.BuildBranch,
|
||||
backgroundServices: backgroundServiceProvider.GetServices(),
|
||||
moduleService: moduleService,
|
||||
}
|
||||
|
||||
return s, nil
|
||||
@@ -103,11 +99,10 @@ type Server struct {
|
||||
HTTPServer *api.HTTPServer
|
||||
roleRegistry accesscontrol.RoleRegistry
|
||||
provisioningService provisioning.ProvisioningService
|
||||
moduleService modules.Engine
|
||||
}
|
||||
|
||||
// init initializes the server and its services.
|
||||
func (s *Server) init() error {
|
||||
// Init initializes the server and its services.
|
||||
func (s *Server) Init() error {
|
||||
s.mtx.Lock()
|
||||
defer s.mtx.Unlock()
|
||||
|
||||
@@ -120,11 +115,6 @@ func (s *Server) init() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Initialize dskit modules.
|
||||
if err := s.moduleService.Init(s.context); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := metrics.SetEnvironmentInformation(s.cfg.MetricsGrafanaEnvironmentInfo); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -141,19 +131,10 @@ func (s *Server) init() error {
|
||||
func (s *Server) Run() error {
|
||||
defer close(s.shutdownFinished)
|
||||
|
||||
if err := s.init(); err != nil {
|
||||
if err := s.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Start dskit modules.
|
||||
s.childRoutines.Go(func() error {
|
||||
err := s.moduleService.Run(s.context)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
services := s.backgroundServices
|
||||
|
||||
// Start background services.
|
||||
@@ -197,9 +178,6 @@ func (s *Server) Shutdown(ctx context.Context, reason string) error {
|
||||
var err error
|
||||
s.shutdownOnce.Do(func() {
|
||||
s.log.Info("Shutdown started", "reason", reason)
|
||||
if err := s.moduleService.Shutdown(ctx); err != nil {
|
||||
s.log.Error("Failed to shutdown modules", "error", err)
|
||||
}
|
||||
// Call cancel func to stop background services.
|
||||
s.shutdownFn()
|
||||
// Wait for server to shut down
|
||||
|
||||
@@ -48,7 +48,7 @@ func (s *testService) IsDisabled() bool {
|
||||
|
||||
func testServer(t *testing.T, services ...registry.BackgroundService) *Server {
|
||||
t.Helper()
|
||||
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...), &MockModuleService{})
|
||||
s, err := newServer(Options{}, setting.NewCfg(), nil, &acimpl.Service{}, nil, backgroundsvcs.NewBackgroundServiceRegistry(services...))
|
||||
require.NoError(t, err)
|
||||
// Required to skip configuration initialization that causes
|
||||
// DI errors in this test.
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/dskit/services"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type coreService struct {
|
||||
*services.BasicService
|
||||
cla setting.CommandLineArgs
|
||||
opts Options
|
||||
apiOpts api.ServerOptions
|
||||
server *Server
|
||||
}
|
||||
|
||||
func NewService(opts Options, apiOpts api.ServerOptions) (*coreService, error) {
|
||||
s := &coreService{
|
||||
opts: opts,
|
||||
apiOpts: apiOpts,
|
||||
}
|
||||
s.BasicService = services.NewBasicService(s.start, s.running, s.stop)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *coreService) start(_ context.Context) error {
|
||||
serv, err := Initialize(s.cla, s.opts, s.apiOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.server = serv
|
||||
return s.server.Init()
|
||||
}
|
||||
|
||||
func (s *coreService) running(_ context.Context) error {
|
||||
return s.server.Run()
|
||||
}
|
||||
|
||||
func (s *coreService) stop(failureReason error) error {
|
||||
return s.server.Shutdown(context.Background(), failureReason.Error())
|
||||
}
|
||||
@@ -29,7 +29,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/login/social"
|
||||
"github.com/grafana/grafana/pkg/middleware/csrf"
|
||||
"github.com/grafana/grafana/pkg/middleware/loggermw"
|
||||
"github.com/grafana/grafana/pkg/modules"
|
||||
pluginDashboards "github.com/grafana/grafana/pkg/plugins/manager/dashboards"
|
||||
"github.com/grafana/grafana/pkg/registry/corekind"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
@@ -42,7 +41,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/auth/jwt"
|
||||
"github.com/grafana/grafana/pkg/services/authn"
|
||||
"github.com/grafana/grafana/pkg/services/authn/authnimpl"
|
||||
"github.com/grafana/grafana/pkg/services/certgenerator"
|
||||
"github.com/grafana/grafana/pkg/services/cleanup"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||
"github.com/grafana/grafana/pkg/services/correlations"
|
||||
@@ -62,7 +60,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/folder"
|
||||
"github.com/grafana/grafana/pkg/services/folder/folderimpl"
|
||||
grafanaapiserver "github.com/grafana/grafana/pkg/services/grafana-apiserver"
|
||||
"github.com/grafana/grafana/pkg/services/grpcserver"
|
||||
grpccontext "github.com/grafana/grafana/pkg/services/grpcserver/context"
|
||||
"github.com/grafana/grafana/pkg/services/grpcserver/interceptors"
|
||||
@@ -202,7 +199,6 @@ var wireBasicSet = wire.NewSet(
|
||||
wire.Bind(new(httpclient.Provider), new(*sdkhttpclient.Provider)),
|
||||
serverlock.ProvideService,
|
||||
annotationsimpl.ProvideCleanupService,
|
||||
certgenerator.WireSet,
|
||||
wire.Bind(new(annotations.Cleaner), new(*annotationsimpl.CleanupServiceImpl)),
|
||||
cleanup.ProvideService,
|
||||
shorturlimpl.ProvideService,
|
||||
@@ -353,11 +349,9 @@ var wireBasicSet = wire.NewSet(
|
||||
wire.Bind(new(authn.Service), new(*authnimpl.Service)),
|
||||
wire.Bind(new(authn.IdentitySynchronizer), new(*authnimpl.Service)),
|
||||
supportbundlesimpl.ProvideService,
|
||||
grafanaapiserver.WireSet,
|
||||
oasimpl.ProvideService,
|
||||
wire.Bind(new(oauthserver.OAuth2Server), new(*oasimpl.OAuth2ServiceImpl)),
|
||||
loggermw.Provide,
|
||||
modules.WireSet,
|
||||
signingkeysimpl.ProvideEmbeddedSigningKeysService,
|
||||
wire.Bind(new(signingkeys.Service), new(*signingkeysimpl.Service)),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user