From 97b867c9be4c1644fa5d088dc4fc6d251f311ff9 Mon Sep 17 00:00:00 2001 From: davis-haba Date: Fri, 20 Jan 2023 18:01:14 -0800 Subject: [PATCH 1/4] generate mock name for expanded resources Signed-off-by: davis-haba --- pkg/expansion/fixtures/fixtures.go | 5 +++++ pkg/expansion/system.go | 17 +++++++++++++++++ .../output/output.yaml | 1 + .../fixtures/basic-expansion/output/output.yaml | 1 + .../fixtures/expand-cr/output/output.yaml | 2 ++ .../fixtures/expand-with-ns/output/output.yaml | 1 + 6 files changed, 27 insertions(+) diff --git a/pkg/expansion/fixtures/fixtures.go b/pkg/expansion/fixtures/fixtures.go index 3d88b5d58d9..77179c44e6e 100644 --- a/pkg/expansion/fixtures/fixtures.go +++ b/pkg/expansion/fixtures/fixtures.go @@ -91,6 +91,7 @@ kind: Pod metadata: labels: app: nginx + name: mock-pod-nginx-deployment namespace: default spec: containers: @@ -108,6 +109,7 @@ kind: Pod metadata: labels: app: nginx + name: mock-pod-nginx-deployment namespace: default spec: containers: @@ -145,6 +147,7 @@ metadata: app: nginx annotations: owner: admin + name: mock-pod-nginx-deployment namespace: default spec: containers: @@ -396,6 +399,7 @@ metadata: sound: meow labels: fluffy: extremely + name: mock-kitten-big-chungus namespace: default spec: breed: calico @@ -409,6 +413,7 @@ kind: Purr metadata: annotations: shouldPet: manytimes + name: mock-purr-big-chungus namespace: default spec: loud: very diff --git a/pkg/expansion/system.go b/pkg/expansion/system.go index 0db07b21668..c0088cbdbae 100644 --- a/pkg/expansion/system.go +++ b/pkg/expansion/system.go @@ -176,10 +176,27 @@ func expandResource(obj *unstructured.Unstructured, ns *corev1.Namespace, templa resource.SetUnstructuredContent(src) resource.SetGroupVersionKind(resultantGVK) resource.SetNamespace(ns.Name) + resource.SetName(mockNameForResource(obj, resultantGVK)) return resource, nil } +// mockNameForResource returns a mock name for a resultant resource created +// from expanding `gen`. The name will be of the form: +// "mock--". For example, a deployment named +// `nginx-deployment` will produce a resultant named `mock-pod-nginx-deployment`. +func mockNameForResource(gen *unstructured.Unstructured, gvk schema.GroupVersionKind) string { + name := "mock" + if gvk.Kind != "" { + name += "-" + gvk.Kind + } + if gen.GetName() != "" { + name += "-" + gen.GetName() + } + + return strings.ToLower(name) +} + func NewSystem(mutationSystem *mutation.System) *System { return &System{ lock: sync.RWMutex{}, diff --git a/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml b/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml index daa0fc09b9f..b09b30a0cd7 100644 --- a/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml +++ b/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml @@ -3,6 +3,7 @@ kind: Pod metadata: labels: app: nginx + name: mock-pod-nginx-deployment spec: containers: - args: diff --git a/test/gator/expand/fixtures/basic-expansion/output/output.yaml b/test/gator/expand/fixtures/basic-expansion/output/output.yaml index 6ed41c65b60..1985ac141d0 100644 --- a/test/gator/expand/fixtures/basic-expansion/output/output.yaml +++ b/test/gator/expand/fixtures/basic-expansion/output/output.yaml @@ -5,6 +5,7 @@ metadata: owner: admin labels: app: nginx + name: mock-pod-nginx-deployment spec: containers: - args: diff --git a/test/gator/expand/fixtures/expand-cr/output/output.yaml b/test/gator/expand/fixtures/expand-cr/output/output.yaml index fefe29f02aa..ca6d87f67d2 100644 --- a/test/gator/expand/fixtures/expand-cr/output/output.yaml +++ b/test/gator/expand/fixtures/expand-cr/output/output.yaml @@ -3,6 +3,7 @@ kind: Purr metadata: annotations: shouldPet: manytimes + name: mock-purr-big-chungus spec: loud: very --- @@ -13,6 +14,7 @@ metadata: sound: meow labels: fluffy: extremely + name: mock-kitten-big-chungus spec: age: 10 breed: calico diff --git a/test/gator/expand/fixtures/expand-with-ns/output/output.yaml b/test/gator/expand/fixtures/expand-with-ns/output/output.yaml index 1a0fc80a699..4c0b023de74 100644 --- a/test/gator/expand/fixtures/expand-with-ns/output/output.yaml +++ b/test/gator/expand/fixtures/expand-with-ns/output/output.yaml @@ -3,6 +3,7 @@ kind: Pod metadata: labels: app: nginx + name: mock-pod-nginx-deployment namespace: my-ns spec: containers: From 32d7d1d84ff9cb05b6612fc905a174ac5b68a279 Mon Sep 17 00:00:00 2001 From: davis-haba Date: Thu, 16 Feb 2023 15:23:24 -0800 Subject: [PATCH 2/4] expanded resource name does not include 'mock' Signed-off-by: davis-haba --- pkg/expansion/fixtures/fixtures.go | 10 +++++----- pkg/expansion/system.go | 11 +++++------ .../output/output.yaml | 2 +- .../fixtures/basic-expansion/output/output.yaml | 2 +- .../expand/fixtures/expand-cr/output/output.yaml | 4 ++-- .../expand/fixtures/expand-with-ns/output/output.yaml | 2 +- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pkg/expansion/fixtures/fixtures.go b/pkg/expansion/fixtures/fixtures.go index 77179c44e6e..8ea1498ab90 100644 --- a/pkg/expansion/fixtures/fixtures.go +++ b/pkg/expansion/fixtures/fixtures.go @@ -91,7 +91,7 @@ kind: Pod metadata: labels: app: nginx - name: mock-pod-nginx-deployment + name: nginx-deployment-pod namespace: default spec: containers: @@ -109,7 +109,7 @@ kind: Pod metadata: labels: app: nginx - name: mock-pod-nginx-deployment + name: nginx-deployment-pod namespace: default spec: containers: @@ -147,7 +147,7 @@ metadata: app: nginx annotations: owner: admin - name: mock-pod-nginx-deployment + name: nginx-deployment-pod namespace: default spec: containers: @@ -399,7 +399,7 @@ metadata: sound: meow labels: fluffy: extremely - name: mock-kitten-big-chungus + name: big-chungus-kitten namespace: default spec: breed: calico @@ -413,7 +413,7 @@ kind: Purr metadata: annotations: shouldPet: manytimes - name: mock-purr-big-chungus + name: big-chungus-purr namespace: default spec: loud: very diff --git a/pkg/expansion/system.go b/pkg/expansion/system.go index c0088cbdbae..3fdd0594141 100644 --- a/pkg/expansion/system.go +++ b/pkg/expansion/system.go @@ -183,15 +183,14 @@ func expandResource(obj *unstructured.Unstructured, ns *corev1.Namespace, templa // mockNameForResource returns a mock name for a resultant resource created // from expanding `gen`. The name will be of the form: -// "mock--". For example, a deployment named -// `nginx-deployment` will produce a resultant named `mock-pod-nginx-deployment`. +// "-". For example, a deployment named +// `nginx-deployment` will produce a resultant named `nginx-deployment-pod`. func mockNameForResource(gen *unstructured.Unstructured, gvk schema.GroupVersionKind) string { - name := "mock" + name := gen.GetName() if gvk.Kind != "" { name += "-" + gvk.Kind - } - if gen.GetName() != "" { - name += "-" + gen.GetName() + } else { + name += gvk.Kind } return strings.ToLower(name) diff --git a/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml b/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml index b09b30a0cd7..e2151b5fe47 100644 --- a/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml +++ b/test/gator/expand/fixtures/basic-expansion-nonmatching-configs/output/output.yaml @@ -3,7 +3,7 @@ kind: Pod metadata: labels: app: nginx - name: mock-pod-nginx-deployment + name: nginx-deployment-pod spec: containers: - args: diff --git a/test/gator/expand/fixtures/basic-expansion/output/output.yaml b/test/gator/expand/fixtures/basic-expansion/output/output.yaml index 1985ac141d0..58941d6ba97 100644 --- a/test/gator/expand/fixtures/basic-expansion/output/output.yaml +++ b/test/gator/expand/fixtures/basic-expansion/output/output.yaml @@ -5,7 +5,7 @@ metadata: owner: admin labels: app: nginx - name: mock-pod-nginx-deployment + name: nginx-deployment-pod spec: containers: - args: diff --git a/test/gator/expand/fixtures/expand-cr/output/output.yaml b/test/gator/expand/fixtures/expand-cr/output/output.yaml index ca6d87f67d2..aabede74d33 100644 --- a/test/gator/expand/fixtures/expand-cr/output/output.yaml +++ b/test/gator/expand/fixtures/expand-cr/output/output.yaml @@ -3,7 +3,7 @@ kind: Purr metadata: annotations: shouldPet: manytimes - name: mock-purr-big-chungus + name: big-chungus-purr spec: loud: very --- @@ -14,7 +14,7 @@ metadata: sound: meow labels: fluffy: extremely - name: mock-kitten-big-chungus + name: big-chungus-kitten spec: age: 10 breed: calico diff --git a/test/gator/expand/fixtures/expand-with-ns/output/output.yaml b/test/gator/expand/fixtures/expand-with-ns/output/output.yaml index 4c0b023de74..d4cf89ebb9a 100644 --- a/test/gator/expand/fixtures/expand-with-ns/output/output.yaml +++ b/test/gator/expand/fixtures/expand-with-ns/output/output.yaml @@ -3,7 +3,7 @@ kind: Pod metadata: labels: app: nginx - name: mock-pod-nginx-deployment + name: nginx-deployment-pod namespace: my-ns spec: containers: From 71bdec546034bcd064220e425ea05c6ad62e34fc Mon Sep 17 00:00:00 2001 From: davis-haba Date: Tue, 7 Mar 2023 09:27:36 -0800 Subject: [PATCH 3/4] cleanup if statement Signed-off-by: davis-haba --- pkg/expansion/system.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/expansion/system.go b/pkg/expansion/system.go index 3fdd0594141..0844f45c108 100644 --- a/pkg/expansion/system.go +++ b/pkg/expansion/system.go @@ -188,10 +188,9 @@ func expandResource(obj *unstructured.Unstructured, ns *corev1.Namespace, templa func mockNameForResource(gen *unstructured.Unstructured, gvk schema.GroupVersionKind) string { name := gen.GetName() if gvk.Kind != "" { - name += "-" + gvk.Kind - } else { - name += gvk.Kind + name += "-" } + name += gvk.Kind return strings.ToLower(name) } From f4bdf031dcf9697588d3b6f87bc7c3a4bba870f2 Mon Sep 17 00:00:00 2001 From: davis-haba Date: Tue, 7 Mar 2023 15:34:20 -0800 Subject: [PATCH 4/4] fix test Signed-off-by: davis-haba --- pkg/expansion/fixtures/fixtures.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/expansion/fixtures/fixtures.go b/pkg/expansion/fixtures/fixtures.go index 8ea1498ab90..8434259f272 100644 --- a/pkg/expansion/fixtures/fixtures.go +++ b/pkg/expansion/fixtures/fixtures.go @@ -128,6 +128,7 @@ kind: Pod metadata: labels: app: nginx + name: nginx-deployment-pod namespace: default spec: containers: