Add goplugin KV generator example.

This commit is contained in:
Jeffrey Regan
2019-03-26 17:49:11 -07:00
parent 99406d4412
commit 99391157ec
11 changed files with 319 additions and 38 deletions

View File

@@ -91,7 +91,7 @@ func keyValuesFromLiteralSources(sources []string) ([]kv.Pair, error) {
}
func (bf baseFactory) keyValuesFromPlugins(sources []types.KVSource) ([]kv.Pair, error) {
var allKvs []kv.Pair
var result []kv.Pair
for _, s := range sources {
plug, err := bf.reg.Load(s.PluginType, s.Name)
if err != nil {
@@ -101,9 +101,11 @@ func (bf baseFactory) keyValuesFromPlugins(sources []types.KVSource) ([]kv.Pair,
if err != nil {
return nil, err
}
allKvs = append(allKvs, kvs...)
for k, v := range kvs {
result = append(result, kv.Pair{Key: k, Value: v})
}
}
return allKvs, nil
return result, nil
}
func (bf baseFactory) keyValuesFromFileSources(sources []string) ([]kv.Pair, error) {

View File

@@ -21,16 +21,16 @@ import (
"sigs.k8s.io/kustomize/pkg/ifc"
)
// Envfiles format should be a path to a file to read lines of key=val
// EnvFiles format should be a path to a file to read lines of key=val
// pairs to create a configmap.
// i.e. a Docker .env file or a .ini file.
type Envfiles struct {
type EnvFiles struct {
Ldr ifc.Loader
}
// Get implements the interface for kv plugins.
func (p Envfiles) Get(root string, args []string) ([]kv.Pair, error) {
var all []kv.Pair
func (p EnvFiles) Get(root string, args []string) (map[string]string, error) {
all := make(map[string]string)
for _, path := range args {
if path == "" {
return nil, nil
@@ -43,7 +43,9 @@ func (p Envfiles) Get(root string, args []string) ([]kv.Pair, error) {
if err != nil {
return nil, err
}
all = append(all, kvs...)
for _, pair := range kvs {
all[pair.Key] = pair.Value
}
}
return all, nil
}

View File

@@ -32,8 +32,8 @@ type Files struct {
}
// Get implements the interface for kv plugins.
func (p Files) Get(root string, args []string) ([]kv.Pair, error) {
var kvs []kv.Pair
func (p Files) Get(root string, args []string) (map[string]string, error) {
kvs := make(map[string]string)
for _, s := range args {
k, fPath, err := kv.ParseFileSource(s)
if err != nil {
@@ -43,7 +43,7 @@ func (p Files) Get(root string, args []string) ([]kv.Pair, error) {
if err != nil {
return nil, err
}
kvs = append(kvs, kv.Pair{Key: k, Value: string(content)})
kvs[k] = string(content)
}
return kvs, nil
}

View File

@@ -26,14 +26,14 @@ import (
type Literals struct{}
// Get implements the interface for kv plugins.
func (p Literals) Get(root string, args []string) ([]kv.Pair, error) {
var kvs []kv.Pair
func (p Literals) Get(root string, args []string) (map[string]string, error) {
kvs := make(map[string]string)
for _, s := range args {
k, v, err := kv.ParseLiteralSource(s)
if err != nil {
return nil, err
}
kvs = append(kvs, kv.Pair{Key: k, Value: v})
kvs[k] = v
}
return kvs, nil
}

View File

@@ -34,7 +34,7 @@ func newBuiltinFactory(ldr ifc.Loader) *builtinFactory {
plugins: map[string]KVSource{
"literals": builtin.Literals{},
"files": builtin.Files{Ldr: ldr},
"envfiles": builtin.Envfiles{Ldr: ldr},
"envfiles": builtin.EnvFiles{Ldr: ldr},
},
}
}

View File

@@ -29,23 +29,20 @@ var _ Factory = &goFactory{}
const (
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.
`
EnableGoPluginsFlagHelp = `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
`
%s`
)
func newGoFactory(c *types.PluginConfig) *goFactory {
@@ -82,7 +79,7 @@ func (p *goFactory) load(name string) (KVSource, error) {
return nil, err
}
symbol, err := goPlugin.Lookup("Plugin")
symbol, err := goPlugin.Lookup("KVSource")
if err != nil {
return nil, err
}

View File

@@ -17,13 +17,9 @@ limitations under the License.
// Package plugin provides a plugin abstraction layer.
package plugin
import (
"sigs.k8s.io/kustomize/k8sdeps/kv"
)
// KVSource is the interface for kv source plugins.
type KVSource interface {
Get(root string, args []string) ([]kv.Pair, error)
Get(root string, args []string) (map[string]string, error)
}
// Factory is the interface for new kv source plugin implementations.