Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#892 from monopole/defaultToBuiltin
Browse files Browse the repository at this point in the history
Make builtin the default pluginType
  • Loading branch information
monopole authored Mar 18, 2019
2 parents 449b1b6 + 3a7c8a0 commit 2965134
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
24 changes: 15 additions & 9 deletions k8sdeps/kv/plugin/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
},
}
}
Expand All @@ -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)
}
Expand Down
61 changes: 45 additions & 16 deletions pkg/target/builtinplugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", `
Expand All @@ -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)
}
12 changes: 9 additions & 3 deletions pkg/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 2965134

Please sign in to comment.