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:
Andy Goldstein 2015-03-06 10:45:16 -05:00 committed by David Lawrence
parent 952f39edff
commit 30bcc17b85
5 changed files with 107 additions and 21 deletions

View file

@ -13,6 +13,8 @@ import (
"github.com/docker/distribution/notifications"
"github.com/docker/distribution/registry/api/v2"
"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"
storagedriver "github.com/docker/distribution/registry/storage/driver"
"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.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()
if authType != "" {
@ -100,22 +111,12 @@ func NewApp(ctx context.Context, configuration configuration.Configuration) *App
app.accessController = accessController
}
for _, mw := range configuration.Middleware {
if mw.Inject == "registry" {
// 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 {
panic(fmt.Sprintf("unable to configure storage middleware (%s): %v", mw.Name, err))
}
app.driver = smw
for _, mw := range configuration.Middleware["storage"] {
smw, err := storagemiddleware.Get(mw.Name, mw.Options, app.driver)
if err != nil {
panic(fmt.Sprintf("unable to configure storage middleware (%s): %v", mw.Name, err))
}
app.driver = smw
}
return app
@ -256,6 +257,14 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
context.Repository = notifications.Listen(
repository,
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)

View 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)
}

View 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)
}

View file

@ -115,5 +115,5 @@ func (lh *cloudFrontStorageMiddleware) URLFor(path string, options map[string]in
// init registers the cloudfront layerHandler backend.
func init() {
storagemiddleware.RegisterStorageMiddleware("cloudfront", storagemiddleware.InitFunc(newCloudFrontStorageMiddleware))
storagemiddleware.Register("cloudfront", storagemiddleware.InitFunc(newCloudFrontStorageMiddleware))
}

View file

@ -12,9 +12,9 @@ type InitFunc func(storageDriver storagedriver.StorageDriver, options map[string
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.
func RegisterStorageMiddleware(name string, initFunc InitFunc) error {
func Register(name string, initFunc InitFunc) error {
if storageMiddlewares == nil {
storageMiddlewares = make(map[string]InitFunc)
}
@ -27,9 +27,8 @@ func RegisterStorageMiddleware(name string, initFunc InitFunc) error {
return nil
}
// GetStorageMiddleware constructs a StorageMiddleware
// with the given options using the named backend.
func GetStorageMiddleware(name string, options map[string]interface{}, storageDriver storagedriver.StorageDriver) (storagedriver.StorageDriver, error) {
// Get constructs a StorageMiddleware with the given options using the named backend.
func Get(name string, options map[string]interface{}, storageDriver storagedriver.StorageDriver) (storagedriver.StorageDriver, error) {
if storageMiddlewares != nil {
if initFunc, exists := storageMiddlewares[name]; exists {
return initFunc(storageDriver, options)