Make builtin the default pluginType

This commit is contained in:
Jeffrey Regan
2019-03-18 10:17:55 -07:00
parent 449b1b68e0
commit 3a7c8a03f4
3 changed files with 70 additions and 29 deletions

View File

@@ -26,12 +26,14 @@ import (
// Registry holds all the plugin factories. // Registry holds all the plugin factories.
type Registry struct { type Registry struct {
factories map[string]Factory factories map[types.PluginType]Factory
ldr ifc.Loader ldr ifc.Loader
} }
const ( const (
PluginsDir = "plugins" PluginsDir = "plugins"
pluginTypeGo = types.PluginType("go")
pluginTypeBuiltIn = types.PluginType("builtin")
) )
func DefaultPluginConfig() types.PluginConfig { func DefaultPluginConfig() types.PluginConfig {
@@ -48,9 +50,9 @@ func NewConfiguredRegistry(
ldr ifc.Loader, pc *types.PluginConfig) Registry { ldr ifc.Loader, pc *types.PluginConfig) Registry {
return Registry{ return Registry{
ldr: ldr, ldr: ldr,
factories: map[string]Factory{ factories: map[types.PluginType]Factory{
"go": newGoFactory(pc), pluginTypeGo: newGoFactory(pc),
"builtin": newBuiltinFactory(ldr), pluginTypeBuiltIn: newBuiltinFactory(ldr),
}, },
} }
} }
@@ -60,11 +62,15 @@ func NewRegistry(ldr ifc.Loader) Registry {
return NewConfiguredRegistry(ldr, &types.PluginConfig{}) return NewConfiguredRegistry(ldr, &types.PluginConfig{})
} }
// Load returns a plugin by type and name, // Load returns a plugin by type and name.
func (r *Registry) Load(pluginType, name string) (KVSource, error) { func (r *Registry) Load(
factory, exists := r.factories[pluginType] pt types.PluginType, name string) (KVSource, error) {
if pt.IsUndefined() {
pt = pluginTypeBuiltIn
}
factory, exists := r.factories[pt]
if !exists { if !exists {
return nil, fmt.Errorf("%s is not a valid plugin type", pluginType) return nil, fmt.Errorf("%s is not a valid plugin type", pt)
} }
return factory.load(name) return factory.load(name)
} }

View File

@@ -20,6 +20,27 @@ import (
"testing" "testing"
) )
const result = `
apiVersion: v1
data:
FRUIT: YXBwbGU=
VEGETABLE: Y2Fycm90
foo.env: Ck1PVU5UQUlOPWV2ZXJlc3QKT0NFQU49cGFjaWZpYwo=
passphrase: ZGF0IHBocmFzZQ==
kind: Secret
metadata:
name: bob-t98kdk9767
type: Opaque
`
func writeDataFiles(th *KustTestHarness) {
th.writeF("/app/foo.env", `
MOUNTAIN=everest
OCEAN=pacific
`)
th.writeF("/app/phrase.dat", "dat phrase")
}
func TestBuiltinPlugins(t *testing.T) { func TestBuiltinPlugins(t *testing.T) {
th := NewKustTestHarness(t, "/app") th := NewKustTestHarness(t, "/app")
th.writeK("/app", ` th.writeK("/app", `
@@ -37,25 +58,33 @@ secretGenerator:
- foo.env - foo.env
- passphrase=phrase.dat - passphrase=phrase.dat
`) `)
th.writeF("/app/foo.env", ` writeDataFiles(th)
MOUNTAIN=everest
OCEAN=pacific
`)
th.writeF("/app/phrase.dat", "dat phrase")
m, err := th.makeKustTarget().MakeCustomizedResMap() m, err := th.makeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
th.assertActualEqualsExpected(m, ` th.assertActualEqualsExpected(m, result)
apiVersion: v1 }
data:
FRUIT: YXBwbGU= func TestBuiltinIsTheDefault(t *testing.T) {
VEGETABLE: Y2Fycm90 th := NewKustTestHarness(t, "/app")
foo.env: Ck1PVU5UQUlOPWV2ZXJlc3QKT0NFQU49cGFjaWZpYwo= th.writeK("/app", `
passphrase: ZGF0IHBocmFzZQ== secretGenerator:
kind: Secret - name: bob
metadata: kvSources:
name: bob-t98kdk9767 - name: literals
type: Opaque args:
`) - FRUIT=apple
- VEGETABLE=carrot
- name: files
args:
- foo.env
- passphrase=phrase.dat
`)
writeDataFiles(th)
m, err := th.makeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.assertActualEqualsExpected(m, result)
} }

View File

@@ -271,9 +271,15 @@ type GeneratorOptions struct {
DisableNameSuffixHash bool `json:"disableNameSuffixHash,omitempty" yaml:"disableNameSuffixHash,omitempty"` DisableNameSuffixHash bool `json:"disableNameSuffixHash,omitempty" yaml:"disableNameSuffixHash,omitempty"`
} }
type PluginType string
func (p PluginType) IsUndefined() bool {
return p == PluginType("")
}
// KVSource represents a KV plugin backend. // KVSource represents a KV plugin backend.
type KVSource struct { type KVSource struct {
PluginType string `json:"pluginType,omitempty" yaml:"pluginType,omitempty"` PluginType PluginType `json:"pluginType,omitempty" yaml:"pluginType,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"` Name string `json:"name,omitempty" yaml:"name,omitempty"`
Args []string `json:"args,omitempty" yaml:"args,omitempty"` Args []string `json:"args,omitempty" yaml:"args,omitempty"`
} }