Skip to content

Commit

Permalink
distinguish individual resources with generateName
Browse files Browse the repository at this point in the history
  • Loading branch information
julian7 committed Oct 28, 2022
1 parent 5f84e6a commit 6544629
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
9 changes: 7 additions & 2 deletions api/filters/nameref/nameref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
86 changes: 86 additions & 0 deletions api/krusty/resourceconflict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 17 additions & 1 deletion kyaml/yaml/rnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package yaml

import (
"crypto/sha256"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6544629

Please sign in to comment.