lint: Move out config reloader context from controller struct

Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
Petu Eusebiu
2022-03-24 14:49:51 +02:00
committed by Ramkumar Chinchani
parent 353b0c6034
commit be910cf01c
17 changed files with 91 additions and 84 deletions
+15 -25
View File
@@ -31,16 +31,14 @@ const (
)
type Controller struct {
Config *config.Config
Router *mux.Router
StoreController storage.StoreController
Log log.Logger
Audit *log.Logger
Server *http.Server
Metrics monitoring.MetricServer
wgShutDown *goSync.WaitGroup // use it to gracefully shutdown goroutines
reloadCtx context.Context // use it to gracefully reload goroutines with new configuration
cancelOnReloadFunc context.CancelFunc // use it to stop goroutines
Config *config.Config
Router *mux.Router
StoreController storage.StoreController
Log log.Logger
Audit *log.Logger
Server *http.Server
Metrics monitoring.MetricServer
wgShutDown *goSync.WaitGroup // use it to gracefully shutdown goroutines
}
func NewController(config *config.Config) *Controller {
@@ -50,9 +48,6 @@ func NewController(config *config.Config) *Controller {
controller.Config = config
controller.Log = logger
controller.wgShutDown = new(goSync.WaitGroup)
/* context used to cancel go routines so that
we can change their config on the fly (restart routines with different config) */
controller.reloadCtx, controller.cancelOnReloadFunc = context.WithCancel(context.Background())
if config.Log.Audit != "" {
audit := log.NewAuditLogger(config.Log.Level, config.Log.Audit)
@@ -106,7 +101,7 @@ func DumpRuntimeParams(log log.Logger) {
evt.Msg("runtime params")
}
func (c *Controller) Run() error {
func (c *Controller) Run(reloadCtx context.Context) error {
// validate configuration
if err := c.Config.Validate(c.Log); err != nil {
c.Log.Error().Err(err).Msg("configuration validation failed")
@@ -157,13 +152,14 @@ func (c *Controller) Run() error {
c.Metrics = monitoring.NewMetricsServer(enabled, c.Log)
if err := c.InitImageStore(); err != nil {
if err := c.InitImageStore(reloadCtx); err != nil {
return err
}
monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion,
c.Config.DistSpecVersion)
// nolint: contextcheck
_ = NewRouteHandler(c)
addr := fmt.Sprintf("%s:%s", c.Config.HTTP.Address, c.Config.HTTP.Port)
@@ -225,7 +221,7 @@ func (c *Controller) Run() error {
return server.Serve(listener)
}
func (c *Controller) InitImageStore() error {
func (c *Controller) InitImageStore(reloadCtx context.Context) error {
c.StoreController = storage.StoreController{}
if c.Config.Storage.RootDirectory != "" {
@@ -326,28 +322,22 @@ func (c *Controller) InitImageStore() error {
// Enable extensions if extension config is provided
if c.Config.Extensions != nil && c.Config.Extensions.Sync != nil && *c.Config.Extensions.Sync.Enable {
ext.EnableSyncExtension(c.reloadCtx, c.Config, c.wgShutDown, c.StoreController, c.Log)
ext.EnableSyncExtension(reloadCtx, c.Config, c.wgShutDown, c.StoreController, c.Log)
}
return nil
}
func (c *Controller) LoadNewConfig(config *config.Config) {
// cancel go routines context so we can reload configuration
c.cancelOnReloadFunc()
func (c *Controller) LoadNewConfig(reloadCtx context.Context, config *config.Config) {
// reload access control config
c.Config.AccessControl = config.AccessControl
c.Config.HTTP.RawAccessControl = config.HTTP.RawAccessControl
// create new context for the next config reload
c.reloadCtx, c.cancelOnReloadFunc = context.WithCancel(context.Background())
// Enable extensions if extension config is provided
if config.Extensions != nil && config.Extensions.Sync != nil {
// reload sync config
c.Config.Extensions.Sync = config.Extensions.Sync
ext.EnableSyncExtension(c.reloadCtx, c.Config, c.wgShutDown, c.StoreController, c.Log)
ext.EnableSyncExtension(reloadCtx, c.Config, c.wgShutDown, c.StoreController, c.Log)
} else if c.Config.Extensions != nil {
c.Config.Extensions.Sync = nil
}
+7 -6
View File
@@ -115,7 +115,7 @@ func TestRunAlreadyRunningServer(t *testing.T) {
ctlr.Config.Storage.RootDirectory = globalDir
go func() {
if err := ctlr.Run(); err != nil {
if err := ctlr.Run(context.Background()); err != nil {
return
}
}()
@@ -135,7 +135,7 @@ func TestRunAlreadyRunningServer(t *testing.T) {
_ = ctlr.Server.Shutdown(ctx)
}()
err := ctlr.Run()
err := ctlr.Run(context.Background())
So(err, ShouldNotBeNil)
})
}
@@ -156,7 +156,7 @@ func TestObjectStorageController(t *testing.T) {
ctlr.Config.Storage.RootDirectory = "zot"
err := ctlr.Run()
err := ctlr.Run(context.Background())
So(err, ShouldNotBeNil)
})
@@ -770,7 +770,7 @@ func TestMultipleInstance(t *testing.T) {
},
}
ctlr := api.NewController(conf)
err := ctlr.Run()
err := ctlr.Run(context.Background())
So(err, ShouldEqual, errors.ErrImgStoreNotFound)
globalDir := t.TempDir()
@@ -3006,7 +3006,7 @@ func TestImageSignatures(t *testing.T) {
ctlr.Config.Storage.RootDirectory = dir
go func(controller *api.Controller) {
// this blocks
if err := controller.Run(); err != nil {
if err := controller.Run(context.Background()); err != nil {
return
}
}(ctlr)
@@ -4198,7 +4198,8 @@ func getAllManifests(imagePath string) []string {
func startServer(c *api.Controller) {
// this blocks
if err := c.Run(); err != nil {
ctx := context.Background()
if err := c.Run(ctx); err != nil {
return
}
}
+2
View File
@@ -42,6 +42,7 @@ type RouteHandler struct {
c *Controller
}
// nolint: contextcheck
func NewRouteHandler(c *Controller) *RouteHandler {
rh := &RouteHandler{c: c}
rh.SetupRoutes()
@@ -53,6 +54,7 @@ func allowedMethods(method string) []string {
return []string{http.MethodOptions, method}
}
// nolint: contextcheck
func (rh *RouteHandler) SetupRoutes() {
rh.c.Router.Use(AuthHandler(rh.c))
// authz is being enabled because authn is found