From a8465c95e14d2bf3a71e16c45439507cdef3c5c1 Mon Sep 17 00:00:00 2001 From: Seth Pollack Date: Sun, 17 Mar 2019 17:22:52 -0400 Subject: [PATCH] add builtin files plugin --- .../configmapandsecret/basefactory_test.go | 17 ++++++ k8sdeps/kv/plugin/builtin/files.go | 49 ++++++++++++++++ k8sdeps/kv/plugin/builtinplugin.go | 3 +- pkg/target/builtinplugins_test.go | 58 +++++++++++++++++++ pkg/target/configmaps_test.go | 28 --------- 5 files changed, 126 insertions(+), 29 deletions(-) create mode 100644 k8sdeps/kv/plugin/builtin/files.go create mode 100644 pkg/target/builtinplugins_test.go diff --git a/k8sdeps/configmapandsecret/basefactory_test.go b/k8sdeps/configmapandsecret/basefactory_test.go index 3f0a545d5..c96fdbfda 100644 --- a/k8sdeps/configmapandsecret/basefactory_test.go +++ b/k8sdeps/configmapandsecret/basefactory_test.go @@ -87,9 +87,26 @@ func TestKeyValuesFromPlugins(t *testing.T) { }, }, }, + { + description: "Create kv.Pairs from builtin files plugin", + sources: []types.KVSource{ + { + PluginType: "builtin", + Name: "files", + Args: []string{"files/app-init.ini"}, + }, + }, + expected: []kv.Pair{ + { + Key: "app-init.ini", + Value: "FOO=bar", + }, + }, + }, } fSys := fs.MakeFakeFS() + fSys.WriteFile("/files/app-init.ini", []byte("FOO=bar")) ldr := loader.NewFileLoaderAtRoot(fSys) reg := plugin.NewRegistry(ldr) bf := baseFactory{ldr, nil, reg} diff --git a/k8sdeps/kv/plugin/builtin/files.go b/k8sdeps/kv/plugin/builtin/files.go new file mode 100644 index 000000000..48276d786 --- /dev/null +++ b/k8sdeps/kv/plugin/builtin/files.go @@ -0,0 +1,49 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package builtin + +import ( + "sigs.k8s.io/kustomize/k8sdeps/kv" + "sigs.k8s.io/kustomize/pkg/ifc" +) + +// Files is a list of file sources. +// Each file source can be specified using its file path, in which case file +// basename will be used as configmap key, or optionally with a key and file +// path, in which case the given key will be used. +// Specifying a directory will iterate each named file in the directory +// whose basename is a valid configmap key. +type Files struct { + Ldr ifc.Loader +} + +// Get implements the interface for kv plugins. +func (p Files) Get(root string, args []string) ([]kv.Pair, error) { + var kvs []kv.Pair + for _, s := range args { + k, fPath, err := kv.ParseFileSource(s) + if err != nil { + return nil, err + } + content, err := p.Ldr.Load(fPath) + if err != nil { + return nil, err + } + kvs = append(kvs, kv.Pair{Key: k, Value: string(content)}) + } + return kvs, nil +} diff --git a/k8sdeps/kv/plugin/builtinplugin.go b/k8sdeps/kv/plugin/builtinplugin.go index 3af666dd4..395445fc4 100644 --- a/k8sdeps/kv/plugin/builtinplugin.go +++ b/k8sdeps/kv/plugin/builtinplugin.go @@ -29,10 +29,11 @@ type builtinFactory struct { plugins map[string]KVSource } -func newBuiltinFactory(_ ifc.Loader) *builtinFactory { +func newBuiltinFactory(ldr ifc.Loader) *builtinFactory { return &builtinFactory{ plugins: map[string]KVSource{ "literals": builtin.Literals{}, + "files": builtin.Files{Ldr: ldr}, }, } } diff --git a/pkg/target/builtinplugins_test.go b/pkg/target/builtinplugins_test.go new file mode 100644 index 000000000..ca09cb1e1 --- /dev/null +++ b/pkg/target/builtinplugins_test.go @@ -0,0 +1,58 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package target_test + +import ( + "testing" +) + +func TestBuiltinPlugins(t *testing.T) { + th := NewKustTestHarness(t, "/app") + th.writeK("/app", ` +secretGenerator: +- name: bob + kvSources: + - pluginType: builtin + name: literals + args: + - FRUIT=apple + - VEGETABLE=carrot + - pluginType: builtin + name: files + args: + - foo.env +`) + th.writeF("/app/foo.env", ` +MOUNTAIN=everest +OCEAN=pacific +`) + 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= +kind: Secret +metadata: + name: bob-hhdkd5cbt9 +type: Opaque +`) +} diff --git a/pkg/target/configmaps_test.go b/pkg/target/configmaps_test.go index c482b6a7d..dd69adb3b 100644 --- a/pkg/target/configmaps_test.go +++ b/pkg/target/configmaps_test.go @@ -240,31 +240,3 @@ metadata: name: p2-com2-c4b8md75k9 `) } - -func TestGeneratorPlugins(t *testing.T) { - th := NewKustTestHarness(t, "/app") - th.writeK("/app", ` -secretGenerator: -- name: bob - kvSources: - - pluginType: builtin - name: literals - args: - - FRUIT=apple - - VEGETABLE=carrot -`) - m, err := th.makeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } - th.assertActualEqualsExpected(m, ` -apiVersion: v1 -data: - FRUIT: YXBwbGU= - VEGETABLE: Y2Fycm90 -kind: Secret -metadata: - name: bob-bgtct8h699 -type: Opaque -`) -}