Merge pull request #2182 from chancez/per_resource_generator_options

Enable specifying GeneratorOptions per resource
This commit is contained in:
Kubernetes Prow Robot
2020-03-06 14:51:34 -08:00
committed by GitHub
8 changed files with 161 additions and 17 deletions

View File

@@ -38,10 +38,7 @@ func (f *Factory) MakeConfigMap(
return nil, errors.Wrap(err, "trouble mapping")
}
}
if f.options != nil {
cm.SetLabels(f.options.Labels)
cm.SetAnnotations(f.options.Annotations)
}
f.setLabelsAndAnnnotations(cm, args.GeneratorOptions)
return cm, nil
}

View File

@@ -53,7 +53,7 @@ BAR=baz
}
}
func makeLiteralConfigMap(name string) *corev1.ConfigMap {
func makeLiteralConfigMap(name string, labels, annotations map[string]string) *corev1.ConfigMap {
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
@@ -69,7 +69,12 @@ func makeLiteralConfigMap(name string) *corev1.ConfigMap {
"d": "true",
},
}
cm.SetLabels(map[string]string{"foo": "bar"})
if labels != nil {
cm.SetLabels(labels)
}
if annotations != nil {
cm.SetAnnotations(annotations)
}
return cm
}
@@ -128,7 +133,49 @@ func TestConstructConfigMap(t *testing.T) {
"foo": "bar",
},
},
expected: makeLiteralConfigMap("literalConfigMap"),
expected: makeLiteralConfigMap("literalConfigMap", map[string]string{
"foo": "bar",
}, nil),
},
{
description: "construct config map from literal with GeneratorOptions in ConfigMapArgs",
input: types.ConfigMapArgs{
GeneratorArgs: types.GeneratorArgs{
Name: "literalConfigMap",
KvPairSources: types.KvPairSources{
LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
},
GeneratorOptions: &types.GeneratorOptions{
Labels: map[string]string{
"foo": "changed",
"cat": "dog",
},
Annotations: map[string]string{
"foo": "changed",
"cat": "dog",
},
},
},
},
options: &types.GeneratorOptions{
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{
"foo": "bar",
},
},
// GeneratorOptions from the ConfigMapArgs take precedence over the
// factory level GeneratorOptions and should overwrite
// labels/annotations set in the factory level if there are common
// labels/annotations
expected: makeLiteralConfigMap("literalConfigMap", map[string]string{
"foo": "changed",
"cat": "dog",
}, map[string]string{
"foo": "changed",
"cat": "dog",
}),
},
}

View File

@@ -4,6 +4,7 @@
package configmapandsecret
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/types"
)
@@ -20,4 +21,35 @@ func NewFactory(
return &Factory{kvLdr: kvLdr, options: o}
}
// setLabelsAndAnnnotations will take the labels and annotations from
// global GeneratorOptions and resource level GeneratorOptions and merge them
// with the resource level taking precedence, and then set them on the provided
// obj.
func (f *Factory) setLabelsAndAnnnotations(obj metav1.Object, opts *types.GeneratorOptions) {
labels := make(map[string]string)
annotations := make(map[string]string)
if f.options != nil {
for k, v := range f.options.Labels {
labels[k] = v
}
for k, v := range f.options.Annotations {
annotations[k] = v
}
}
if opts != nil {
for k, v := range opts.Labels {
labels[k] = v
}
for k, v := range opts.Annotations {
annotations[k] = v
}
}
if len(labels) != 0 {
obj.SetLabels(labels)
}
if len(annotations) != 0 {
obj.SetAnnotations(annotations)
}
}
const keyExistsErrorMsg = "cannot add key %s, another key by that name already exists: %v"

View File

@@ -39,10 +39,7 @@ func (f *Factory) MakeSecret(
return nil, err
}
}
if f.options != nil {
s.SetLabels(f.options.Labels)
s.SetAnnotations(f.options.Annotations)
}
f.setLabelsAndAnnnotations(s, args.GeneratorOptions)
return s, nil
}

