traefik/pkg/plugins/builder.go

133 lines
3.6 KiB
Go
Raw Normal View History

2020-04-20 16:36:34 +00:00
package plugins
import (
"context"
"fmt"
"net/http"
2021-11-09 13:30:09 +00:00
"os"
2020-04-20 16:36:34 +00:00
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v2/pkg/logs"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
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)
2020-04-20 16:36:34 +00:00
// Builder is a plugin builder.
type Builder struct {
middlewareBuilders map[string]*middlewareBuilder
providerBuilders map[string]providerBuilder
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) {
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)
}
2022-11-21 17:36:05 +00:00
logger := log.With().Str("plugin", "plugin-"+pName).Str("module", desc.ModuleName).Logger()
i := interp.New(interp.Options{
GoPath: client.GoPath(),
Env: os.Environ(),
2022-11-21 17:36:05 +00:00
Stdout: logs.NoLevel(logger, zerolog.DebugLevel),
Stderr: logs.NoLevel(logger, zerolog.ErrorLevel),
})
err = i.Use(stdlib.Symbols)
if err != nil {
return nil, fmt.Errorf("%s: failed to load symbols: %w", desc.ModuleName, err)
}
err = i.Use(ppSymbols())
if err != nil {
return nil, fmt.Errorf("%s: failed to load provider symbols: %w", desc.ModuleName, err)
}
2020-04-20 16:36:34 +00:00
_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifest.Import))
if err != nil {
return nil, fmt.Errorf("%s: failed to import plugin code %q: %w", desc.ModuleName, manifest.Import, err)
}
switch manifest.Type {
case "middleware":
middleware, err := newMiddlewareBuilder(i, manifest.BasePkg, manifest.Import)
if err != nil {
return nil, err
}
pb.middlewareBuilders[pName] = middleware
case "provider":
pb.providerBuilders[pName] = providerBuilder{
interpreter: i,
Import: manifest.Import,
BasePkg: manifest.BasePkg,
}
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
}
2022-11-21 17:36:05 +00:00
logger := log.With().Str("plugin", "plugin-"+pName).Str("module", desc.ModuleName).Logger()
i := interp.New(interp.Options{
GoPath: localGoPath,
Env: os.Environ(),
2022-11-21 17:36:05 +00:00
Stdout: logs.NoLevel(logger, zerolog.DebugLevel),
Stderr: logs.NoLevel(logger, zerolog.ErrorLevel),
})
err = i.Use(stdlib.Symbols)
if err != nil {
return nil, fmt.Errorf("%s: failed to load symbols: %w", desc.ModuleName, err)
}
err = i.Use(ppSymbols())
if err != nil {
return nil, fmt.Errorf("%s: failed to load provider symbols: %w", desc.ModuleName, err)
}
2020-04-20 16:36:34 +00:00
_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifest.Import))
if err != nil {
return nil, fmt.Errorf("%s: failed to import plugin code %q: %w", desc.ModuleName, manifest.Import, err)
2020-04-20 16:36:34 +00:00
}
switch manifest.Type {
case "middleware":
middleware, err := newMiddlewareBuilder(i, manifest.BasePkg, manifest.Import)
if err != nil {
return nil, err
}
pb.middlewareBuilders[pName] = middleware
case "provider":
pb.providerBuilders[pName] = providerBuilder{
interpreter: i,
Import: manifest.Import,
BasePkg: manifest.BasePkg,
}
default:
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
2020-04-20 16:36:34 +00:00
}
}
return pb, nil
}