From 3a7c8a03f4f988c183cf2d6775cf9d3be0381d09 Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Mon, 18 Mar 2019 10:17:55 -0700 Subject: [PATCH] Make builtin the default pluginType --- k8sdeps/kv/plugin/registry.go | 24 +++++++----- pkg/target/builtinplugins_test.go | 63 ++++++++++++++++++++++--------- pkg/types/kustomization.go | 12 ++++-- 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/k8sdeps/kv/plugin/registry.go b/k8sdeps/kv/plugin/registry.go index 183ed16e3..f505a3766 100644 --- a/k8sdeps/kv/plugin/registry.go +++ b/k8sdeps/kv/plugin/registry.go @@ -26,12 +26,14 @@ import ( // Registry holds all the plugin factories. type Registry struct { - factories map[string]Factory + factories map[types.PluginType]Factory ldr ifc.Loader } const ( - PluginsDir = "plugins" + PluginsDir = "plugins" + pluginTypeGo = types.PluginType("go") + pluginTypeBuiltIn = types.PluginType("builtin") ) func DefaultPluginConfig() types.PluginConfig { @@ -48,9 +50,9 @@ func NewConfiguredRegistry( ldr ifc.Loader, pc *types.PluginConfig) Registry { return Registry{ ldr: ldr, - factories: map[string]Factory{ - "go": newGoFactory(pc), - "builtin": newBuiltinFactory(ldr), + factories: map[types.PluginType]Factory{ + pluginTypeGo: newGoFactory(pc), + pluginTypeBuiltIn: newBuiltinFactory(ldr), }, } } @@ -60,11 +62,15 @@ 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] +// Load returns a plugin by type and name. +func (r *Registry) Load( + pt types.PluginType, name string) (KVSource, error) { + if pt.IsUndefined() { + pt = pluginTypeBuiltIn + } + factory, exists := r.factories[pt] 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) } diff --git a/pkg/target/builtinplugins_test.go b/pkg/target/builtinplugins_test.go index 62e6d432e..07bf3bfa9 100644 --- a/pkg/target/builtinplugins_test.go +++ b/pkg/target/builtinplugins_test.go @@ -20,6 +20,27 @@ import ( "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) { th := NewKustTestHarness(t, "/app") th.writeK("/app", ` @@ -37,25 +58,33 @@ secretGenerator: - foo.env - passphrase=phrase.dat `) - th.writeF("/app/foo.env", ` -MOUNTAIN=everest -OCEAN=pacific -`) - th.writeF("/app/phrase.dat", "dat phrase") + writeDataFiles(th) m, err := th.makeKustTarget().MakeCustomizedResMap() if err != nil { t.Fatalf("Err: %v", err) } - th.assertActualEqualsExpected(m, ` -apiVersion: v1 -data: - FRUIT: YXBwbGU= - VEGETABLE: Y2Fycm90 - foo.env: Ck1PVU5UQUlOPWV2ZXJlc3QKT0NFQU49cGFjaWZpYwo= - passphrase: ZGF0IHBocmFzZQ== -kind: Secret -metadata: - name: bob-t98kdk9767 -type: Opaque -`) + th.assertActualEqualsExpected(m, result) +} + +func TestBuiltinIsTheDefault(t *testing.T) { + th := NewKustTestHarness(t, "/app") + th.writeK("/app", ` +secretGenerator: +- name: bob + kvSources: + - name: literals + 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) } diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 39a08700d..37b07f58f 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -271,9 +271,15 @@ type GeneratorOptions struct { 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. type KVSource struct { - PluginType string `json:"pluginType,omitempty" yaml:"pluginType,omitempty"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Args []string `json:"args,omitempty" yaml:"args,omitempty"` + PluginType PluginType `json:"pluginType,omitempty" yaml:"pluginType,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Args []string `json:"args,omitempty" yaml:"args,omitempty"` }