Improve plugins builder.

This commit is contained in:
Ludovic Fernandez 2020-09-07 13:58:03 +02:00 committed by GitHub
parent 1dbee90d34
commit 66d151df77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 10 deletions

View file

@ -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.

View file

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

View file

@ -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")

View file

@ -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()