add builtin envfiles plugin

This commit is contained in:
Seth Pollack
2019-03-18 13:58:38 -04:00
parent 2965134f89
commit 9fc4d388ce
4 changed files with 76 additions and 1 deletions

View File

@@ -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()

View File

@@ -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
}

View File

@@ -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},
},
}
}

View File

@@ -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()