Middleware!
Convert middleware in the config to be a map of type->[]Middleware Add support for registry & repository middleware. Some naming updates as well. Signed-off-by: Andy Goldstein <agoldste@redhat.com>
This commit is contained in:
parent
952f39edff
commit
30bcc17b85
@ -13,6 +13,8 @@ import (
|
|||||||
"github.com/docker/distribution/notifications"
|
"github.com/docker/distribution/notifications"
|
||||||
"github.com/docker/distribution/registry/api/v2"
|
"github.com/docker/distribution/registry/api/v2"
|
||||||
"github.com/docker/distribution/registry/auth"
|
"github.com/docker/distribution/registry/auth"
|
||||||
|
registrymiddleware "github.com/docker/distribution/registry/middleware/registry"
|
||||||
|
repositorymiddleware "github.com/docker/distribution/registry/middleware/repository"
|
||||||
"github.com/docker/distribution/registry/storage"
|
"github.com/docker/distribution/registry/storage"
|
||||||
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
||||||
"github.com/docker/distribution/registry/storage/driver/factory"
|
"github.com/docker/distribution/registry/storage/driver/factory"
|
||||||
@ -89,7 +91,16 @@ func NewApp(ctx context.Context, configuration configuration.Configuration) *App
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.configureEvents(&configuration)
|
app.configureEvents(&configuration)
|
||||||
|
|
||||||
app.registry = storage.NewRegistryWithDriver(app.driver)
|
app.registry = storage.NewRegistryWithDriver(app.driver)
|
||||||
|
for _, mw := range configuration.Middleware["registry"] {
|
||||||
|
rmw, err := registrymiddleware.Get(mw.Name, mw.Options, app.registry)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("unable to configure registry middleware (%s): %s", mw.Name, err))
|
||||||
|
}
|
||||||
|
app.registry = rmw
|
||||||
|
}
|
||||||
|
|
||||||
authType := configuration.Auth.Type()
|
authType := configuration.Auth.Type()
|
||||||
|
|
||||||
if authType != "" {
|
if authType != "" {
|
||||||
@ -100,23 +111,13 @@ func NewApp(ctx context.Context, configuration configuration.Configuration) *App
|
|||||||
app.accessController = accessController
|
app.accessController = accessController
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, mw := range configuration.Middleware {
|
for _, mw := range configuration.Middleware["storage"] {
|
||||||
if mw.Inject == "registry" {
|
smw, err := storagemiddleware.Get(mw.Name, mw.Options, app.driver)
|
||||||
// registry middleware can director wrap app.registry identically to storage middlewares with driver
|
|
||||||
panic(fmt.Sprintf("unable to configure registry middleware (%s): %v", mw.Name, err))
|
|
||||||
} else if mw.Inject == "repository" {
|
|
||||||
// we have to do something more intelligent with repository middleware, It needs to be staged
|
|
||||||
// for later to be wrapped around the repository at request time.
|
|
||||||
panic(fmt.Sprintf("unable to configure repository middleware (%s): %v", mw.Name, err))
|
|
||||||
} else if mw.Inject == "storage" {
|
|
||||||
smw, err := storagemiddleware.GetStorageMiddleware(mw.Name, mw.Options, app.driver)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("unable to configure storage middleware (%s): %v", mw.Name, err))
|
panic(fmt.Sprintf("unable to configure storage middleware (%s): %v", mw.Name, err))
|
||||||
}
|
}
|
||||||
app.driver = smw
|
app.driver = smw
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
@ -256,6 +257,14 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
|
|||||||
context.Repository = notifications.Listen(
|
context.Repository = notifications.Listen(
|
||||||
repository,
|
repository,
|
||||||
app.eventBridge(context, r))
|
app.eventBridge(context, r))
|
||||||
|
|
||||||
|
for _, mw := range app.Config.Middleware["repository"] {
|
||||||
|
rmw, err := repositorymiddleware.Get(mw.Name, mw.Options, context.Repository)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("unable to configure repository middleware (%s): %s", mw.Name, err))
|
||||||
|
}
|
||||||
|
context.Repository = rmw
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := dispatch(context, r)
|
handler := dispatch(context, r)
|
||||||
|
39
docs/middleware/registry/middleware.go
Normal file
39
docs/middleware/registry/middleware.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/docker/distribution"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InitFunc is the type of a RegistryMiddleware factory function and is
|
||||||
|
// used to register the contsructor for different RegistryMiddleware backends.
|
||||||
|
type InitFunc func(registry distribution.Registry, options map[string]interface{}) (distribution.Registry, error)
|
||||||
|
|
||||||
|
var middlewares map[string]InitFunc
|
||||||
|
|
||||||
|
// Register is used to register an InitFunc for
|
||||||
|
// a RegistryMiddleware backend with the given name.
|
||||||
|
func Register(name string, initFunc InitFunc) error {
|
||||||
|
if middlewares == nil {
|
||||||
|
middlewares = make(map[string]InitFunc)
|
||||||
|
}
|
||||||
|
if _, exists := middlewares[name]; exists {
|
||||||
|
return fmt.Errorf("name already registered: %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
middlewares[name] = initFunc
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get constructs a RegistryMiddleware with the given options using the named backend.
|
||||||
|
func Get(name string, options map[string]interface{}, registry distribution.Registry) (distribution.Registry, error) {
|
||||||
|
if middlewares != nil {
|
||||||
|
if initFunc, exists := middlewares[name]; exists {
|
||||||
|
return initFunc(registry, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("no registry middleware registered with name: %s", name)
|
||||||
|
}
|
39
docs/middleware/repository/middleware.go
Normal file
39
docs/middleware/repository/middleware.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/docker/distribution"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InitFunc is the type of a RepositoryMiddleware factory function and is
|
||||||
|
// used to register the contsructor for different RepositoryMiddleware backends.
|
||||||
|
type InitFunc func(repository distribution.Repository, options map[string]interface{}) (distribution.Repository, error)
|
||||||
|
|
||||||
|
var middlewares map[string]InitFunc
|
||||||
|
|
||||||
|
// Register is used to register an InitFunc for
|
||||||
|
// a RepositoryMiddleware backend with the given name.
|
||||||
|
func Register(name string, initFunc InitFunc) error {
|
||||||
|
if middlewares == nil {
|
||||||
|
middlewares = make(map[string]InitFunc)
|
||||||
|
}
|
||||||
|
if _, exists := middlewares[name]; exists {
|
||||||
|
return fmt.Errorf("name already registered: %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
middlewares[name] = initFunc
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get constructs a RepositoryMiddleware with the given options using the named backend.
|
||||||
|
func Get(name string, options map[string]interface{}, repository distribution.Repository) (distribution.Repository, error) {
|
||||||
|
if middlewares != nil {
|
||||||
|
if initFunc, exists := middlewares[name]; exists {
|
||||||
|
return initFunc(repository, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("no repository middleware registered with name: %s", name)
|
||||||
|
}
|
@ -115,5 +115,5 @@ func (lh *cloudFrontStorageMiddleware) URLFor(path string, options map[string]in
|
|||||||
|
|
||||||
// init registers the cloudfront layerHandler backend.
|
// init registers the cloudfront layerHandler backend.
|
||||||
func init() {
|
func init() {
|
||||||
storagemiddleware.RegisterStorageMiddleware("cloudfront", storagemiddleware.InitFunc(newCloudFrontStorageMiddleware))
|
storagemiddleware.Register("cloudfront", storagemiddleware.InitFunc(newCloudFrontStorageMiddleware))
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,9 @@ type InitFunc func(storageDriver storagedriver.StorageDriver, options map[string
|
|||||||
|
|
||||||
var storageMiddlewares map[string]InitFunc
|
var storageMiddlewares map[string]InitFunc
|
||||||
|
|
||||||
// RegisterStorageMiddleware is used to register an StorageMiddlewareInitFunc for
|
// Register is used to register an InitFunc for
|
||||||
// a StorageMiddleware backend with the given name.
|
// a StorageMiddleware backend with the given name.
|
||||||
func RegisterStorageMiddleware(name string, initFunc InitFunc) error {
|
func Register(name string, initFunc InitFunc) error {
|
||||||
if storageMiddlewares == nil {
|
if storageMiddlewares == nil {
|
||||||
storageMiddlewares = make(map[string]InitFunc)
|
storageMiddlewares = make(map[string]InitFunc)
|
||||||
}
|
}
|
||||||
@ -27,9 +27,8 @@ func RegisterStorageMiddleware(name string, initFunc InitFunc) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStorageMiddleware constructs a StorageMiddleware
|
// Get constructs a StorageMiddleware with the given options using the named backend.
|
||||||
// with the given options using the named backend.
|
func Get(name string, options map[string]interface{}, storageDriver storagedriver.StorageDriver) (storagedriver.StorageDriver, error) {
|
||||||
func GetStorageMiddleware(name string, options map[string]interface{}, storageDriver storagedriver.StorageDriver) (storagedriver.StorageDriver, error) {
|
|
||||||
if storageMiddlewares != nil {
|
if storageMiddlewares != nil {
|
||||||
if initFunc, exists := storageMiddlewares[name]; exists {
|
if initFunc, exists := storageMiddlewares[name]; exists {
|
||||||
return initFunc(storageDriver, options)
|
return initFunc(storageDriver, options)
|
||||||
|
Loading…
Reference in New Issue
Block a user