From 66d151df772c88aa99ac5bb260cfdbd5838f26fa Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Mon, 7 Sep 2020 13:58:03 +0200 Subject: [PATCH] Improve plugins builder. --- pkg/plugins/builder.go | 12 ++++++++++-- pkg/server/middleware/middlewares.go | 9 ++++----- pkg/server/middleware/plugins.go | 6 ++++++ pkg/server/routerfactory.go | 5 ++--- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pkg/plugins/builder.go b/pkg/plugins/builder.go index 448250568..58527475c 100644 --- a/pkg/plugins/builder.go +++ b/pkg/plugins/builder.go @@ -15,6 +15,9 @@ import ( const devPluginName = "dev" +// Constructor creates a plugin handler. +type Constructor func(context.Context, http.Handler) (http.Handler, error) + // pluginContext The static part of a plugin configuration. type pluginContext struct { // GoPath plugin's GOPATH @@ -89,7 +92,7 @@ func NewBuilder(client *Client, plugins map[string]Descriptor, devPlugin *DevPlu } // Build builds a plugin. -func (b Builder) Build(pName string, config map[string]interface{}, middlewareName string) (*Middleware, error) { +func (b Builder) Build(pName string, config map[string]interface{}, middlewareName string) (Constructor, error) { if b.descriptors == nil { return nil, fmt.Errorf("plugin: no plugin definition in the static configuration: %s", pName) } @@ -99,7 +102,12 @@ func (b Builder) Build(pName string, config map[string]interface{}, middlewareNa return nil, fmt.Errorf("plugin: unknown plugin type: %s", pName) } - return newMiddleware(descriptor, config, middlewareName) + m, err := newMiddleware(descriptor, config, middlewareName) + if err != nil { + return nil, err + } + + return m.NewHandler, err } // Middleware is a HTTP handler plugin wrapper. diff --git a/pkg/server/middleware/middlewares.go b/pkg/server/middleware/middlewares.go index e14faad99..e38286bec 100644 --- a/pkg/server/middleware/middlewares.go +++ b/pkg/server/middleware/middlewares.go @@ -28,7 +28,6 @@ import ( "github.com/containous/traefik/v2/pkg/middlewares/stripprefix" "github.com/containous/traefik/v2/pkg/middlewares/stripprefixregex" "github.com/containous/traefik/v2/pkg/middlewares/tracing" - "github.com/containous/traefik/v2/pkg/plugins" "github.com/containous/traefik/v2/pkg/server/provider" ) @@ -41,7 +40,7 @@ const ( // Builder the middleware builder. type Builder struct { configs map[string]*runtime.MiddlewareInfo - pluginBuilder *plugins.Builder + pluginBuilder PluginsBuilder serviceBuilder serviceBuilder } @@ -50,7 +49,7 @@ type serviceBuilder interface { } // NewBuilder creates a new Builder. -func NewBuilder(configs map[string]*runtime.MiddlewareInfo, serviceBuilder serviceBuilder, pluginBuilder *plugins.Builder) *Builder { +func NewBuilder(configs map[string]*runtime.MiddlewareInfo, serviceBuilder serviceBuilder, pluginBuilder PluginsBuilder) *Builder { return &Builder{configs: configs, serviceBuilder: serviceBuilder, pluginBuilder: pluginBuilder} } @@ -351,13 +350,13 @@ func (b *Builder) buildConstructor(ctx context.Context, middlewareName string) ( return nil, err } - m, err := b.pluginBuilder.Build(pluginType, rawPluginConfig, middlewareName) + plug, err := b.pluginBuilder.Build(pluginType, rawPluginConfig, middlewareName) if err != nil { return nil, err } middleware = func(next http.Handler) (http.Handler, error) { - return m.NewHandler(ctx, next) + return plug(ctx, next) } } diff --git a/pkg/server/middleware/plugins.go b/pkg/server/middleware/plugins.go index 80b2b4069..da2d60970 100644 --- a/pkg/server/middleware/plugins.go +++ b/pkg/server/middleware/plugins.go @@ -5,8 +5,14 @@ import ( "fmt" "github.com/containous/traefik/v2/pkg/config/dynamic" + "github.com/containous/traefik/v2/pkg/plugins" ) +// PluginsBuilder the plugin's builder interface. +type PluginsBuilder interface { + Build(pName string, config map[string]interface{}, middlewareName string) (plugins.Constructor, error) +} + func findPluginConfig(rawConfig map[string]dynamic.PluginConf) (string, map[string]interface{}, error) { if len(rawConfig) != 1 { return "", nil, errors.New("plugin: invalid configuration: no configuration or too many plugin definition") diff --git a/pkg/server/routerfactory.go b/pkg/server/routerfactory.go index 53d53bc48..2c0baec81 100644 --- a/pkg/server/routerfactory.go +++ b/pkg/server/routerfactory.go @@ -6,7 +6,6 @@ import ( "github.com/containous/traefik/v2/pkg/config/runtime" "github.com/containous/traefik/v2/pkg/config/static" "github.com/containous/traefik/v2/pkg/log" - "github.com/containous/traefik/v2/pkg/plugins" "github.com/containous/traefik/v2/pkg/server/middleware" "github.com/containous/traefik/v2/pkg/server/router" routertcp "github.com/containous/traefik/v2/pkg/server/router/tcp" @@ -26,14 +25,14 @@ type RouterFactory struct { managerFactory *service.ManagerFactory - pluginBuilder *plugins.Builder + pluginBuilder middleware.PluginsBuilder chainBuilder *middleware.ChainBuilder tlsManager *tls.Manager } // NewRouterFactory creates a new RouterFactory. -func NewRouterFactory(staticConfiguration static.Configuration, managerFactory *service.ManagerFactory, tlsManager *tls.Manager, chainBuilder *middleware.ChainBuilder, pluginBuilder *plugins.Builder) *RouterFactory { +func NewRouterFactory(staticConfiguration static.Configuration, managerFactory *service.ManagerFactory, tlsManager *tls.Manager, chainBuilder *middleware.ChainBuilder, pluginBuilder middleware.PluginsBuilder) *RouterFactory { var entryPointsTCP, entryPointsUDP []string for name, cfg := range staticConfiguration.EntryPoints { protocol, err := cfg.GetProtocol()