mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 01:46:23 +00:00
Remove some duped code.
This commit is contained in:
@@ -18,81 +18,40 @@ package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
"sigs.k8s.io/kustomize/pkg/resid"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/resource"
|
||||
)
|
||||
|
||||
const generatorSymbol = "Generator"
|
||||
|
||||
type Generatable interface {
|
||||
Generate() (resmap.ResMap, error)
|
||||
}
|
||||
|
||||
type generatorLoader struct {
|
||||
pluginDir string
|
||||
enabled bool
|
||||
rf *resmap.Factory
|
||||
pc *types.PluginConfig
|
||||
}
|
||||
|
||||
func NewGeneratorLoader(b bool, f *resmap.Factory) generatorLoader {
|
||||
return generatorLoader{
|
||||
pluginDir: filepath.Join(pgmconfig.ConfigRoot(), pgmconfig.PluginsDir),
|
||||
enabled: b,
|
||||
rf: f,
|
||||
}
|
||||
func NewGeneratorLoader(pc *types.PluginConfig) generatorLoader {
|
||||
return generatorLoader{pc: pc}
|
||||
}
|
||||
|
||||
func (l generatorLoader) Load(rm resmap.ResMap) (resmap.ResMap, error) {
|
||||
func (l generatorLoader) Load(
|
||||
rm resmap.ResMap) ([]transformers.Generator, error) {
|
||||
if len(rm) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if !l.enabled {
|
||||
return nil, fmt.Errorf("plugin is not enabled")
|
||||
if !l.pc.GoEnabled {
|
||||
return nil, fmt.Errorf("plugins not enabled")
|
||||
}
|
||||
var result resmap.ResMap
|
||||
var result []transformers.Generator
|
||||
for id, res := range rm {
|
||||
r, err := l.load(id, res)
|
||||
fileName := pluginFileName(l.pc, id)
|
||||
c, err := loadAndConfigurePlugin(fileName, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err = resmap.MergeWithErrorOnIdCollision(result, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
g, ok := c.(transformers.Generator)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not a generator", fileName)
|
||||
}
|
||||
result = append(result, g)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l generatorLoader) load(id resid.ResId, res *resource.Resource) (resmap.ResMap, error) {
|
||||
fileName := filepath.Join(l.pluginDir, id.Gvk().Kind+".so")
|
||||
goPlugin, err := plugin.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plugin %s file not opened", fileName)
|
||||
}
|
||||
|
||||
symbol, err := goPlugin.Lookup(generatorSymbol)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plugin %s fails lookup", fileName)
|
||||
}
|
||||
|
||||
c, ok := symbol.(Configurable)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not configurable", fileName)
|
||||
}
|
||||
err = c.Config(res)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "plugin %s fails configuration", fileName)
|
||||
}
|
||||
|
||||
g, ok := c.(Generatable)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not a transformer", fileName)
|
||||
}
|
||||
return g.Generate()
|
||||
}
|
||||
|
||||
@@ -43,7 +43,8 @@ func NewTransformerLoader(pc *types.PluginConfig) transformerLoader {
|
||||
return transformerLoader{pc: pc}
|
||||
}
|
||||
|
||||
func (l transformerLoader) Load(rm resmap.ResMap) ([]transformers.Transformer, error) {
|
||||
func (l transformerLoader) Load(
|
||||
rm resmap.ResMap) ([]transformers.Transformer, error) {
|
||||
if len(rm) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -52,30 +53,37 @@ func (l transformerLoader) Load(rm resmap.ResMap) ([]transformers.Transformer, e
|
||||
}
|
||||
var result []transformers.Transformer
|
||||
for id, res := range rm {
|
||||
t, err := l.load(id, res)
|
||||
fileName := pluginFileName(l.pc, id)
|
||||
c, err := loadAndConfigurePlugin(fileName, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, ok := c.(transformers.Transformer)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not a transformer", fileName)
|
||||
}
|
||||
result = append(result, t)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l transformerLoader) load(
|
||||
id resid.ResId, res *resource.Resource) (transformers.Transformer, error) {
|
||||
fileName := filepath.Join(
|
||||
l.pc.DirectoryPath,
|
||||
func pluginFileName(pc *types.PluginConfig, id resid.ResId) string {
|
||||
return filepath.Join(
|
||||
pc.DirectoryPath,
|
||||
id.Gvk().Group, id.Gvk().Version, id.Gvk().Kind+".so")
|
||||
}
|
||||
|
||||
func loadAndConfigurePlugin(
|
||||
fileName string, res *resource.Resource) (Configurable, error) {
|
||||
goPlugin, err := plugin.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plugin %s file not opened", fileName)
|
||||
}
|
||||
|
||||
symbol, err := goPlugin.Lookup(kplugin.TransformerSymbol)
|
||||
symbol, err := goPlugin.Lookup(kplugin.PluginSymbol)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plugin %s fails lookup", fileName)
|
||||
return nil, fmt.Errorf(
|
||||
"plugin %s doesn't have symbol %s", fileName, kplugin.PluginSymbol)
|
||||
}
|
||||
|
||||
c, ok := symbol.(Configurable)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not configurable", fileName)
|
||||
@@ -84,10 +92,5 @@ func (l transformerLoader) load(
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "plugin %s fails configuration", fileName)
|
||||
}
|
||||
|
||||
t, ok := c.(transformers.Transformer)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not a transformer", fileName)
|
||||
}
|
||||
return t, nil
|
||||
return c, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user