From 8391daefd352ca80413eaf0ec4e31ee2f9bdaa0b Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 1 Dec 2020 17:51:50 +0100 Subject: [PATCH] pkg/apis: unexport ApplyContainerReplacements It is only used internally. This also removes the `container_replacements_test.go` as the code is/should already be covered by `sidecar_replacements_test.go` and `step_replacements_test.go`. Signed-off-by: Vincent Demeester --- .../v1beta1/container_replacements.go | 3 +- .../v1beta1/container_replacements_test.go | 149 ------------------ .../pipeline/v1beta1/sidecar_replacements.go | 3 +- .../pipeline/v1beta1/step_replacements.go | 3 +- 4 files changed, 6 insertions(+), 152 deletions(-) delete mode 100644 pkg/apis/pipeline/v1beta1/container_replacements_test.go diff --git a/pkg/apis/pipeline/v1beta1/container_replacements.go b/pkg/apis/pipeline/v1beta1/container_replacements.go index cd0da192478..30af4e11e8c 100644 --- a/pkg/apis/pipeline/v1beta1/container_replacements.go +++ b/pkg/apis/pipeline/v1beta1/container_replacements.go @@ -21,7 +21,8 @@ import ( corev1 "k8s.io/api/core/v1" ) -func ApplyContainerReplacements(step *corev1.Container, stringReplacements map[string]string, arrayReplacements map[string][]string) { +// applyContainerReplacements applies variable interpolation on a Container (subset of a Step). +func applyContainerReplacements(step *corev1.Container, stringReplacements map[string]string, arrayReplacements map[string][]string) { step.Name = substitution.ApplyReplacements(step.Name, stringReplacements) step.Image = substitution.ApplyReplacements(step.Image, stringReplacements) step.ImagePullPolicy = corev1.PullPolicy(substitution.ApplyReplacements(string(step.ImagePullPolicy), stringReplacements)) diff --git a/pkg/apis/pipeline/v1beta1/container_replacements_test.go b/pkg/apis/pipeline/v1beta1/container_replacements_test.go deleted file mode 100644 index a8bf535465c..00000000000 --- a/pkg/apis/pipeline/v1beta1/container_replacements_test.go +++ /dev/null @@ -1,149 +0,0 @@ -/* - Copyright 2019 The Tekton Authors - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package v1beta1_test - -import ( - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - corev1 "k8s.io/api/core/v1" -) - -func TestApplyContainerReplacements(t *testing.T) { - replacements := map[string]string{ - "replace.me": "replaced!", - } - - arrayReplacements := map[string][]string{ - "array.replace.me": {"val1", "val2"}, - } - - s := corev1.Container{ - Name: "$(replace.me)", - Image: "$(replace.me)", - ImagePullPolicy: "$(replace.me)", - Command: []string{"$(array.replace.me)"}, - Args: []string{"$(array.replace.me)"}, - WorkingDir: "$(replace.me)", - EnvFrom: []corev1.EnvFromSource{{ - ConfigMapRef: &corev1.ConfigMapEnvSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "$(replace.me)", - }, - }, - SecretRef: &corev1.SecretEnvSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "$(replace.me)", - }, - }, - }}, - Env: []corev1.EnvVar{{ - Name: "not_me", - Value: "$(replace.me)", - ValueFrom: &corev1.EnvVarSource{ - ConfigMapKeyRef: &corev1.ConfigMapKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "$(replace.me)", - }, - Key: "$(replace.me)", - }, - SecretKeyRef: &corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "$(replace.me)", - }, - Key: "$(replace.me)", - }, - }, - }}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "$(replace.me)", - MountPath: "$(replace.me)", - SubPath: "$(replace.me)", - }}, - } - - expected := corev1.Container{ - Name: "replaced!", - Image: "replaced!", - ImagePullPolicy: "replaced!", - Command: []string{"val1", "val2"}, - Args: []string{"val1", "val2"}, - WorkingDir: "replaced!", - EnvFrom: []corev1.EnvFromSource{{ - ConfigMapRef: &corev1.ConfigMapEnvSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "replaced!", - }, - }, - SecretRef: &corev1.SecretEnvSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "replaced!", - }, - }, - }}, - Env: []corev1.EnvVar{{ - Name: "not_me", - Value: "replaced!", - ValueFrom: &corev1.EnvVarSource{ - ConfigMapKeyRef: &corev1.ConfigMapKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "replaced!", - }, - Key: "replaced!", - }, - SecretKeyRef: &corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "replaced!", - }, - Key: "replaced!", - }, - }, - }}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "replaced!", - MountPath: "replaced!", - SubPath: "replaced!", - }}, - } - - v1beta1.ApplyContainerReplacements(&s, replacements, arrayReplacements) - if d := cmp.Diff(s, expected); d != "" { - t.Errorf("Container replacements failed: %s", d) - } -} - -func TestApplyContainerReplacements_NotDefined(t *testing.T) { - s := corev1.Container{ - Name: "$(params.not.defined)", - } - replacements := map[string]string{ - "replace.me": "replaced!", - } - - arrayReplacements := map[string][]string{ - "array.replace.me": {"val1", "val2"}, - } - - expected := corev1.Container{ - Name: "$(params.not.defined)", - } - v1beta1.ApplyContainerReplacements(&s, replacements, arrayReplacements) - if d := cmp.Diff(s, expected); d != "" { - t.Errorf("Unexpected container replacement: %s", d) - } -} diff --git a/pkg/apis/pipeline/v1beta1/sidecar_replacements.go b/pkg/apis/pipeline/v1beta1/sidecar_replacements.go index e4951d57b16..bf41f463383 100644 --- a/pkg/apis/pipeline/v1beta1/sidecar_replacements.go +++ b/pkg/apis/pipeline/v1beta1/sidecar_replacements.go @@ -20,7 +20,8 @@ import ( "github.com/tektoncd/pipeline/pkg/substitution" ) +// ApplySidecarReplacements applies variable interpolation on a Sidecar. func ApplySidecarReplacements(sidecar *Sidecar, stringReplacements map[string]string, arrayReplacements map[string][]string) { sidecar.Script = substitution.ApplyReplacements(sidecar.Script, stringReplacements) - ApplyContainerReplacements(&sidecar.Container, stringReplacements, arrayReplacements) + applyContainerReplacements(&sidecar.Container, stringReplacements, arrayReplacements) } diff --git a/pkg/apis/pipeline/v1beta1/step_replacements.go b/pkg/apis/pipeline/v1beta1/step_replacements.go index 188035bc6a9..66ef5e3a7f6 100644 --- a/pkg/apis/pipeline/v1beta1/step_replacements.go +++ b/pkg/apis/pipeline/v1beta1/step_replacements.go @@ -20,7 +20,8 @@ import ( "github.com/tektoncd/pipeline/pkg/substitution" ) +// ApplyStepReplacements applies variable interpolation on a Step. func ApplyStepReplacements(step *Step, stringReplacements map[string]string, arrayReplacements map[string][]string) { step.Script = substitution.ApplyReplacements(step.Script, stringReplacements) - ApplyContainerReplacements(&step.Container, stringReplacements, arrayReplacements) + applyContainerReplacements(&step.Container, stringReplacements, arrayReplacements) }