2021-05-11 14:14:10 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-05-11 14:14:10 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/logs"
|
2021-05-11 14:14:10 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/provider"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/safe"
|
2022-01-14 11:22:06 +00:00
|
|
|
"github.com/traefik/yaegi/interp"
|
2021-05-11 14:14:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PP the interface of a plugin's provider.
|
|
|
|
type PP interface {
|
|
|
|
Init() error
|
|
|
|
Provide(cfgChan chan<- json.Marshaler) error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type _PP struct {
|
2021-07-20 09:58:06 +00:00
|
|
|
IValue interface{}
|
2021-05-11 14:14:10 +00:00
|
|
|
WInit func() error
|
|
|
|
WProvide func(cfgChan chan<- json.Marshaler) error
|
|
|
|
WStop func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p _PP) Init() error {
|
|
|
|
return p.WInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p _PP) Provide(cfgChan chan<- json.Marshaler) error {
|
|
|
|
return p.WProvide(cfgChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p _PP) Stop() error {
|
|
|
|
return p.WStop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ppSymbols() map[string]map[string]reflect.Value {
|
|
|
|
return map[string]map[string]reflect.Value{
|
2021-07-19 16:54:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/plugins/plugins": {
|
2021-05-11 14:14:10 +00:00
|
|
|
"PP": reflect.ValueOf((*PP)(nil)),
|
|
|
|
"_PP": reflect.ValueOf((*_PP)(nil)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildProvider builds a plugin's provider.
|
|
|
|
func (b Builder) BuildProvider(pName string, config map[string]interface{}) (provider.Provider, error) {
|
2022-01-14 11:22:06 +00:00
|
|
|
if b.providerBuilders == nil {
|
2021-05-11 14:14:10 +00:00
|
|
|
return nil, fmt.Errorf("no plugin definition in the static configuration: %s", pName)
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
builder, ok := b.providerBuilders[pName]
|
2021-05-11 14:14:10 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown plugin type: %s", pName)
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
return newProvider(builder, config, "plugin-"+pName)
|
|
|
|
}
|
|
|
|
|
|
|
|
type providerBuilder struct {
|
|
|
|
// Import plugin's import/package
|
|
|
|
Import string `json:"import,omitempty" toml:"import,omitempty" yaml:"import,omitempty"`
|
|
|
|
|
|
|
|
// BasePkg plugin's base package name (optional)
|
|
|
|
BasePkg string `json:"basePkg,omitempty" toml:"basePkg,omitempty" yaml:"basePkg,omitempty"`
|
|
|
|
|
|
|
|
interpreter *interp.Interpreter
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Provider is a plugin's provider wrapper.
|
|
|
|
type Provider struct {
|
|
|
|
name string
|
|
|
|
pp PP
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
func newProvider(builder providerBuilder, config map[string]interface{}, providerName string) (*Provider, error) {
|
|
|
|
basePkg := builder.BasePkg
|
2021-05-11 14:14:10 +00:00
|
|
|
if basePkg == "" {
|
2022-01-14 11:22:06 +00:00
|
|
|
basePkg = strings.ReplaceAll(path.Base(builder.Import), "-", "_")
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
vConfig, err := builder.interpreter.Eval(basePkg + `.CreateConfig()`)
|
2021-05-11 14:14:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to eval CreateConfig: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &mapstructure.DecoderConfig{
|
2022-08-01 13:12:08 +00:00
|
|
|
DecodeHook: mapstructure.StringToSliceHookFunc(","),
|
2021-05-11 14:14:10 +00:00
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: vConfig.Interface(),
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create configuration decoder: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = decoder.Decode(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
_, err = builder.interpreter.Eval(`package wrapper
|
2021-05-11 14:14:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
` + basePkg + ` "` + builder.Import + `"
|
2021-05-11 14:14:10 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewWrapper(ctx context.Context, config *` + basePkg + `.Config, name string) (plugins.PP, error) {
|
|
|
|
p, err := ` + basePkg + `.New(ctx, config, name)
|
|
|
|
var pv plugins.PP = p
|
|
|
|
return pv, err
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to eval wrapper: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:22:06 +00:00
|
|
|
fnNew, err := builder.interpreter.Eval("wrapper.NewWrapper")
|
2021-05-11 14:14:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to eval New: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
args := []reflect.Value{reflect.ValueOf(ctx), vConfig, reflect.ValueOf(providerName)}
|
|
|
|
results := fnNew.Call(args)
|
|
|
|
|
|
|
|
if len(results) > 1 && results[1].Interface() != nil {
|
|
|
|
return nil, results[1].Interface().(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
prov, ok := results[0].Interface().(PP)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid provider type: %T", results[0].Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Provider{name: providerName, pp: prov}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init wraps the Init method of a plugin.
|
|
|
|
func (p *Provider) Init() error {
|
|
|
|
return p.pp.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide wraps the Provide method of a plugin.
|
|
|
|
func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Str(logs.ProviderName, p.name).Msgf("Panic inside the plugin %v", err)
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cfgChan := make(chan json.Marshaler)
|
|
|
|
|
|
|
|
pool.GoCtx(func(ctx context.Context) {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str(logs.ProviderName, p.name).Logger()
|
2021-05-11 14:14:10 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := p.pp.Stop()
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to stop the provider")
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
case cfgPg := <-cfgChan:
|
|
|
|
marshalJSON, err := cfgPg.MarshalJSON()
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to marshal configuration")
|
2021-05-11 14:14:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &dynamic.Configuration{}
|
|
|
|
err = json.Unmarshal(marshalJSON, cfg)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Failed to unmarshal configuration")
|
2021-05-11 14:14:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
configurationChan <- dynamic.Message{
|
|
|
|
ProviderName: p.name,
|
|
|
|
Configuration: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-12-08 16:08:05 +00:00
|
|
|
err := p.pp.Provide(cfgChan)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from %s: %w", p.name, err)
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:14:10 +00:00
|
|
|
return nil
|
|
|
|
}
|