Merge pull request #1239 from sethpollack/fix_plugins

allow reuse of plugins
This commit is contained in:
Jeff Regan
2019-06-25 09:10:05 -07:00
committed by GitHub
2 changed files with 34 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"plugin" "plugin"
"reflect"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -102,18 +103,13 @@ func (l *Loader) absolutePluginPath(id resid.ResId) string {
// TODO: https://github.com/kubernetes-sigs/kustomize/issues/1164 // TODO: https://github.com/kubernetes-sigs/kustomize/issues/1164
func (l *Loader) loadAndConfigurePlugin( func (l *Loader) loadAndConfigurePlugin(
ldr ifc.Loader, res *resource.Resource) (c Configurable, err error) { ldr ifc.Loader, res *resource.Resource) (Configurable, error) {
if !l.pc.Enabled { if !l.pc.Enabled {
return nil, NotEnabledErr(res.OrgId().Kind) return nil, NotEnabledErr(res.OrgId().Kind)
} }
if p := NewExecPlugin( c, err := l.loadPlugin(res.OrgId())
l.absolutePluginPath(res.OrgId())); p.isAvailable() { if err != nil {
c = p return nil, err
} else {
c, err = l.loadGoPlugin(res.OrgId())
if err != nil {
return nil, err
}
} }
yaml, err := res.AsYAML() yaml, err := res.AsYAML()
if err != nil { if err != nil {
@@ -127,6 +123,18 @@ func (l *Loader) loadAndConfigurePlugin(
return c, nil return c, nil
} }
func (l *Loader) loadPlugin(resId resid.ResId) (Configurable, error) {
p := NewExecPlugin(l.absolutePluginPath(resId))
if p.isAvailable() {
return p, nil
}
c, err := l.loadGoPlugin(resId)
if err != nil {
return nil, err
}
return c, nil
}
// registry is a means to avoid trying to load the same .so file // registry is a means to avoid trying to load the same .so file
// into memory more than once, which results in an error. // into memory more than once, which results in an error.
// Each test makes its own loader, and tries to load its own plugins, // Each test makes its own loader, and tries to load its own plugins,
@@ -135,11 +143,10 @@ func (l *Loader) loadAndConfigurePlugin(
// as a Loader instance variable. So make it a package variable. // as a Loader instance variable. So make it a package variable.
var registry = make(map[string]Configurable) var registry = make(map[string]Configurable)
func (l *Loader) loadGoPlugin(id resid.ResId) (c Configurable, err error) { func (l *Loader) loadGoPlugin(id resid.ResId) (Configurable, error) {
regId := relativePluginPath(id) regId := relativePluginPath(id)
var ok bool if c, ok := registry[regId]; ok {
if c, ok = registry[regId]; ok { return copy(c), nil
return c, nil
} }
absPath := l.absolutePluginPath(id) absPath := l.absolutePluginPath(id)
p, err := plugin.Open(absPath + ".so") p, err := plugin.Open(absPath + ".so")
@@ -152,10 +159,18 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (c Configurable, err error) {
err, "plugin %s doesn't have symbol %s", err, "plugin %s doesn't have symbol %s",
regId, PluginSymbol) regId, PluginSymbol)
} }
c, ok = symbol.(Configurable) c, ok := symbol.(Configurable)
if !ok { if !ok {
return nil, fmt.Errorf("plugin %s not configurable", regId) return nil, fmt.Errorf("plugin %s not configurable", regId)
} }
registry[regId] = c registry[regId] = c
return return copy(c), nil
}
func copy(i interface{}) Configurable {
indirect := reflect.Indirect(reflect.ValueOf(i))
newIndirect := reflect.New(indirect.Type())
newIndirect.Elem().Set(reflect.ValueOf(indirect.Interface()))
newNamed := newIndirect.Interface()
return newNamed.(Configurable)
} }

View File

@@ -4,11 +4,12 @@
package target_test package target_test
import ( import (
"sigs.k8s.io/kustomize/v3/pkg/plugins"
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/v3/pkg/kusttest" "sigs.k8s.io/kustomize/v3/pkg/plugins"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
) )
func writeDeployment(th *kusttest_test.KustTestHarness, path string) { func writeDeployment(th *kusttest_test.KustTestHarness, path string) {
@@ -78,13 +79,11 @@ transformers:
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
// TODO: Fix #1164; the value of the name: field below
// should be: 2018-05-11-peach-2018-05-11-apple-myDeployment
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: 2018-05-11-apple-2018-05-11-apple-myDeployment name: 2018-05-11-apple-2018-05-11-peach-myDeployment
spec: spec:
template: template:
metadata: metadata: