From 6544629d4e428ed4d5588ee48606690fcd58781a Mon Sep 17 00:00:00 2001 From: Balazs Nagy Date: Sat, 22 Oct 2022 22:25:58 +0200 Subject: [PATCH] distinguish individual resources with generateName --- api/filters/nameref/nameref.go | 9 ++- api/krusty/resourceconflict_test.go | 86 +++++++++++++++++++++++++++++ kyaml/yaml/rnode.go | 18 +++++- 3 files changed, 110 insertions(+), 3 deletions(-) diff --git a/api/filters/nameref/nameref.go b/api/filters/nameref/nameref.go index 7549ab7001..44c0590415 100644 --- a/api/filters/nameref/nameref.go +++ b/api/filters/nameref/nameref.go @@ -402,8 +402,13 @@ func (f Filter) confirmNodeMatchesReferrer(node *yaml.RNode) error { return err } if err = checkEqual( - "Name", meta.Name, f.Referrer.GetName()); err != nil { - return err + "generateName", + meta.GenerateName, + f.Referrer.GetGenerateName()); err != nil { + if err = checkEqual( + "Name", meta.Name, f.Referrer.GetName()); err != nil { + return err + } } if err = checkEqual( "Namespace", meta.Namespace, f.Referrer.GetNamespace()); err != nil { diff --git a/api/krusty/resourceconflict_test.go b/api/krusty/resourceconflict_test.go index 713e21feb4..5bb2d3f797 100644 --- a/api/krusty/resourceconflict_test.go +++ b/api/krusty/resourceconflict_test.go @@ -13,12 +13,28 @@ import ( func writeBase(th kusttest_test.Harness) { th.WriteK("base", ` resources: +- job.yaml - serviceaccount.yaml - rolebinding.yaml - clusterrolebinding.yaml - clusterrole.yaml namePrefix: pfx- nameSuffix: -sfx +`) + th.WriteF("base/job.yaml", ` +apiVersion: batch/v1 +kind: Job +metadata: + generateName: job- +spec: + template: + spec: + containers: + - name: job + image: run/job:1.0 + command: + - echo + - done `) th.WriteF("base/serviceaccount.yaml", ` apiVersion: v1 @@ -94,6 +110,20 @@ func TestBase(t *testing.T) { writeBase(th) m := th.Run("base", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` +apiVersion: batch/v1 +kind: Job +metadata: + generateName: job- +spec: + template: + spec: + containers: + - command: + - echo + - done + image: run/job:1.0 + name: job +--- apiVersion: v1 kind: ServiceAccount metadata: @@ -145,6 +175,20 @@ func TestMidLevelA(t *testing.T) { writeMidOverlays(th) m := th.Run("overlays/a", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` +apiVersion: batch/v1 +kind: Job +metadata: + generateName: job- +spec: + template: + spec: + containers: + - command: + - echo + - done + image: run/job:1.0 + name: job +--- apiVersion: v1 kind: ServiceAccount metadata: @@ -196,6 +240,20 @@ func TestMidLevelB(t *testing.T) { writeMidOverlays(th) m := th.Run("overlays/b", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` +apiVersion: batch/v1 +kind: Job +metadata: + generateName: job- +spec: + template: + spec: + containers: + - command: + - echo + - done + image: run/job:1.0 + name: job +--- apiVersion: v1 kind: ServiceAccount metadata: @@ -248,6 +306,20 @@ func TestMultibasesNoConflict(t *testing.T) { writeTopOverlay(th) m := th.Run("combined", th.MakeDefaultOptions()) th.AssertActualEqualsExpected(m, ` +apiVersion: batch/v1 +kind: Job +metadata: + generateName: job- +spec: + template: + spec: + containers: + - command: + - echo + - done + image: run/job:1.0 + name: job +--- apiVersion: v1 kind: ServiceAccount metadata: @@ -291,6 +363,20 @@ rules: - watch - list --- +apiVersion: batch/v1 +kind: Job +metadata: + generateName: job- +spec: + template: + spec: + containers: + - command: + - echo + - done + image: run/job:1.0 + name: job +--- apiVersion: v1 kind: ServiceAccount metadata: diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index b39c8037b4..a17ea2c79d 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -4,6 +4,7 @@ package yaml import ( + "crypto/sha256" "encoding/json" "fmt" "log" @@ -432,10 +433,25 @@ func (rn *RNode) getMapFieldValue(field string) *yaml.Node { return nil } +// GetGenerateName returns the name, or empty string if +// field not found. The setter is more restrictive. +func (rn *RNode) GetGenerateName() string { + return rn.getMetaStringField(GenerateNameField) +} + // GetName returns the name, or empty string if // field not found. The setter is more restrictive. func (rn *RNode) GetName() string { - return rn.getMetaStringField(NameField) + name := rn.getMetaStringField(NameField) + if name == "" { + data := rn.MustString() + suffix := fmt.Sprintf("%x", sha256.Sum256([]byte(data))) + + name = fmt.Sprintf("%s[%s]", rn.GetGenerateName(), suffix[:10]) + + } + + return name } // getMetaStringField returns the value of a string field in metadata.