Short circuit anno/label transformer for performance.

This commit is contained in:
monopole
2021-01-08 17:44:08 -08:00
parent 6f2f401f6b
commit 769f65d6c4
7 changed files with 54 additions and 34 deletions

View File

@@ -24,6 +24,9 @@ func (p *AnnotationsTransformerPlugin) Config(
} }
func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error { func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error {
if len(p.Annotations) == 0 {
return nil
}
for _, r := range m.Resources() { for _, r := range m.Resources() {
err := r.ApplyFilter(annotations.Filter{ err := r.ApplyFilter(annotations.Filter{
Annotations: p.Annotations, Annotations: p.Annotations,

View File

@@ -24,6 +24,9 @@ func (p *LabelTransformerPlugin) Config(
} }
func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error { func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error {
if len(p.Labels) == 0 {
return nil
}
for _, r := range m.Resources() { for _, r := range m.Resources() {
err := r.ApplyFilter(labels.Filter{ err := r.ApplyFilter(labels.Filter{
Labels: p.Labels, Labels: p.Labels,

View File

@@ -59,6 +59,15 @@ func (ra *ResAccumulator) GetTransformerConfig() *builtinconfig.TransformerConfi
return ra.tConfig return ra.tConfig
} }
// MergeVars accumulates vars into ResAccumulator.
// A Var is a tuple of name, object reference and field reference.
// This func takes a list of vars from the current kustomization file and
// annotates the accumulated resources with the names of the vars that match
// those resources. E.g. if there's a var named "sam" that wants to get
// its data from a ConfigMap named "james", and the resource list contains a
// ConfigMap named "james", then that ConfigMap will be annotated with the
// var name "sam". Later this annotation is used to find the data for "sam"
// by digging into a particular fieldpath of "james".
func (ra *ResAccumulator) MergeVars(incoming []types.Var) error { func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
for _, v := range incoming { for _, v := range incoming {
targetId := resid.NewResIdWithNamespace(v.ObjRef.GVK(), v.ObjRef.Name, v.ObjRef.Namespace) targetId := resid.NewResIdWithNamespace(v.ObjRef.GVK(), v.ObjRef.Name, v.ObjRef.Namespace)
@@ -104,12 +113,10 @@ func (ra *ResAccumulator) findVarValueFromResources(v types.Var) (interface{}, e
"field specified in var '%v' "+ "field specified in var '%v' "+
"not found in corresponding resource", v) "not found in corresponding resource", v)
} }
return s, nil return s, nil
} }
} }
} }
return "", fmt.Errorf( return "", fmt.Errorf(
"var '%v' cannot be mapped to a field "+ "var '%v' cannot be mapped to a field "+
"in the set of known resources", v) "in the set of known resources", v)
@@ -125,10 +132,8 @@ func (ra *ResAccumulator) makeVarReplacementMap() (map[string]interface{}, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
result[v.Name] = s result[v.Name] = s
} }
return result, nil return result, nil
} }

View File

@@ -1976,67 +1976,64 @@ spec:
func TestDeploymentAnnotations(t *testing.T) { func TestDeploymentAnnotations(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("/app", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator: configMapGenerator:
- name: testConfigMap - name: theConfigMap
envs: envs:
- test.properties - test.properties
vars: vars:
- name: FOO - name: SOMERIVER
objref: objref:
kind: ConfigMap kind: ConfigMap
name: testConfigMap name: theConfigMap
apiVersion: v1 apiVersion: v1
fieldref: fieldref:
fieldpath: data.foo fieldpath: data.waterway
commonAnnotations: commonAnnotations:
foo: $(FOO) river: $(SOMERIVER)
resources: resources:
- deployment.yaml - deployment.yaml
`) `)
th.WriteF("/app/deployment.yaml", ` th.WriteF("deployment.yaml", `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: test name: theDeployment
spec: spec:
template: template:
spec: spec:
containers: containers:
- name: test - name: test
`) `)
th.WriteF("/app/test.properties", `foo=bar`) th.WriteF("test.properties", `waterway=mississippi`)
m := th.Run("/app", th.MakeDefaultOptions()) m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
annotations: annotations:
foo: bar river: mississippi
name: test name: theDeployment
spec: spec:
template: template:
metadata: metadata:
annotations: annotations:
foo: bar river: mississippi
spec: spec:
containers: containers:
- name: test - name: test
--- ---
apiVersion: v1 apiVersion: v1
data: data:
foo: bar waterway: mississippi
kind: ConfigMap kind: ConfigMap
metadata: metadata:
annotations: annotations:
foo: bar river: mississippi
name: testConfigMap-798k5k7g9f name: theConfigMap-hdd8h8cgdt
`) `)
} }

View File

@@ -648,9 +648,15 @@ func (s FieldSetter) Filter(rn *RNode) (*RNode, error) {
} }
// create the field // create the field
rn.YNode().Content = append(rn.YNode().Content, rn.YNode().Content = append(
&yaml.Node{Kind: yaml.ScalarNode, HeadComment: s.Comments.HeadComment, rn.YNode().Content,
LineComment: s.Comments.LineComment, FootComment: s.Comments.FootComment, Value: s.Name}, &yaml.Node{
Kind: yaml.ScalarNode,
Value: s.Name,
HeadComment: s.Comments.HeadComment,
LineComment: s.Comments.LineComment,
FootComment: s.Comments.FootComment,
},
s.Value.YNode()) s.Value.YNode())
return s.Value, nil return s.Value, nil
} }

View File

@@ -28,6 +28,9 @@ func (p *plugin) Config(
} }
func (p *plugin) Transform(m resmap.ResMap) error { func (p *plugin) Transform(m resmap.ResMap) error {
if len(p.Annotations) == 0 {
return nil
}
for _, r := range m.Resources() { for _, r := range m.Resources() {
err := r.ApplyFilter(annotations.Filter{ err := r.ApplyFilter(annotations.Filter{
Annotations: p.Annotations, Annotations: p.Annotations,

View File

@@ -28,6 +28,9 @@ func (p *plugin) Config(
} }
func (p *plugin) Transform(m resmap.ResMap) error { func (p *plugin) Transform(m resmap.ResMap) error {
if len(p.Labels) == 0 {
return nil
}
for _, r := range m.Resources() { for _, r := range m.Resources() {
err := r.ApplyFilter(labels.Filter{ err := r.ApplyFilter(labels.Filter{
Labels: p.Labels, Labels: p.Labels,