traefik/pkg/plugins/builder.go

185 lines
4.8 KiB
Go
Raw Permalink Normal View History

2020-04-20 16:36:34 +00:00
package plugins
import (
"context"
2024-02-08 13:15:45 +00:00
"errors"
2020-04-20 16:36:34 +00:00
"fmt"
"net/http"
"path/filepath"
2020-04-20 16:36:34 +00:00
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/log"
2020-04-20 16:36:34 +00:00
)
2020-09-07 11:58:03 +00:00
// Constructor creates a plugin handler.
type Constructor func(context.Context, http.Handler) (http.Handler, error)
type pluginMiddleware interface {
NewHandler(ctx context.Context, next http.Handler) (http.Handler, error)
}
type middlewareBuilder interface {
newMiddleware(config map[string]interface{}, middlewareName string) (pluginMiddleware, error)
}
2020-04-20 16:36:34 +00:00
// Builder is a plugin builder.
type Builder struct {
providerBuilders map[string]providerBuilder
middlewareBuilders map[string]middlewareBuilder
2020-04-20 16:36:34 +00:00
}
// NewBuilder creates a new Builder.
func NewBuilder(client *Client, plugins map[string]Descriptor, localPlugins map[string]LocalDescriptor) (*Builder, error) {
ctx := context.Background()
2020-04-20 16:36:34 +00:00
pb := &Builder{
middlewareBuilders: map[string]middlewareBuilder{},
providerBuilders: map[string]providerBuilder{},
2020-04-20 16:36:34 +00:00
}
for pName, desc := range plugins {
manifest, err := client.ReadManifest(desc.ModuleName)
if err != nil {
_ = client.ResetAll()
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
}
logger := log.With().
Str("plugin", "plugin-"+pName).
Str("module", desc.ModuleName).
Str("runtime", manifest.Runtime).
Logger()
logCtx := logger.WithContext(ctx)
2022-11-21 17:36:05 +00:00
switch manifest.Type {
case typeMiddleware:
middleware, err := newMiddlewareBuilder(logCtx, client.GoPath(), manifest, desc.ModuleName, desc.Settings)
if err != nil {
return nil, err
}
pb.middlewareBuilders[pName] = middleware
case typeProvider:
pBuilder, err := newProviderBuilder(logCtx, manifest, client.GoPath())
if err != nil {
return nil, fmt.Errorf("%s: %w", desc.ModuleName, err)
}
pb.providerBuilders[pName] = pBuilder
default:
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
}
}
2020-04-20 16:36:34 +00:00
for pName, desc := range localPlugins {
manifest, err := ReadManifest(localGoPath, desc.ModuleName)
2020-04-20 16:36:34 +00:00
if err != nil {
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
2020-04-20 16:36:34 +00:00
}
logger := log.With().
Str("plugin", "plugin-"+pName).
Str("module", desc.ModuleName).
Str("runtime", manifest.Runtime).
Logger()
logCtx := logger.WithContext(ctx)
switch manifest.Type {
case typeMiddleware:
middleware, err := newMiddlewareBuilder(logCtx, localGoPath, manifest, desc.ModuleName, desc.Settings)
if err != nil {
return nil, err
}
pb.middlewareBuilders[pName] = middleware
case typeProvider:
builder, err := newProviderBuilder(logCtx, manifest, localGoPath)
if err != nil {
return nil, fmt.Errorf("%s: %w", desc.ModuleName, err)
}
pb.providerBuilders[pName] = builder
default:
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
2020-04-20 16:36:34 +00:00
}
}
return pb, nil
}
2020-04-20 16:36:34 +00:00
// Build builds a middleware plugin.
func (b Builder) Build(pName string, config map[string]interface{}, middlewareName string) (Constructor, error) {
if b.middlewareBuilders == nil {
return nil, fmt.Errorf("no plugin definitions in the static configuration: %s", pName)
}
// plugin (pName) can be located in yaegi or wasm middleware builders.
if descriptor, ok := b.middlewareBuilders[pName]; ok {
m, err := descriptor.newMiddleware(config, middlewareName)
2020-04-20 16:36:34 +00:00
if err != nil {
return nil, err
2020-04-20 16:36:34 +00:00
}
return m.NewHandler, nil
}
2022-11-21 17:36:05 +00:00
return nil, fmt.Errorf("unknown plugin type: %s", pName)
}
func newMiddlewareBuilder(ctx context.Context, goPath string, manifest *Manifest, moduleName string, settings Settings) (middlewareBuilder, error) {
switch manifest.Runtime {
case runtimeWasm:
wasmPath, err := getWasmPath(manifest)
if err != nil {
return nil, fmt.Errorf("wasm path: %w", err)
}
return newWasmMiddlewareBuilder(goPath, moduleName, wasmPath, settings)
case runtimeYaegi, "":
i, err := newInterpreter(ctx, goPath, manifest.Import)
if err != nil {
2024-04-18 13:14:04 +00:00
return nil, fmt.Errorf("failed to create Yaegi interpreter: %w", err)
}
2020-04-20 16:36:34 +00:00
return newYaegiMiddlewareBuilder(i, manifest.BasePkg, manifest.Import)
default:
return nil, fmt.Errorf("unknown plugin runtime: %s", manifest.Runtime)
}
}
func newProviderBuilder(ctx context.Context, manifest *Manifest, goPath string) (providerBuilder, error) {
switch manifest.Runtime {
case runtimeYaegi, "":
i, err := newInterpreter(ctx, goPath, manifest.Import)
2020-04-20 16:36:34 +00:00
if err != nil {
return providerBuilder{}, err
2020-04-20 16:36:34 +00:00
}
return providerBuilder{
interpreter: i,
Import: manifest.Import,
BasePkg: manifest.BasePkg,
}, nil
default:
return providerBuilder{}, fmt.Errorf("unknown plugin runtime: %s", manifest.Runtime)
2020-04-20 16:36:34 +00:00
}
}
2020-04-20 16:36:34 +00:00
func getWasmPath(manifest *Manifest) (string, error) {
wasmPath := manifest.WasmPath
if wasmPath == "" {
wasmPath = "plugin.wasm"
}
if !filepath.IsLocal(wasmPath) {
2024-02-08 13:15:45 +00:00
return "", errors.New("wasmPath must be a local path")
}
return wasmPath, nil
2020-04-20 16:36:34 +00:00
}