Skip to content

Commit

Permalink
Merge pull request #1041 from monopole/introduceEnvs
Browse files Browse the repository at this point in the history
Introduce envs field.
  • Loading branch information
k8s-ci-robot authored May 7, 2019
2 parents f2295ac + 529db04 commit 8c5d412
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 84 deletions.
6 changes: 3 additions & 3 deletions docs/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ secretGenerator:
- tls.key=secret/tls.key
type: "kubernetes.io/tls"
- name: env_file_secret
# env is a path to a file to read lines of key=val
# you can only specify one env file per secret.
env: env.txt
# paths to files with k=v pairs, one pair per line.
envs:
- env.txt
type: Opaque

# generatorOptions modify behavior of all ConfigMap and Secret generators
Expand Down
30 changes: 17 additions & 13 deletions k8sdeps/configmapandsecret/basefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ func (bf baseFactory) loadKvPairs(
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf(
"plugins: %s",
args.EnvSource))
args.KVSources))
}
all = append(all, pairs...)

pairs, err = bf.keyValuesFromEnvFile(args.EnvSource)
pairs, err = bf.keyValuesFromEnvFiles(args.EnvSources)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf(
"env source file: %s",
args.EnvSource))
"env source files: %v",
args.EnvSources))
}
all = append(all, pairs...)

Expand Down Expand Up @@ -136,13 +135,18 @@ func (bf baseFactory) keyValuesFromFileSources(sources []string) ([]kv.Pair, err
return kvs, nil
}

