Local private plugins.
Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
parent
a243ac4dde
commit
5e3e47b484
11 changed files with 196 additions and 123 deletions
|
@ -6,12 +6,15 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/traefik/traefik/v2/pkg/log"
|
||||
)
|
||||
|
||||
// Setup setup plugins environment.
|
||||
func Setup(client *Client, plugins map[string]Descriptor, devPlugin *DevPlugin) error {
|
||||
err := checkPluginsConfiguration(plugins)
|
||||
const localGoPath = "./plugins-local/"
|
||||
|
||||
// SetupRemotePlugins setup remote plugins environment.
|
||||
func SetupRemotePlugins(client *Client, plugins map[string]Descriptor) error {
|
||||
err := checkRemotePluginsConfiguration(plugins)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid configuration: %w", err)
|
||||
}
|
||||
|
@ -53,65 +56,10 @@ func Setup(client *Client, plugins map[string]Descriptor, devPlugin *DevPlugin)
|
|||
}
|
||||
}
|
||||
|
||||
if devPlugin != nil {
|
||||
err := checkDevPluginConfiguration(devPlugin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid configuration: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkDevPluginConfiguration(plugin *DevPlugin) error {
|
||||
if plugin == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if plugin.GoPath == "" {
|
||||
return errors.New("missing Go Path (prefer a dedicated Go Path)")
|
||||
}
|
||||
|
||||
if plugin.ModuleName == "" {
|
||||
return errors.New("missing module name")
|
||||
}
|
||||
|
||||
m, err := ReadManifest(plugin.GoPath, plugin.ModuleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch m.Type {
|
||||
case "middleware", "provider":
|
||||
// noop
|
||||
default:
|
||||
return errors.New("unsupported type")
|
||||
}
|
||||
|
||||
if m.Import == "" {
|
||||
return errors.New("missing import")
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(m.Import, plugin.ModuleName) {
|
||||
return fmt.Errorf("the import %q must be related to the module name %q", m.Import, plugin.ModuleName)
|
||||
}
|
||||
|
||||
if m.DisplayName == "" {
|
||||
return errors.New("missing DisplayName")
|
||||
}
|
||||
|
||||
if m.Summary == "" {
|
||||
return errors.New("missing Summary")
|
||||
}
|
||||
|
||||
if m.TestData == nil {
|
||||
return errors.New("missing TestData")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkPluginsConfiguration(plugins map[string]Descriptor) error {
|
||||
func checkRemotePluginsConfiguration(plugins map[string]Descriptor) error {
|
||||
if plugins == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -147,3 +95,74 @@ func checkPluginsConfiguration(plugins map[string]Descriptor) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetupLocalPlugins setup local plugins environment.
|
||||
func SetupLocalPlugins(plugins map[string]LocalDescriptor) error {
|
||||
if plugins == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
uniq := make(map[string]struct{})
|
||||
|
||||
var errs *multierror.Error
|
||||
for pAlias, descriptor := range plugins {
|
||||
if descriptor.ModuleName == "" {
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: plugin name is missing", pAlias))
|
||||
}
|
||||
|
||||
if strings.HasPrefix(descriptor.ModuleName, "/") || strings.HasSuffix(descriptor.ModuleName, "/") {
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: plugin name should not start or end with a /", pAlias))
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := uniq[descriptor.ModuleName]; ok {
|
||||
errs = multierror.Append(errs, fmt.Errorf("only one version of a plugin is allowed, there is a duplicate of %s", descriptor.ModuleName))
|
||||
continue
|
||||
}
|
||||
|
||||
uniq[descriptor.ModuleName] = struct{}{}
|
||||
|
||||
err := checkLocalPluginManifest(descriptor)
|
||||
errs = multierror.Append(errs, err)
|
||||
}
|
||||
|
||||
return errs.ErrorOrNil()
|
||||
}
|
||||
|
||||
func checkLocalPluginManifest(descriptor LocalDescriptor) error {
|
||||
m, err := ReadManifest(localGoPath, descriptor.ModuleName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errs *multierror.Error
|
||||
|
||||
switch m.Type {
|
||||
case "middleware", "provider":
|
||||
// noop
|
||||
default:
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: unsupported type %q", descriptor.ModuleName, m.Type))
|
||||
}
|
||||
|
||||
if m.Import == "" {
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: missing import", descriptor.ModuleName))
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(m.Import, descriptor.ModuleName) {
|
||||
errs = multierror.Append(errs, fmt.Errorf("the import %q must be related to the module name %q", m.Import, descriptor.ModuleName))
|
||||
}
|
||||
|
||||
if m.DisplayName == "" {
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: missing DisplayName", descriptor.ModuleName))
|
||||
}
|
||||
|
||||
if m.Summary == "" {
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: missing Summary", descriptor.ModuleName))
|
||||
}
|
||||
|
||||
if m.TestData == nil {
|
||||
errs = multierror.Append(errs, fmt.Errorf("%s: missing TestData", descriptor.ModuleName))
|
||||
}
|
||||
|
||||
return errs.ErrorOrNil()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue