From d2bd3ecb116c912113e7c10abd87b236bda4a13c Mon Sep 17 00:00:00 2001 From: Seth Pollack Date: Sun, 10 Mar 2019 17:50:24 -0400 Subject: [PATCH] refactor --- k8sdeps/configmapandsecret/kv.go | 20 +++++++++---------- k8sdeps/configmapandsecret/kv_test.go | 18 ++++++++--------- k8sdeps/configmapandsecret/secretfactory.go | 8 ++++---- .../configmapandsecret/secretfactory_test.go | 4 +++- k8sdeps/kunstruct/factory.go | 3 ++- k8sdeps/kv/plugin/goplugin.go | 6 +++--- k8sdeps/kv/plugin/registry.go | 7 +++++-- k8sdeps/kv/plugin/testonly.go | 14 ++++++------- pkg/target/configmaps_test.go | 8 ++++---- pkg/types/kustomization.go | 6 +++--- 10 files changed, 50 insertions(+), 44 deletions(-) diff --git a/k8sdeps/configmapandsecret/kv.go b/k8sdeps/configmapandsecret/kv.go index 1889229d9bb..e050a70692a 100644 --- a/k8sdeps/configmapandsecret/kv.go +++ b/k8sdeps/configmapandsecret/kv.go @@ -28,32 +28,32 @@ import ( "sigs.k8s.io/kustomize/pkg/types" ) -func keyValuesFromPlugins(registry plugin.Registry, args types.KVSource) ([]kv.Pair, error) { - plug, err := registry.Load(args.Type, args.Name) +func keyValuesFromPlugins(registry plugin.Registry, source types.KVSource) ([]kv.Pair, error) { + plug, err := registry.Load(source.PluginType, source.Name) if err != nil { return nil, err } - kvs, err := plug.Get(registry.Root(), args.Args) + pairs, err := plug.Get(registry.Root(), source.Args) if err != nil { return nil, err } - return kvs, nil + return pairs, nil } func keyValuesFromLiteralSources(sources []string) ([]kv.Pair, error) { - var kvs []kv.Pair + var pairs []kv.Pair for _, s := range sources { k, v, err := parseLiteralSource(s) if err != nil { return nil, err } - kvs = append(kvs, kv.Pair{Key: k, Value: v}) + pairs = append(pairs, kv.Pair{Key: k, Value: v}) } - return kvs, nil + return pairs, nil } func keyValuesFromFileSources(ldr ifc.Loader, sources []string) ([]kv.Pair, error) { - var kvs []kv.Pair + var pairs []kv.Pair for _, s := range sources { k, fPath, err := parseFileSource(s) if err != nil { @@ -63,9 +63,9 @@ func keyValuesFromFileSources(ldr ifc.Loader, sources []string) ([]kv.Pair, erro if err != nil { return nil, err } - kvs = append(kvs, kv.Pair{Key: k, Value: string(content)}) + pairs = append(pairs, kv.Pair{Key: k, Value: string(content)}) } - return kvs, nil + return pairs, nil } func keyValuesFromEnvFile(l ifc.Loader, path string) ([]kv.Pair, error) { diff --git a/k8sdeps/configmapandsecret/kv_test.go b/k8sdeps/configmapandsecret/kv_test.go index 67cd675b26f..71a9c409c56 100644 --- a/k8sdeps/configmapandsecret/kv_test.go +++ b/k8sdeps/configmapandsecret/kv_test.go @@ -37,23 +37,23 @@ func TestKeyValuesFromPlugins(t *testing.T) { description: "Create kv.Pairs from plugin", sources: []types.KVSource{ { - Type: "testonly", - Name: "testonly", - Args: []string{"FOO", "BAR", "BAZ"}, + PluginType: "testonly", + Name: "testonly", + Args: []string{"FOO", "BAR", "BAZ"}, }, }, expected: []kv.Pair{ { - Key: "FOO", - Value: "FOO", + Key: "k_FOO", + Value: "v_FOO", }, { - Key: "BAR", - Value: "BAR", + Key: "k_BAR", + Value: "v_BAR", }, { - Key: "BAZ", - Value: "BAZ", + Key: "k_BAZ", + Value: "v_BAZ", }, }, }, diff --git a/k8sdeps/configmapandsecret/secretfactory.go b/k8sdeps/configmapandsecret/secretfactory.go index cad64cbb940..abba4b80edb 100644 --- a/k8sdeps/configmapandsecret/secretfactory.go +++ b/k8sdeps/configmapandsecret/secretfactory.go @@ -32,11 +32,12 @@ import ( // SecretFactory makes Secrets. type SecretFactory struct { ldr ifc.Loader + reg plugin.Registry } // NewSecretFactory returns a new SecretFactory. -func NewSecretFactory(ldr ifc.Loader) *SecretFactory { - return &SecretFactory{ldr: ldr} +func NewSecretFactory(ldr ifc.Loader, reg plugin.Registry) *SecretFactory { + return &SecretFactory{ldr: ldr, reg: reg} } func (f *SecretFactory) makeFreshSecret(args *types.SecretArgs) *corev1.Secret { @@ -59,9 +60,8 @@ func (f *SecretFactory) MakeSecret(args *types.SecretArgs, options *types.Genera var err error s := f.makeFreshSecret(args) - registry := plugin.NewRegistry(f.ldr) for _, plug := range args.KVSources { - pairs, err := keyValuesFromPlugins(registry, plug) + pairs, err := keyValuesFromPlugins(f.reg, plug) if err != nil { return nil, err } diff --git a/k8sdeps/configmapandsecret/secretfactory_test.go b/k8sdeps/configmapandsecret/secretfactory_test.go index da405a27f8e..afe8e9bc782 100644 --- a/k8sdeps/configmapandsecret/secretfactory_test.go +++ b/k8sdeps/configmapandsecret/secretfactory_test.go @@ -22,6 +22,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/types" @@ -138,7 +139,8 @@ func TestConstructSecret(t *testing.T) { fSys := fs.MakeFakeFS() fSys.WriteFile("/secret/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n")) fSys.WriteFile("/secret/app-init.ini", []byte("FOO=bar\nBAR=baz\n")) - f := NewSecretFactory(loader.NewFileLoaderAtRoot(fSys)) + ldr := loader.NewFileLoaderAtRoot(fSys) + f := NewSecretFactory(ldr, plugin.NewRegistry(ldr)) for _, tc := range testCases { cm, err := f.MakeSecret(&tc.input, tc.options) if err != nil { diff --git a/k8sdeps/kunstruct/factory.go b/k8sdeps/kunstruct/factory.go index cfd3ad309a5..a6662eba653 100644 --- a/k8sdeps/kunstruct/factory.go +++ b/k8sdeps/kunstruct/factory.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/util/yaml" "sigs.k8s.io/kustomize/k8sdeps/configmapandsecret" + "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/types" ) @@ -99,7 +100,7 @@ func (kf *KunstructuredFactoryImpl) MakeSecret(args *types.SecretArgs, options * // Set sets loader func (kf *KunstructuredFactoryImpl) Set(ldr ifc.Loader) { kf.cmFactory = configmapandsecret.NewConfigMapFactory(ldr) - kf.secretFactory = configmapandsecret.NewSecretFactory(ldr) + kf.secretFactory = configmapandsecret.NewSecretFactory(ldr, plugin.NewRegistry(ldr)) } // validate validates that u has kind and name diff --git a/k8sdeps/kv/plugin/goplugin.go b/k8sdeps/kv/plugin/goplugin.go index b43b8ef2997..d316a76cd31 100644 --- a/k8sdeps/kv/plugin/goplugin.go +++ b/k8sdeps/kv/plugin/goplugin.go @@ -28,10 +28,10 @@ const ( dir = "$HOME/.config/kustomize/plugins/kvsource" ) -func init() { - register("go", &goFactory{ +func newGoFactory() *goFactory { + return &goFactory{ plugins: make(map[string]KVSource), - }) + } } type goFactory struct { diff --git a/k8sdeps/kv/plugin/registry.go b/k8sdeps/kv/plugin/registry.go index 3ce61a49274..ded74641495 100644 --- a/k8sdeps/kv/plugin/registry.go +++ b/k8sdeps/kv/plugin/registry.go @@ -37,8 +37,11 @@ type Registry struct { // NewRegistry returns a new Registry loaded with all the factories. func NewRegistry(ldr ifc.Loader) Registry { return Registry{ - ldr: ldr, - factories: factories, + ldr: ldr, + factories: map[string]Factory{ + "go": newGoFactory(), + "testonly": newTestonlyFactory(), + }, } } diff --git a/k8sdeps/kv/plugin/testonly.go b/k8sdeps/kv/plugin/testonly.go index 0491c06f100..a06da588a68 100644 --- a/k8sdeps/kv/plugin/testonly.go +++ b/k8sdeps/kv/plugin/testonly.go @@ -22,22 +22,22 @@ import ( "sigs.k8s.io/kustomize/k8sdeps/kv" ) -var _ Factory = &testonly{} +var _ Factory = &testonlyFactory{} -func init() { - register("testonly", &testonly{}) +func newTestonlyFactory() *testonlyFactory { + return &testonlyFactory{} } -type testonly struct{} +type testonlyFactory struct{} -func (p testonly) Get(root string, args []string) ([]kv.Pair, error) { +func (p testonlyFactory) Get(_ string, args []string) ([]kv.Pair, error) { var kvs []kv.Pair for _, arg := range args { - kvs = append(kvs, kv.Pair{Key: arg, Value: arg}) + kvs = append(kvs, kv.Pair{Key: "k_" + arg, Value: "v_" + arg}) } return kvs, nil } -func (p *testonly) load(name string) (KVSource, error) { +func (p *testonlyFactory) load(name string) (KVSource, error) { return p, nil } diff --git a/pkg/target/configmaps_test.go b/pkg/target/configmaps_test.go index b6fda9695be..5f8e91418bb 100644 --- a/pkg/target/configmaps_test.go +++ b/pkg/target/configmaps_test.go @@ -237,7 +237,7 @@ func TestGeneratorPlugins(t *testing.T) { secretGenerator: - name: bob kvSources: - - type: testonly + - pluginType: testonly name: testonly args: - FRUIT @@ -250,11 +250,11 @@ secretGenerator: th.assertActualEqualsExpected(m, ` apiVersion: v1 data: - FRUIT: RlJVSVQ= - VEGETABLE: VkVHRVRBQkxF + k_FRUIT: dl9GUlVJVA== + k_VEGETABLE: dl9WRUdFVEFCTEU= kind: Secret metadata: - name: bob-h5fb5tmhk9 + name: bob-cb9mhbh9gg type: Opaque `) } diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 6ae54e4eab9..c928674223e 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -254,7 +254,7 @@ type GeneratorOptions struct { // KVSource represents a KV plugin backend. type KVSource struct { - Type string `json:"type,omitempty" yaml:"type,omitempty"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Args []string `json:"args,omitempty" yaml:"args,omitempty"` + PluginType string `json:"pluginType,omitempty" yaml:"pluginType,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Args []string `json:"args,omitempty" yaml:"args,omitempty"` }