Improve plugins builder.
This commit is contained in:
parent
1dbee90d34
commit
66d151df77
4 changed files with 22 additions and 10 deletions
|
@ -15,6 +15,9 @@ import (
|
||||||
|
|
||||||
const devPluginName = "dev"
|
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.
|
// pluginContext The static part of a plugin configuration.
|
||||||
type pluginContext struct {
|
type pluginContext struct {
|
||||||
// GoPath plugin's GOPATH
|
// GoPath plugin's GOPATH
|
||||||
|
@ -89,7 +92,7 @@ func NewBuilder(client *Client, plugins map[string]Descriptor, devPlugin *DevPlu
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build builds a plugin.
|
// 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 {
|
if b.descriptors == nil {
|
||||||
return nil, fmt.Errorf("plugin: no plugin definition in the static configuration: %s", pName)
|
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 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.
|
// Middleware is a HTTP handler plugin wrapper.
|
||||||
|
|
|
@ -28,7 +28,6 @@ import (
|
||||||
"github.com/containous/traefik/v2/pkg/middlewares/stripprefix"
|
"github.com/containous/traefik/v2/pkg/middlewares/stripprefix"
|
||||||
"github.com/containous/traefik/v2/pkg/middlewares/stripprefixregex"
|
"github.com/containous/traefik/v2/pkg/middlewares/stripprefixregex"
|
||||||
"github.com/containous/traefik/v2/pkg/middlewares/tracing"
|
"github.com/containous/traefik/v2/pkg/middlewares/tracing"
|
||||||
"github.com/containous/traefik/v2/pkg/plugins"
|
|
||||||
"github.com/containous/traefik/v2/pkg/server/provider"
|
"github.com/containous/traefik/v2/pkg/server/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -41,7 +40,7 @@ const (
|
||||||
// Builder the middleware builder.
|
// Builder the middleware builder.
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
configs map[string]*runtime.MiddlewareInfo
|
configs map[string]*runtime.MiddlewareInfo
|
||||||
pluginBuilder *plugins.Builder
|
pluginBuilder PluginsBuilder
|
||||||
serviceBuilder serviceBuilder
|
serviceBuilder serviceBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +49,7 @@ type serviceBuilder interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuilder creates a new Builder.
|
// 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}
|
return &Builder{configs: configs, serviceBuilder: serviceBuilder, pluginBuilder: pluginBuilder}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,13 +350,13 @@ func (b *Builder) buildConstructor(ctx context.Context, middlewareName string) (
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := b.pluginBuilder.Build(pluginType, rawPluginConfig, middlewareName)
|
plug, err := b.pluginBuilder.Build(pluginType, rawPluginConfig, middlewareName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
middleware = func(next http.Handler) (http.Handler, error) {
|
middleware = func(next http.Handler) (http.Handler, error) {
|
||||||
return m.NewHandler(ctx, next)
|
return plug(ctx, next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
"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) {
|
func findPluginConfig(rawConfig map[string]dynamic.PluginConf) (string, map[string]interface{}, error) {
|
||||||
if len(rawConfig) != 1 {
|
if len(rawConfig) != 1 {
|
||||||
return "", nil, errors.New("plugin: invalid configuration: no configuration or too many plugin definition")
|
return "", nil, errors.New("plugin: invalid configuration: no configuration or too many plugin definition")
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"github.com/containous/traefik/v2/pkg/config/runtime"
|
"github.com/containous/traefik/v2/pkg/config/runtime"
|
||||||
"github.com/containous/traefik/v2/pkg/config/static"
|
"github.com/containous/traefik/v2/pkg/config/static"
|
||||||
"github.com/containous/traefik/v2/pkg/log"
|
"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/middleware"
|
||||||
"github.com/containous/traefik/v2/pkg/server/router"
|
"github.com/containous/traefik/v2/pkg/server/router"
|
||||||
routertcp "github.com/containous/traefik/v2/pkg/server/router/tcp"
|
routertcp "github.com/containous/traefik/v2/pkg/server/router/tcp"
|
||||||
|
@ -26,14 +25,14 @@ type RouterFactory struct {
|
||||||
|
|
||||||
managerFactory *service.ManagerFactory
|
managerFactory *service.ManagerFactory
|
||||||
|
|
||||||
pluginBuilder *plugins.Builder
|
pluginBuilder middleware.PluginsBuilder
|
||||||
|
|
||||||
chainBuilder *middleware.ChainBuilder
|
chainBuilder *middleware.ChainBuilder
|
||||||
tlsManager *tls.Manager
|
tlsManager *tls.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRouterFactory creates a new RouterFactory.
|
// 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
|
var entryPointsTCP, entryPointsUDP []string
|
||||||
for name, cfg := range staticConfiguration.EntryPoints {
|
for name, cfg := range staticConfiguration.EntryPoints {
|
||||||
protocol, err := cfg.GetProtocol()
|
protocol, err := cfg.GetProtocol()
|
||||||
|
|
Loading…
Reference in a new issue