Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sethpollack committed Mar 11, 2019
1 parent 35f7abf commit d2bd3ec
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 44 deletions.
20 changes: 10 additions & 10 deletions k8sdeps/configmapandsecret/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions k8sdeps/configmapandsecret/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions k8sdeps/configmapandsecret/secretfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion k8sdeps/configmapandsecret/secretfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion k8sdeps/kunstruct/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions k8sdeps/kv/plugin/goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions k8sdeps/kv/plugin/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}

Expand Down
14 changes: 7 additions & 7 deletions k8sdeps/kv/plugin/testonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions pkg/target/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestGeneratorPlugins(t *testing.T) {
secretGenerator:
- name: bob
kvSources:
- type: testonly
- pluginType: testonly
name: testonly
args:
- FRUIT
Expand All @@ -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
`)
}
6 changes: 3 additions & 3 deletions pkg/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit d2bd3ec

Please sign in to comment.