2020-04-20 16:36:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-25 13:50:09 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/static"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/plugins"
|
2020-04-20 16:36:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const outputDir = "./plugins-storage/"
|
|
|
|
|
2020-12-01 09:04:04 +00:00
|
|
|
func createPluginBuilder(staticConfiguration *static.Configuration) (*plugins.Builder, error) {
|
2021-06-25 13:50:09 +00:00
|
|
|
client, plgs, localPlgs, err := initPlugins(staticConfiguration)
|
2020-12-01 09:04:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
return plugins.NewBuilder(client, plgs, localPlgs)
|
2020-12-01 09:04:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
func initPlugins(staticCfg *static.Configuration) (*plugins.Client, map[string]plugins.Descriptor, map[string]plugins.LocalDescriptor, error) {
|
|
|
|
err := checkUniquePluginNames(staticCfg.Experimental)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
var client *plugins.Client
|
|
|
|
plgs := map[string]plugins.Descriptor{}
|
|
|
|
|
2022-03-24 07:54:07 +00:00
|
|
|
if hasPlugins(staticCfg) {
|
2021-06-25 13:50:09 +00:00
|
|
|
opts := plugins.ClientOptions{
|
|
|
|
Output: outputDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
client, err = plugins.NewClient(opts)
|
|
|
|
if err != nil {
|
2023-06-02 09:34:06 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unable to create plugins client: %w", err)
|
2021-06-25 13:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = plugins.SetupRemotePlugins(client, staticCfg.Experimental.Plugins)
|
|
|
|
if err != nil {
|
2023-06-02 09:34:06 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unable to set up plugins environment: %w", err)
|
2021-06-25 13:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
plgs = staticCfg.Experimental.Plugins
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
localPlgs := map[string]plugins.LocalDescriptor{}
|
|
|
|
|
|
|
|
if hasLocalPlugins(staticCfg) {
|
|
|
|
err := plugins.SetupLocalPlugins(staticCfg.Experimental.LocalPlugins)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
localPlgs = staticCfg.Experimental.LocalPlugins
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
return client, plgs, localPlgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkUniquePluginNames(e *static.Experimental) error {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
for s := range e.LocalPlugins {
|
|
|
|
if _, ok := e.Plugins[s]; ok {
|
|
|
|
return fmt.Errorf("the plugin's name %q must be unique", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hasPlugins(staticCfg *static.Configuration) bool {
|
2021-06-25 13:50:09 +00:00
|
|
|
return staticCfg.Experimental != nil && len(staticCfg.Experimental.Plugins) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasLocalPlugins(staticCfg *static.Configuration) bool {
|
|
|
|
return staticCfg.Experimental != nil && len(staticCfg.Experimental.LocalPlugins) > 0
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|