mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-01 18:30:15 +00:00
Put goplugins behind flag.
This commit is contained in:
@@ -18,40 +18,66 @@ package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
)
|
||||
|
||||
var _ Factory = &goFactory{}
|
||||
|
||||
const (
|
||||
pluginDir = "kustomize/plugins/kvsource"
|
||||
kvSourcesDir = "kvSources"
|
||||
EnableGoPluginsFlagName = "enable_alpha_goplugins_accept_panic_risk"
|
||||
EnableGoPluginsFlagHelp = `
|
||||
Warning: the main program may panic and exit on an
|
||||
attempt to use a goplugin that was compiled under
|
||||
conditions differing from the those in effect when
|
||||
main was compiled. It's safest to use this flag in
|
||||
the context of a container image holding both the
|
||||
main and the goplugins it needs, all built on the
|
||||
same machine, with the same transitive libs and
|
||||
the same compiler version.
|
||||
`
|
||||
errorFmt = `
|
||||
enable go plugins by specifying flag
|
||||
--%s
|
||||
Place .so files in
|
||||
%s
|
||||
%s
|
||||
`
|
||||
)
|
||||
|
||||
func newGoFactory() *goFactory {
|
||||
func newGoFactory(c *types.PluginConfig) *goFactory {
|
||||
return &goFactory{
|
||||
config: c,
|
||||
plugins: make(map[string]KVSource),
|
||||
}
|
||||
}
|
||||
|
||||
type goFactory struct {
|
||||
config *types.PluginConfig
|
||||
plugins map[string]KVSource
|
||||
}
|
||||
|
||||
func configDir() string {
|
||||
xdghome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if len(xdghome) == 0 {
|
||||
return os.ExpandEnv("$HOME/.config")
|
||||
}
|
||||
return xdghome
|
||||
}
|
||||
|
||||
func (p *goFactory) load(name string) (KVSource, error) {
|
||||
if plug, ok := p.plugins[name]; ok {
|
||||
return plug, nil
|
||||
}
|
||||
|
||||
goPlugin, err := plugin.Open(fmt.Sprintf("%s/%s/kustomize-%s.so", configDir(), pluginDir, name))
|
||||
dir := filepath.Join(
|
||||
p.config.DirectoryPath,
|
||||
kvSourcesDir)
|
||||
if !p.config.GoEnabled {
|
||||
return nil, fmt.Errorf(
|
||||
errorFmt,
|
||||
EnableGoPluginsFlagName,
|
||||
dir,
|
||||
EnableGoPluginsFlagHelp)
|
||||
}
|
||||
|
||||
goPlugin, err := plugin.Open(
|
||||
filepath.Join(dir, name+".so"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -18,8 +18,10 @@ package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"path/filepath"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
)
|
||||
|
||||
// Registry holds all the plugin factories.
|
||||
@@ -28,17 +30,36 @@ type Registry struct {
|
||||
ldr ifc.Loader
|
||||
}
|
||||
|
||||
// NewRegistry returns a new Registry loaded with all the factories.
|
||||
func NewRegistry(ldr ifc.Loader) Registry {
|
||||
const (
|
||||
PluginsDir = "plugins"
|
||||
)
|
||||
|
||||
func DefaultPluginConfig() types.PluginConfig {
|
||||
return types.PluginConfig{
|
||||
GoEnabled: false,
|
||||
DirectoryPath: filepath.Join(
|
||||
pgmconfig.ConfigRoot(), PluginsDir),
|
||||
}
|
||||
}
|
||||
|
||||
// NewConfiguredRegistry returns a new Registry loaded
|
||||
// with all the factories and a custom PluginConfig.
|
||||
func NewConfiguredRegistry(
|
||||
ldr ifc.Loader, pc *types.PluginConfig) Registry {
|
||||
return Registry{
|
||||
ldr: ldr,
|
||||
factories: map[string]Factory{
|
||||
"go": newGoFactory(),
|
||||
"go": newGoFactory(pc),
|
||||
"testonly": newTestonlyFactory(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewRegistry returns a new Registry with default config.
|
||||
func NewRegistry(ldr ifc.Loader) Registry {
|
||||
return NewConfiguredRegistry(ldr, &types.PluginConfig{})
|
||||
}
|
||||
|
||||
// Load returns a plugin by type and name,
|
||||
func (r *Registry) Load(pluginType, name string) (KVSource, error) {
|
||||
factory, exists := r.factories[pluginType]
|
||||
|
||||
Reference in New Issue
Block a user