From 769f65d6c4d5de5d4cac420cb552bd7d3f5ae228 Mon Sep 17 00:00:00 2001 From: monopole Date: Fri, 8 Jan 2021 17:44:08 -0800 Subject: [PATCH] Short circuit anno/label transformer for performance. --- api/builtins/AnnotationsTransformer.go | 3 ++ api/builtins/LabelTransformer.go | 3 ++ api/internal/accumulator/resaccumulator.go | 13 +++-- api/krusty/variableref_test.go | 51 +++++++++---------- kyaml/yaml/fns.go | 12 +++-- .../AnnotationsTransformer.go | 3 ++ .../labeltransformer/LabelTransformer.go | 3 ++ 7 files changed, 54 insertions(+), 34 deletions(-) diff --git a/api/builtins/AnnotationsTransformer.go b/api/builtins/AnnotationsTransformer.go index 6cf7621e8..68d351e18 100644 --- a/api/builtins/AnnotationsTransformer.go +++ b/api/builtins/AnnotationsTransformer.go @@ -24,6 +24,9 @@ func (p *AnnotationsTransformerPlugin) Config( } func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error { + if len(p.Annotations) == 0 { + return nil + } for _, r := range m.Resources() { err := r.ApplyFilter(annotations.Filter{ Annotations: p.Annotations, diff --git a/api/builtins/LabelTransformer.go b/api/builtins/LabelTransformer.go index bb8b4837e..647e57efd 100644 --- a/api/builtins/LabelTransformer.go +++ b/api/builtins/LabelTransformer.go @@ -24,6 +24,9 @@ func (p *LabelTransformerPlugin) Config( } func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error { + if len(p.Labels) == 0 { + return nil + } for _, r := range m.Resources() { err := r.ApplyFilter(labels.Filter{ Labels: p.Labels, diff --git a/api/internal/accumulator/resaccumulator.go b/api/internal/accumulator/resaccumulator.go index bdf983eb5..1e4804e36 100644 --- a/api/internal/accumulator/resaccumulator.go +++ b/api/internal/accumulator/resaccumulator.go @@ -59,6 +59,15 @@ func (ra *ResAccumulator) GetTransformerConfig() *builtinconfig.TransformerConfi 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 { for _, v := range incoming { 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' "+ "not found in corresponding resource", v) } - return s, nil } } } - return "", fmt.Errorf( "var '%v' cannot be mapped to a field "+ "in the set of known resources", v) @@ -125,10 +132,8 @@ func (ra *ResAccumulator) makeVarReplacementMap() (map[string]interface{}, error if err != nil { return nil, err } - result[v.Name] = s } - return result, nil } diff --git a/api/krusty/variableref_test.go b/api/krusty/variableref_test.go index a93c28ddc..19379aa4e 100644 --- a/api/krusty/variableref_test.go +++ b/api/krusty/variableref_test.go @@ -1976,67 +1976,64 @@ spec: func TestDeploymentAnnotations(t *testing.T) { th := kusttest_test.MakeHarness(t) - th.WriteK("/app", ` -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization - + th.WriteK(".", ` configMapGenerator: - - name: testConfigMap - envs: - - test.properties +- name: theConfigMap + envs: + - test.properties vars: - - name: FOO - objref: - kind: ConfigMap - name: testConfigMap - apiVersion: v1 - fieldref: - fieldpath: data.foo +- name: SOMERIVER + objref: + kind: ConfigMap + name: theConfigMap + apiVersion: v1 + fieldref: + fieldpath: data.waterway commonAnnotations: - foo: $(FOO) + river: $(SOMERIVER) resources: - - deployment.yaml +- deployment.yaml `) - th.WriteF("/app/deployment.yaml", ` + th.WriteF("deployment.yaml", ` apiVersion: apps/v1 kind: Deployment metadata: - name: test + name: theDeployment spec: template: spec: containers: - - name: test + - name: test `) - th.WriteF("/app/test.properties", `foo=bar`) - m := th.Run("/app", th.MakeDefaultOptions()) + th.WriteF("test.properties", `waterway=mississippi`) + m := th.Run(".", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` apiVersion: apps/v1 kind: Deployment metadata: annotations: - foo: bar - name: test + river: mississippi + name: theDeployment spec: template: metadata: annotations: - foo: bar + river: mississippi spec: containers: - name: test --- apiVersion: v1 data: - foo: bar + waterway: mississippi kind: ConfigMap metadata: annotations: - foo: bar - name: testConfigMap-798k5k7g9f + river: mississippi + name: theConfigMap-hdd8h8cgdt `) } diff --git a/kyaml/yaml/fns.go b/kyaml/yaml/fns.go index e28b79e24..2271cc0b4 100644 --- a/kyaml/yaml/fns.go +++ b/kyaml/yaml/fns.go @@ -648,9 +648,15 @@ func (s FieldSetter) Filter(rn *RNode) (*RNode, error) { } // create the field - rn.YNode().Content = append(rn.YNode().Content, - &yaml.Node{Kind: yaml.ScalarNode, HeadComment: s.Comments.HeadComment, - LineComment: s.Comments.LineComment, FootComment: s.Comments.FootComment, Value: s.Name}, + rn.YNode().Content = append( + rn.YNode().Content, + &yaml.Node{ + Kind: yaml.ScalarNode, + Value: s.Name, + HeadComment: s.Comments.HeadComment, + LineComment: s.Comments.LineComment, + FootComment: s.Comments.FootComment, + }, s.Value.YNode()) return s.Value, nil } diff --git a/plugin/builtin/annotationstransformer/AnnotationsTransformer.go b/plugin/builtin/annotationstransformer/AnnotationsTransformer.go index 8454887b9..00bc1656f 100644 --- a/plugin/builtin/annotationstransformer/AnnotationsTransformer.go +++ b/plugin/builtin/annotationstransformer/AnnotationsTransformer.go @@ -28,6 +28,9 @@ func (p *plugin) Config( } func (p *plugin) Transform(m resmap.ResMap) error { + if len(p.Annotations) == 0 { + return nil + } for _, r := range m.Resources() { err := r.ApplyFilter(annotations.Filter{ Annotations: p.Annotations, diff --git a/plugin/builtin/labeltransformer/LabelTransformer.go b/plugin/builtin/labeltransformer/LabelTransformer.go index 8f5b13356..f362dd638 100644 --- a/plugin/builtin/labeltransformer/LabelTransformer.go +++ b/plugin/builtin/labeltransformer/LabelTransformer.go @@ -28,6 +28,9 @@ func (p *plugin) Config( } func (p *plugin) Transform(m resmap.ResMap) error { + if len(p.Labels) == 0 { + return nil + } for _, r := range m.Resources() { err := r.ApplyFilter(labels.Filter{ Labels: p.Labels,