Enable specifying GeneratorOptions per resource

In addition to specifying GeneratorOptions globally within a
kustomization, also allow users to set GeneratorOptions on a
per-resource level.
This commit is contained in:
Chance Zibolski
2020-02-05 10:15:55 -08:00
parent 5707962df5
commit 62f21cbe69
6 changed files with 137 additions and 14 deletions

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"