From f0b4cc45810838602b6843159092112180d53d51 Mon Sep 17 00:00:00 2001 From: Katrina Verey Date: Tue, 10 Aug 2021 16:08:28 -0700 Subject: [PATCH] ContainerPatch supports all common workload paths --- kyaml/fn/framework/patch.go | 3 +- kyaml/fn/framework/processors_test.go | 52 +++++++++++++++++++ .../container-sources/cron_job.template.yaml | 19 +++++++ .../daemon_set.template.yaml | 23 ++++++++ .../deployment.template.yaml | 22 ++++++++ .../container-sources/job.template.yaml | 16 ++++++ .../container-sources/pod.template.yaml | 14 +++++ .../pod_template.template.yaml | 17 ++++++ .../replica_set.template.yaml | 22 ++++++++ .../replication_controller.template.yaml | 22 ++++++++ .../stateful_set.template.yaml | 24 +++++++++ 11 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/cron_job.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/daemon_set.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/deployment.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/job.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod_template.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/replica_set.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/replication_controller.template.yaml create mode 100644 kyaml/fn/framework/testdata/template-processor/templates/container-sources/stateful_set.template.yaml diff --git a/kyaml/fn/framework/patch.go b/kyaml/fn/framework/patch.go index 5b0b3b098..157080d1a 100644 --- a/kyaml/fn/framework/patch.go +++ b/kyaml/fn/framework/patch.go @@ -168,8 +168,7 @@ func (cpt ContainerPatchTemplate) apply(matches []*yaml.RNode) error { } for i := range matches { - // TODO(knverey): Make this work for more Kinds and expose the helper for doing so. - containers, err := matches[i].Pipe(yaml.Lookup("spec", "template", "spec", "containers")) + containers, err := matches[i].Pipe(yaml.LookupFirstMatch(yaml.ConventionalContainerPaths)) if err != nil { return errors.Wrap(err) } diff --git a/kyaml/fn/framework/processors_test.go b/kyaml/fn/framework/processors_test.go index 554a15e47..0b3e9f9d3 100644 --- a/kyaml/fn/framework/processors_test.go +++ b/kyaml/fn/framework/processors_test.go @@ -5,6 +5,7 @@ package framework_test import ( "bytes" + "regexp" "strings" "testing" @@ -276,6 +277,57 @@ functionConfig: `), strings.TrimSpace(out.String())) } +func TestTemplateProcessor_ContainerPatchTemplates_MultipleWorkloadKinds(t *testing.T) { + type API struct { + Spec struct { + Key string `json:"key" yaml:"key"` + Value string `json:"value" yaml:"value"` + A string `json:"a" yaml:"a"` + } + } + config := &API{} + p := framework.TemplateProcessor{ + TemplateData: config, + ResourceTemplates: []framework.ResourceTemplate{{ + Templates: parser.TemplateFiles("testdata/template-processor/templates/container-sources"), + }}, + PatchTemplates: []framework.PatchTemplate{ + &framework.ContainerPatchTemplate{ + Templates: parser.TemplateFiles("testdata/template-processor/container-patches"), + }, + }, + } + + out := new(bytes.Buffer) + rw := &kio.ByteReadWriter{Writer: out, Reader: bytes.NewBufferString(` +apiVersion: config.kubernetes.io/v1alpha1 +kind: ResourceList +items: [] +functionConfig: + spec: + key: Hello + value: World + a: bar +`)} + + require.NoError(t, framework.Execute(p, rw)) + resources, err := (&kio.ByteReader{Reader: out}).Read() + require.NoError(t, err) + envRegex := regexp.MustCompile(strings.TrimSpace(` +\s+ env: +\s+ - name: EXISTING +\s+ value: variable +\s+ - name: Hello +\s+ value: World +`)) + require.Equal(t, 9, len(resources)) + for i, r := range resources { + t.Run(r.GetKind(), func(t *testing.T) { + assert.Regexp(t, envRegex, resources[i].MustString()) + }) + } +} + func TestSimpleProcessor_Process_loads_config(t *testing.T) { cfg := new(struct { Value string `yaml:"value"` diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/cron_job.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/cron_job.template.yaml new file mode 100644 index 000000000..d6a47caed --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/cron_job.template.yaml @@ -0,0 +1,19 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: batch/v1 +kind: CronJob +metadata: + name: hello +spec: + schedule: "*/1 * * * *" + jobTemplate: + spec: + template: + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/daemon_set.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/daemon_set.template.yaml new file mode 100644 index 000000000..1ef61dc2a --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/daemon_set.template.yaml @@ -0,0 +1,23 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: hello +spec: + selector: + matchLabels: + name: hello + template: + metadata: + labels: + name: hello + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable + diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/deployment.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/deployment.template.yaml new file mode 100644 index 000000000..a9e8882a6 --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/deployment.template.yaml @@ -0,0 +1,22 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hello +spec: + selector: + matchLabels: + app: hello + template: + metadata: + labels: + app: hello + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/job.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/job.template.yaml new file mode 100644 index 000000000..8cbd7e46f --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/job.template.yaml @@ -0,0 +1,16 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: batch/v1 +kind: Job +metadata: + name: hello +spec: + template: + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod.template.yaml new file mode 100644 index 000000000..0ff1a656e --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod.template.yaml @@ -0,0 +1,14 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: v1 +kind: Pod +metadata: + name: hello +spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod_template.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod_template.template.yaml new file mode 100644 index 000000000..a3069a080 --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/pod_template.template.yaml @@ -0,0 +1,17 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: v1 +kind: PodTemplate +metadata: + name: hello + labels: + tier: hello +template: + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/replica_set.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/replica_set.template.yaml new file mode 100644 index 000000000..d0c0f9b72 --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/replica_set.template.yaml @@ -0,0 +1,22 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: apps/v1 +kind: ReplicaSet +metadata: + name: hello +spec: + selector: + matchLabels: + app: hello + template: + metadata: + labels: + app: hello + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/replication_controller.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/replication_controller.template.yaml new file mode 100644 index 000000000..3c8e386c8 --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/replication_controller.template.yaml @@ -0,0 +1,22 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: v1 +kind: ReplicationController +metadata: + name: hello +spec: + selector: + app: hello + template: + metadata: + name: hello + labels: + app: hello + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable diff --git a/kyaml/fn/framework/testdata/template-processor/templates/container-sources/stateful_set.template.yaml b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/stateful_set.template.yaml new file mode 100644 index 000000000..1aa312fce --- /dev/null +++ b/kyaml/fn/framework/testdata/template-processor/templates/container-sources/stateful_set.template.yaml @@ -0,0 +1,24 @@ +# Copyright 2021 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hello +spec: + serviceName: "nginx" + selector: + matchLabels: + app: hello + template: + metadata: + labels: + app: hello + spec: + containers: + - name: hello + image: nginx + env: + - name: EXISTING + value: variable +