View File

@@ -51,7 +51,7 @@ BAR=baz
}
}
func makeLiteralSecret(name string) *corev1.Secret {
func makeLiteralSecret(name string, labels, annotations map[string]string) *corev1.Secret {
s := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
@@ -66,7 +66,12 @@ func makeLiteralSecret(name string) *corev1.Secret {
},
Type: "Opaque",
}
s.SetLabels(map[string]string{"foo": "bar"})
if labels != nil {
s.SetLabels(labels)
}
if annotations != nil {
s.SetAnnotations(annotations)
}
return s
}
@@ -120,7 +125,49 @@ func TestConstructSecret(t *testing.T) {
"foo": "bar",
},
},
expected: makeLiteralSecret("literalSecret"),
expected: makeLiteralSecret("literalSecret", map[string]string{
"foo": "bar",
}, nil),
},
{
description: "construct secret from literal with GeneratorOptions in SecretArgs",
input: types.SecretArgs{
GeneratorArgs: types.GeneratorArgs{
Name: "literalSecret",
KvPairSources: types.KvPairSources{
LiteralSources: []string{"a=x", "b=y"},
},
GeneratorOptions: &types.GeneratorOptions{
Labels: map[string]string{
"foo": "changed",
"cat": "dog",
},
Annotations: map[string]string{
"foo": "changed",
"cat": "dog",
},
},
},
},
options: &types.GeneratorOptions{
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{
"foo": "bar",
},
},
// GeneratorOptions from the SecretArgs take precedence over the
// factory level GeneratorOptions and should overwrite
// labels/annotations set in the factory level if there are common
// labels/annotations
expected: makeLiteralSecret("literalSecret", map[string]string{
"foo": "changed",
"cat": "dog",
}, map[string]string{
"foo": "changed",
"cat": "dog",
}),
},
}

View File

@@ -21,4 +21,7 @@ type GeneratorArgs struct {
// KvPairSources for the generator.
KvPairSources `json:",inline,omitempty" yaml:",inline,omitempty"`
// GeneratorOptions modify this generator
GeneratorOptions *GeneratorOptions `json:"generatorOptions,omitempty" yaml:"generatorOptions,omitempty"`
}

View File

@@ -131,6 +131,10 @@ crds:
Modifies behavior of all [ConfigMap](#configmapgenerator)
and [Secret](#secretgenerator) generators.
Additionally, generatorOptions can be set on a per resource level within each
generator. For details on per-resource generatorOptions usage see
[field-name-configMapGenerator] and See [field-name-secretGenerator].
```
generatorOptions:
# labels to add to all generated resources

View File

@@ -90,9 +90,9 @@ commonAnnotations:
Each entry in this list results in the creation of
one ConfigMap resource (it's a generator of n maps).
The example below creates two ConfigMaps. One with the
names and contents of the given files, the other with
key/value as data.
The example below creates three ConfigMaps. One with the names and contents of
the given files, one with key/value as data, and a third which sets an
annotation and label via generatorOptions for that single ConfigMap.
Each configMapGenerator item accepts a parameter of
`behavior: [create|replace|merge]`.
@@ -109,6 +109,14 @@ configMapGenerator:
literals:
- JAVA_HOME=/opt/java/jdk
- JAVA_TOOL_OPTIONS=-agentlib:hprof
- name: dashboards
files:
- mydashboard.json
generatorOptions:
annotations:
dashboard: "1"
labels:
app.kubernetes.io/name: "app1"
```
It is also possible to
@@ -651,6 +659,15 @@ secretGenerator:
envs:
- env.txt
type: Opaque
- name: secret-with-annotation
files:
- app-config.yaml
type: Opaque
generatorOptions:
annotations:
app_config: "true"
labels:
app.kubernetes.io/name: "app2"
```
### Usage via plugin