2021-05-11 16:14:10 +02:00
package plugins
import (
"context"
"encoding/json"
"fmt"
"path"
"reflect"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/provider"
"github.com/traefik/traefik/v2/pkg/safe"
2022-01-14 12:22:06 +01:00
"github.com/traefik/yaegi/interp"
2021-05-11 16:14:10 +02: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 11:58:06 +02:00
IValue interface { }
2021-05-11 16:14:10 +02: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 18:54:04 +02:00
"github.com/traefik/traefik/v2/pkg/plugins/plugins" : {
2021-05-11 16:14:10 +02: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 12:22:06 +01:00
if b . providerBuilders == nil {
2021-05-11 16:14:10 +02:00
return nil , fmt . Errorf ( "no plugin definition in the static configuration: %s" , pName )
}
2022-01-14 12:22:06 +01:00
builder , ok := b . providerBuilders [ pName ]
2021-05-11 16:14:10 +02:00
if ! ok {
return nil , fmt . Errorf ( "unknown plugin type: %s" , pName )
}
2022-01-14 12:22:06 +01: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 16:14:10 +02:00
}
// Provider is a plugin's provider wrapper.
type Provider struct {
name string
pp PP
}
2022-01-14 12:22:06 +01:00
func newProvider ( builder providerBuilder , config map [ string ] interface { } , providerName string ) ( * Provider , error ) {
basePkg := builder . BasePkg
2021-05-11 16:14:10 +02:00
if basePkg == "" {
2022-01-14 12:22:06 +01:00
basePkg = strings . ReplaceAll ( path . Base ( builder . Import ) , "-" , "_" )
2021-05-11 16:14:10 +02:00
}
2022-01-14 12:22:06 +01:00
vConfig , err := builder . interpreter . Eval ( basePkg + ` .CreateConfig() ` )
2021-05-11 16:14:10 +02:00
if err != nil {
return nil , fmt . Errorf ( "failed to eval CreateConfig: %w" , err )
}
cfg := & mapstructure . DecoderConfig {
2022-08-01 15:12:08 +02:00
DecodeHook : mapstructure . StringToSliceHookFunc ( "," ) ,
2021-05-11 16:14:10 +02: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 12:22:06 +01:00
_ , err = builder . interpreter . Eval ( ` package wrapper
2021-05-11 16:14:10 +02:00
import (
"context"
2022-01-14 12:22:06 +01:00
` + basePkg + ` "` + builder.Import + `"
2021-05-11 16:14:10 +02: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 12:22:06 +01:00
fnNew , err := builder . interpreter . Eval ( "wrapper.NewWrapper" )
2021-05-11 16:14:10 +02: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 {
log . WithoutContext ( ) . WithField ( log . ProviderName , p . name ) . Errorf ( "panic inside the plugin %v" , err )
}
} ( )
cfgChan := make ( chan json . Marshaler )
pool . GoCtx ( func ( ctx context . Context ) {
logger := log . FromContext ( log . With ( ctx , log . Str ( log . ProviderName , p . name ) ) )
for {
select {
case <- ctx . Done ( ) :
err := p . pp . Stop ( )
if err != nil {
logger . Errorf ( "failed to stop the provider: %v" , err )
}
return
case cfgPg := <- cfgChan :
marshalJSON , err := cfgPg . MarshalJSON ( )
if err != nil {
logger . Errorf ( "failed to marshal configuration: %v" , err )
continue
}
cfg := & dynamic . Configuration { }
err = json . Unmarshal ( marshalJSON , cfg )
if err != nil {
logger . Errorf ( "failed to unmarshal configuration: %v" , err )
continue
}
configurationChan <- dynamic . Message {
ProviderName : p . name ,
Configuration : cfg ,
}
}
}
} )
2021-12-08 17:08:05 +01:00
err := p . pp . Provide ( cfgChan )
if err != nil {
return fmt . Errorf ( "error from %s: %w" , p . name , err )
}
2021-05-11 16:14:10 +02:00
return nil
}