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>master
							parent
							
								
									4acda57e05
								
							
						
					
					
						commit
						a20abfbf3c
					
				|  | @ -27,7 +27,7 @@ type Configuration struct { | ||||||
| 	Auth Auth `yaml:"auth,omitempty"` | 	Auth Auth `yaml:"auth,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// Middleware lists all middlewares to be used by the registry.
 | 	// Middleware lists all middlewares to be used by the registry.
 | ||||||
| 	Middleware []Middleware `yaml:"middleware,omitempty"` | 	Middleware map[string][]Middleware `yaml:"middleware,omitempty"` | ||||||
| 
 | 
 | ||||||
| 	// Reporting is the configuration for error reporting
 | 	// Reporting is the configuration for error reporting
 | ||||||
| 	Reporting Reporting `yaml:"reporting,omitempty"` | 	Reporting Reporting `yaml:"reporting,omitempty"` | ||||||
|  | @ -299,10 +299,6 @@ type NewRelicReporting struct { | ||||||
| type Middleware struct { | type Middleware struct { | ||||||
| 	// Name the middleware registers itself as
 | 	// Name the middleware registers itself as
 | ||||||
| 	Name string `yaml:"name"` | 	Name string `yaml:"name"` | ||||||
| 	// Injection point the middleware should be applied at
 |  | ||||||
| 	// N.B. ensure the middleware is applicable for the named injection point, middlewares
 |  | ||||||
| 	//      for different injection points are not interchangeable.
 |  | ||||||
| 	Inject string `yaml:"inject"` |  | ||||||
| 	// Flag to disable middleware easily
 | 	// Flag to disable middleware easily
 | ||||||
| 	Disabled bool `yaml:"Disabled,omitempty"` | 	Disabled bool `yaml:"Disabled,omitempty"` | ||||||
| 	// Map of parameters that will be passed to the middleware's initialization function
 | 	// Map of parameters that will be passed to the middleware's initialization function
 | ||||||
|  |  | ||||||
|  | @ -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,22 +111,12 @@ 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
 | 		if err != nil { | ||||||
| 			panic(fmt.Sprintf("unable to configure registry middleware (%s): %v", mw.Name, err)) | 			panic(fmt.Sprintf("unable to configure storage 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 |  | ||||||
| 		} | 		} | ||||||
|  | 		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) | ||||||
|  |  | ||||||
|  | @ -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) | ||||||
|  | } | ||||||
|  | @ -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