From 9fc4d388ce3b09ecb7b509780e533355e8db6c07 Mon Sep 17 00:00:00 2001 From: Seth Pollack Date: Mon, 18 Mar 2019 13:58:38 -0400 Subject: [PATCH] add builtin envfiles plugin --- .../configmapandsecret/basefactory_test.go | 16 ++++++ k8sdeps/kv/plugin/builtin/envfiles.go | 49 +++++++++++++++++++ k8sdeps/kv/plugin/builtinplugin.go | 1 + pkg/target/builtinplugins_test.go | 11 ++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 k8sdeps/kv/plugin/builtin/envfiles.go diff --git a/k8sdeps/configmapandsecret/basefactory_test.go b/k8sdeps/configmapandsecret/basefactory_test.go index c96fdbfda..75d5eda9e 100644 --- a/k8sdeps/configmapandsecret/basefactory_test.go +++ b/k8sdeps/configmapandsecret/basefactory_test.go @@ -103,6 +103,22 @@ func TestKeyValuesFromPlugins(t *testing.T) { }, }, }, + { + description: "Create kv.Pairs from builtin envfiles plugin", + sources: []types.KVSource{ + { + PluginType: "builtin", + Name: "envfiles", + Args: []string{"files/app-init.ini"}, + }, + }, + expected: []kv.Pair{ + { + Key: "FOO", + Value: "bar", + }, + }, + }, } fSys := fs.MakeFakeFS() diff --git a/k8sdeps/kv/plugin/builtin/envfiles.go b/k8sdeps/kv/plugin/builtin/envfiles.go new file mode 100644 index 000000000..ad3993dff --- /dev/null +++ b/k8sdeps/kv/plugin/builtin/envfiles.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" +) + +// Envfiles format should be a path to a file to read lines of key=val +// pairs to create a configmap. +// i.e. a Docker .env file or a .ini file. +type Envfiles struct { + Ldr ifc.Loader +} + +// Get implements the interface for kv plugins. +func (p Envfiles) Get(root string, args []string) ([]kv.Pair, error) { + var all []kv.Pair + for _, path := range args { + if path == "" { + return nil, nil + } + content, err := p.Ldr.Load(path) + if err != nil { + return nil, err + } + kvs, err := kv.KeyValuesFromLines(content) + if err != nil { + return nil, err + } + all = append(all, kvs...) + } + return all, nil +} diff --git a/k8sdeps/kv/plugin/builtinplugin.go b/k8sdeps/kv/plugin/builtinplugin.go index 395445fc4..ec2045ca7 100644 --- a/k8sdeps/kv/plugin/builtinplugin.go +++ b/k8sdeps/kv/plugin/builtinplugin.go @@ -34,6 +34,7 @@ func newBuiltinFactory(ldr ifc.Loader) *builtinFactory { plugins: map[string]KVSource{ "literals": builtin.Literals{}, "files": builtin.Files{Ldr: ldr}, + "envfiles": builtin.Envfiles{Ldr: ldr}, }, } } diff --git a/pkg/target/builtinplugins_test.go b/pkg/target/builtinplugins_test.go index 07bf3bfa9..90fc27f76 100644 --- a/pkg/target/builtinplugins_test.go +++ b/pkg/target/builtinplugins_test.go @@ -24,12 +24,14 @@ const result = ` apiVersion: v1 data: FRUIT: YXBwbGU= + MOUNTAIN: ZXZlcmVzdA== + OCEAN: cGFjaWZpYw== VEGETABLE: Y2Fycm90 foo.env: Ck1PVU5UQUlOPWV2ZXJlc3QKT0NFQU49cGFjaWZpYwo= passphrase: ZGF0IHBocmFzZQ== kind: Secret metadata: - name: bob-t98kdk9767 + name: bob-kf5c9fccbt type: Opaque ` @@ -57,6 +59,10 @@ secretGenerator: args: - foo.env - passphrase=phrase.dat + - pluginType: builtin + name: envfiles + args: + - foo.env `) writeDataFiles(th) m, err := th.makeKustTarget().MakeCustomizedResMap() @@ -80,6 +86,9 @@ secretGenerator: args: - foo.env - passphrase=phrase.dat + - name: envfiles + args: + - foo.env `) writeDataFiles(th) m, err := th.makeKustTarget().MakeCustomizedResMap()