func (bf baseFactory) keyValuesFromEnvFile(path string) ([]kv.Pair, error) {
if path == "" {
return nil, nil
}
content, err := bf.ldr.Load(path)
if err != nil {
return nil, err
func (bf baseFactory) keyValuesFromEnvFiles(paths []string) ([]kv.Pair, error) {
var kvs []kv.Pair
for _, path := range paths {
content, err := bf.ldr.Load(path)
if err != nil {
return nil, err
}
more, err := kv.KeyValuesFromLines(content)
if err != nil {
return nil, err
}
kvs = append(kvs, more...)
}
return kv.KeyValuesFromLines(content)
return kvs, nil
}
2 changes: 1 addition & 1 deletion k8sdeps/configmapandsecret/configmapfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestConstructConfigMap(t *testing.T) {
GeneratorArgs: types.GeneratorArgs{
Name: "envConfigMap",
DataSources: types.DataSources{
EnvSource: "configmap/app.env",
EnvSources: []string{"configmap/app.env"},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion k8sdeps/configmapandsecret/secretfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestConstructSecret(t *testing.T) {
GeneratorArgs: types.GeneratorArgs{
Name: "envSecret",
DataSources: types.DataSources{
EnvSource: "secret/app.env",
EnvSources: []string{"secret/app.env"},
},
},
},
Expand Down
1 change: 0 additions & 1 deletion k8sdeps/kv/plugin/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Registry struct {
}

const (
PluginSymbol = "KustomizePlugin"
PluginRoot = "plugin"
pluginTypeGo = types.PluginType("go")
pluginTypeBuiltIn = types.PluginType("builtin")
Expand Down
7 changes: 2 additions & 5 deletions pkg/commands/kustfile/kustomizationfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,13 @@ func (mf *kustomizationFile) Read() (*types.Kustomization, error) {
if err != nil {
return nil, err
}
data = types.DealWithDeprecatedFields(data)
data = types.FixKustomizationPreUnmarshalling(data)
var k types.Kustomization
err = yaml.Unmarshal(data, &k)
if err != nil {
return nil, err
}
msgs := k.DealWithMissingFields()
if len(msgs) > 0 {
log.Printf(strings.Join(msgs, "\n"))
}
k.FixKustomizationPostUnmarshalling()
err = mf.parseCommentedFields(data)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/kustfile/kustomizationfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestWriteAndRead(t *testing.T) {
if err != nil {
t.Fatalf("Couldn't read kustomization file: %v\n", err)
}
kustomization.DealWithMissingFields()
kustomization.FixKustomizationPostUnmarshalling()
if !reflect.DeepEqual(kustomization, content) {
t.Fatal("Read kustomization is different from written kustomization")
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/plugins/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
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 plugins

const pluginSymbol = "KustomizePlugin"
5 changes: 2 additions & 3 deletions pkg/plugins/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"plugin"

"github.com/pkg/errors"
kplugin "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
Expand Down Expand Up @@ -122,11 +121,11 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (c Configurable, err error) {
if err != nil {
return nil, errors.Wrapf(err, "plugin %s fails to load", name)
}
symbol, err := p.Lookup(kplugin.PluginSymbol)
symbol, err := p.Lookup(pluginSymbol)
if err != nil {
return nil, errors.Wrapf(
err, "plugin %s doesn't have symbol %s",
name, kplugin.PluginSymbol)
name, pluginSymbol)
}
c, ok = symbol.(Configurable)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/resmap/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestNewFromConfigMaps(t *testing.T) {
GeneratorArgs: types.GeneratorArgs{
Name: "envConfigMap",
DataSources: types.DataSources{
EnvSource: "app.env",
EnvSources: []string{"app.env"},
},
},
},
Expand Down
44 changes: 29 additions & 15 deletions pkg/target/baseandoverlaymedium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,33 @@ patchesStrategicMerge:
- deployment/deployment.yaml
configMapGenerator:
- name: app-env
env: configmap/app.env
env: configmap/db.env
envs:
- configmap/units.ini
- configmap/food.ini
- name: app-config
files:
- configmap/app-init.ini
- nonsense=configmap/dummy.txt
images:
- name: nginx
newTag: 1.8.0`)

th.writeF("/app/overlay/configmap/app.env", `
th.writeF("/app/overlay/configmap/db.env", `
DB_USERNAME=admin
DB_PASSWORD=somepw
`)
th.writeF("/app/overlay/configmap/app-init.ini", `
FOO=bar
BAR=baz
th.writeF("/app/overlay/configmap/units.ini", `
LENGTH=kilometer
ENERGY=electronvolt
`)
th.writeF("/app/overlay/configmap/food.ini", `
FRUIT=banana
LEGUME=chickpea
`)
th.writeF("/app/overlay/configmap/dummy.txt",
`Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
`)
th.writeF("/app/overlay/deployment/deployment.yaml", `
apiVersion: extensions/v1beta1
Expand Down Expand Up @@ -217,10 +229,8 @@ spec:
th.assertActualEqualsExpected(m, `
apiVersion: v1
data:
app-init.ini: |2
FOO=bar
BAR=baz
nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod
tempor\nincididunt ut labore et dolore magna aliqua. \n"
kind: ConfigMap
metadata:
annotations:
Expand All @@ -229,12 +239,16 @@ metadata:
app: mungebot
org: kubernetes
repo: test-infra
name: test-infra-app-config-fd62mfc87h
name: test-infra-app-config-f462h769f9
---
apiVersion: v1
data:
DB_PASSWORD: somepw
DB_USERNAME: admin
ENERGY: electronvolt
FRUIT: banana
LEGUME: chickpea
LENGTH: kilometer
kind: ConfigMap
metadata:
annotations:
Expand All @@ -243,7 +257,7 @@ metadata:
app: mungebot
org: kubernetes
repo: test-infra
name: test-infra-app-env-bh449c299k
name: test-infra-app-env-ffmd9b969m
---
apiVersion: v1
kind: Service
Expand Down Expand Up @@ -303,7 +317,7 @@ spec:
valueFrom:
configMapKeyRef:
key: somekey
name: test-infra-app-env-bh449c299k
name: test-infra-app-env-ffmd9b969m
- name: foo
value: bar
image: nginx:1.8.0
Expand All @@ -314,15 +328,15 @@ spec:
- configMapRef:
name: someConfigMap
- configMapRef:
name: test-infra-app-env-bh449c299k
name: test-infra-app-env-ffmd9b969m
image: busybox
name: busybox
volumeMounts:
- mountPath: /tmp/env
name: app-env
volumes:
- configMap:
name: test-infra-app-env-bh449c299k
name: test-infra-app-env-ffmd9b969m
name: app-env
`)
}
3 changes: 2 additions & 1 deletion pkg/target/generatorplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ generators:
th.assertActualEqualsExpected(m, `
apiVersion: v1
data:
DB_PASSWORD: aWxvdmV5b3U=
FRUIT: YXBwbGU=
ROUTER_PASSWORD: YWRtaW4=
VEGETABLE: Y2Fycm90
longsecret.txt: CkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0LApjb25zZWN0ZXR1ciBhZGlwaXNjaW5nIGVsaXQsCnNlZCBkbyBlaXVzbW9kIHRlbXBvciBpbmNpZGlkdW50CnV0IGxhYm9yZSBldCBkb2xvcmUgbWFnbmEgYWxpcXVhLgo=
kind: Secret
metadata:
name: -2kt2h55789
name: -ktm999dkcc
type: Opaque
`)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ func NewKustTarget(
if err != nil {
return nil, err
}
content = types.DealWithDeprecatedFields(content)
content = types.FixKustomizationPreUnmarshalling(content)
var k types.Kustomization
err = unmarshal(content, &k)
if err != nil {
return nil, err
}
k.FixKustomizationPostUnmarshalling()
errs := k.EnforceFields()
if len(errs) > 0 {
return nil, fmt.Errorf(
Expand Down
Loading

0 comments on commit 8c5d412

Please sign in to comment.