From 85d53445ceca86e914236a7c1042229994d14dfa Mon Sep 17 00:00:00 2001 From: Lee Bernick Date: Wed, 20 Jul 2022 12:47:31 -0400 Subject: [PATCH] Move step and sidecar replacements to new package This commit moves logic related to variable replacement in Steps and Sidecars out of pkg/apis into its own package. This will prevent this code from being duplicated in the v1 api package. No functional changes. --- .../v1beta1 => container}/container_replacements.go | 7 ++++--- .../v1beta1 => container}/sidecar_replacements.go | 5 +++-- .../sidecar_replacements_test.go | 5 +++-- .../v1beta1 => container}/step_replacements.go | 7 ++++--- .../v1beta1 => container}/step_replacements_test.go | 5 +++-- pkg/reconciler/taskrun/resources/apply.go | 11 ++++++----- 6 files changed, 23 insertions(+), 17 deletions(-) rename pkg/{apis/pipeline/v1beta1 => container}/container_replacements.go (91%) rename pkg/{apis/pipeline/v1beta1 => container}/sidecar_replacements.go (80%) rename pkg/{apis/pipeline/v1beta1 => container}/sidecar_replacements_test.go (95%) rename pkg/{apis/pipeline/v1beta1 => container}/step_replacements.go (79%) rename pkg/{apis/pipeline/v1beta1 => container}/step_replacements_test.go (96%) diff --git a/pkg/apis/pipeline/v1beta1/container_replacements.go b/pkg/container/container_replacements.go similarity index 91% rename from pkg/apis/pipeline/v1beta1/container_replacements.go rename to pkg/container/container_replacements.go index 5274507f772..64d92d2485a 100644 --- a/pkg/apis/pipeline/v1beta1/container_replacements.go +++ b/pkg/container/container_replacements.go @@ -14,22 +14,23 @@ limitations under the License. */ -package v1beta1 +package container import ( + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/substitution" corev1 "k8s.io/api/core/v1" ) // applyStepReplacements returns a StepContainer with variable interpolation applied. -func applyStepReplacements(step *Step, stringReplacements map[string]string, arrayReplacements map[string][]string) { +func applyStepReplacements(step *v1beta1.Step, stringReplacements map[string]string, arrayReplacements map[string][]string) { c := step.ToK8sContainer() applyContainerReplacements(c, stringReplacements, arrayReplacements) step.SetContainerFields(*c) } // applySidecarReplacements returns a SidecarContainer with variable interpolation applied. -func applySidecarReplacements(sidecar *Sidecar, stringReplacements map[string]string, arrayReplacements map[string][]string) { +func applySidecarReplacements(sidecar *v1beta1.Sidecar, stringReplacements map[string]string, arrayReplacements map[string][]string) { c := sidecar.ToK8sContainer() applyContainerReplacements(c, stringReplacements, arrayReplacements) sidecar.SetContainerFields(*c) diff --git a/pkg/apis/pipeline/v1beta1/sidecar_replacements.go b/pkg/container/sidecar_replacements.go similarity index 80% rename from pkg/apis/pipeline/v1beta1/sidecar_replacements.go rename to pkg/container/sidecar_replacements.go index a331936cd4b..908677a11f4 100644 --- a/pkg/apis/pipeline/v1beta1/sidecar_replacements.go +++ b/pkg/container/sidecar_replacements.go @@ -14,14 +14,15 @@ limitations under the License. */ -package v1beta1 +package container import ( + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "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) { +func ApplySidecarReplacements(sidecar *v1beta1.Sidecar, stringReplacements map[string]string, arrayReplacements map[string][]string) { sidecar.Script = substitution.ApplyReplacements(sidecar.Script, stringReplacements) applySidecarReplacements(sidecar, stringReplacements, arrayReplacements) } diff --git a/pkg/apis/pipeline/v1beta1/sidecar_replacements_test.go b/pkg/container/sidecar_replacements_test.go similarity index 95% rename from pkg/apis/pipeline/v1beta1/sidecar_replacements_test.go rename to pkg/container/sidecar_replacements_test.go index e2ce2adf861..e93bdcc955f 100644 --- a/pkg/apis/pipeline/v1beta1/sidecar_replacements_test.go +++ b/pkg/container/sidecar_replacements_test.go @@ -14,13 +14,14 @@ limitations under the License. */ -package v1beta1_test +package container_test import ( "testing" "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/container" corev1 "k8s.io/api/core/v1" ) @@ -120,7 +121,7 @@ func TestApplySidecarReplacements(t *testing.T) { SubPath: "replaced!", }}, } - v1beta1.ApplySidecarReplacements(&s, replacements, arrayReplacements) + container.ApplySidecarReplacements(&s, replacements, arrayReplacements) if d := cmp.Diff(s, expected); d != "" { t.Errorf("Container replacements failed: %s", d) } diff --git a/pkg/apis/pipeline/v1beta1/step_replacements.go b/pkg/container/step_replacements.go similarity index 79% rename from pkg/apis/pipeline/v1beta1/step_replacements.go rename to pkg/container/step_replacements.go index 12f4e76eef0..31265a17e16 100644 --- a/pkg/apis/pipeline/v1beta1/step_replacements.go +++ b/pkg/container/step_replacements.go @@ -14,14 +14,15 @@ limitations under the License. */ -package v1beta1 +package container import ( + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "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) { +func ApplyStepReplacements(step *v1beta1.Step, stringReplacements map[string]string, arrayReplacements map[string][]string) { step.Script = substitution.ApplyReplacements(step.Script, stringReplacements) if step.StdoutConfig != nil { step.StdoutConfig.Path = substitution.ApplyReplacements(step.StdoutConfig.Path, stringReplacements) @@ -33,7 +34,7 @@ func ApplyStepReplacements(step *Step, stringReplacements map[string]string, arr } // ApplyStepTemplateReplacements applies variable interpolation on a StepTemplate (aka a container) -func ApplyStepTemplateReplacements(stepTemplate *StepTemplate, stringReplacements map[string]string, arrayReplacements map[string][]string) { +func ApplyStepTemplateReplacements(stepTemplate *v1beta1.StepTemplate, stringReplacements map[string]string, arrayReplacements map[string][]string) { container := stepTemplate.ToK8sContainer() applyContainerReplacements(container, stringReplacements, arrayReplacements) stepTemplate.SetContainerFields(*container) diff --git a/pkg/apis/pipeline/v1beta1/step_replacements_test.go b/pkg/container/step_replacements_test.go similarity index 96% rename from pkg/apis/pipeline/v1beta1/step_replacements_test.go rename to pkg/container/step_replacements_test.go index 55a166f42ca..52a7420776c 100644 --- a/pkg/apis/pipeline/v1beta1/step_replacements_test.go +++ b/pkg/container/step_replacements_test.go @@ -14,13 +14,14 @@ limitations under the License. */ -package v1beta1_test +package container_test import ( "testing" "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/container" corev1 "k8s.io/api/core/v1" ) @@ -133,7 +134,7 @@ func TestApplyStepReplacements(t *testing.T) { Path: "/workspace/data/stderr.txt", }, } - v1beta1.ApplyStepReplacements(&s, replacements, arrayReplacements) + container.ApplyStepReplacements(&s, replacements, arrayReplacements) if d := cmp.Diff(s, expected); d != "" { t.Errorf("Container replacements failed: %s", d) } diff --git a/pkg/reconciler/taskrun/resources/apply.go b/pkg/reconciler/taskrun/resources/apply.go index ab6c5bb4511..bb5995b32b0 100644 --- a/pkg/reconciler/taskrun/resources/apply.go +++ b/pkg/reconciler/taskrun/resources/apply.go @@ -28,6 +28,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/container" "github.com/tektoncd/pipeline/pkg/pod" "github.com/tektoncd/pipeline/pkg/substitution" ) @@ -202,7 +203,7 @@ func applyWorkspaceMountPath(variable string, spec *v1beta1.TaskSpec, declaratio for _, usage := range step.Workspaces { if usage.Name == declaration.Name && usage.MountPath != "" { stringReplacements[variable] = usage.MountPath - v1beta1.ApplyStepReplacements(step, stringReplacements, emptyArrayReplacements) + container.ApplyStepReplacements(step, stringReplacements, emptyArrayReplacements) } } } @@ -212,7 +213,7 @@ func applyWorkspaceMountPath(variable string, spec *v1beta1.TaskSpec, declaratio for _, usage := range sidecar.Workspaces { if usage.Name == declaration.Name && usage.MountPath != "" { stringReplacements[variable] = usage.MountPath - v1beta1.ApplySidecarReplacements(sidecar, stringReplacements, emptyArrayReplacements) + container.ApplySidecarReplacements(sidecar, stringReplacements, emptyArrayReplacements) } } } @@ -269,12 +270,12 @@ func ApplyReplacements(spec *v1beta1.TaskSpec, stringReplacements map[string]str // Apply variable expansion to steps fields. steps := spec.Steps for i := range steps { - v1beta1.ApplyStepReplacements(&steps[i], stringReplacements, arrayReplacements) + container.ApplyStepReplacements(&steps[i], stringReplacements, arrayReplacements) } // Apply variable expansion to stepTemplate fields. if spec.StepTemplate != nil { - v1beta1.ApplyStepTemplateReplacements(spec.StepTemplate, stringReplacements, arrayReplacements) + container.ApplyStepTemplateReplacements(spec.StepTemplate, stringReplacements, arrayReplacements) } // Apply variable expansion to the build's volumes @@ -329,7 +330,7 @@ func ApplyReplacements(spec *v1beta1.TaskSpec, stringReplacements map[string]str // Apply variable substitution to the sidecar definitions sidecars := spec.Sidecars for i := range sidecars { - v1beta1.ApplySidecarReplacements(&sidecars[i], stringReplacements, arrayReplacements) + container.ApplySidecarReplacements(&sidecars[i], stringReplacements, arrayReplacements) } return spec