Skip to content

Commit

Permalink
Enable sidecars to partake in substitution
Browse files Browse the repository at this point in the history
This change enables sidecars to use variable subtitution and leverage
pipeline-specific params to execute whatever actions during a
pipelinerun.

Fixes #1761
  • Loading branch information
MLBMatt committed Jan 10, 2020
1 parent 242bb06 commit 6c8bf0a
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 68 deletions.
27 changes: 27 additions & 0 deletions docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,33 @@ spec:
key: bot-token
```
#### Using a sidecar
```yaml
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: with-sidecar-task
spec:
inputs:
params:
- name: sidecar-image
type: string
description: Image name of the sidecar container
- name: sidecar-env
type: string
description: Environment variable value
sidecars:
- name: sidecar
image: $(inputs.params.sidecar-image)
env:
- name: SIDECAR_ENV
value: $(inputs.params.sidecar-env)
steps:
- name: test
image: hello-world
```
Except as otherwise noted, the content of this page is licensed under the
[Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/),
and code samples are licensed under the
Expand Down
34 changes: 34 additions & 0 deletions examples/taskruns/sidecar-interp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
generateName: sidecar-interp-
spec:
taskSpec:
inputs:
params:
- name: some-input
default: foo
volumes:
- name: shared
emptyDir: {}
sidecars:
- name: value-sidecar
image: ubuntu
command:
- /bin/bash
args:
- -c
- "echo $(inputs.params.some-input) > /shared/value && sleep infinity"
volumeMounts:
- name: shared
mountPath: /shared
steps:
- name: check-value
image: ubuntu
script: |
#!/bin/bash
VALUE=$(cat /shared/value)
[[ $VALUE == 'foo' ]]
volumeMounts:
- name: shared
mountPath: /shared
72 changes: 72 additions & 0 deletions pkg/apis/pipeline/v1alpha1/container_replacements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
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 v1alpha1

import (
"github.com/tektoncd/pipeline/pkg/substitution"
corev1 "k8s.io/api/core/v1"
)

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)

// Use ApplyArrayReplacements here, as additional args may be added via an array parameter.
var newArgs []string
for _, a := range step.Args {
newArgs = append(newArgs, substitution.ApplyArrayReplacements(a, stringReplacements, arrayReplacements)...)
}
step.Args = newArgs

for ie, e := range step.Env {
step.Env[ie].Value = substitution.ApplyReplacements(e.Value, stringReplacements)
if step.Env[ie].ValueFrom != nil {
if e.ValueFrom.SecretKeyRef != nil {
step.Env[ie].ValueFrom.SecretKeyRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.ValueFrom.SecretKeyRef.LocalObjectReference.Name, stringReplacements)
step.Env[ie].ValueFrom.SecretKeyRef.Key = substitution.ApplyReplacements(e.ValueFrom.SecretKeyRef.Key, stringReplacements)
}
if e.ValueFrom.ConfigMapKeyRef != nil {
step.Env[ie].ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name, stringReplacements)
step.Env[ie].ValueFrom.ConfigMapKeyRef.Key = substitution.ApplyReplacements(e.ValueFrom.ConfigMapKeyRef.Key, stringReplacements)
}
}
}

for ie, e := range step.EnvFrom {
step.EnvFrom[ie].Prefix = substitution.ApplyReplacements(e.Prefix, stringReplacements)
if e.ConfigMapRef != nil {
step.EnvFrom[ie].ConfigMapRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.ConfigMapRef.LocalObjectReference.Name, stringReplacements)
}
if e.SecretRef != nil {
step.EnvFrom[ie].SecretRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.SecretRef.LocalObjectReference.Name, stringReplacements)
}
}
step.WorkingDir = substitution.ApplyReplacements(step.WorkingDir, stringReplacements)

// Use ApplyArrayReplacements here, as additional commands may be added via an array parameter.
var newCommand []string
for _, c := range step.Command {
newCommand = append(newCommand, substitution.ApplyArrayReplacements(c, stringReplacements, arrayReplacements)...)
}
step.Command = newCommand

for iv, v := range step.VolumeMounts {
step.VolumeMounts[iv].Name = substitution.ApplyReplacements(v.Name, stringReplacements)
step.VolumeMounts[iv].MountPath = substitution.ApplyReplacements(v.MountPath, stringReplacements)
step.VolumeMounts[iv].SubPath = substitution.ApplyReplacements(v.SubPath, stringReplacements)
}
}
147 changes: 147 additions & 0 deletions pkg/apis/pipeline/v1alpha1/container_replacements_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
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 v1alpha1_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
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)",
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!",
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!",
}},
}

v1alpha1.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)",
}
v1alpha1.ApplyContainerReplacements(&s, replacements, arrayReplacements)
if d := cmp.Diff(s, expected); d != "" {
t.Errorf("Unexpected container replacement: %s", d)
}
}
48 changes: 1 addition & 47 deletions pkg/apis/pipeline/v1alpha1/step_replacements.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,6 @@ import (
)

func ApplyStepReplacements(step *Step, stringReplacements map[string]string, arrayReplacements map[string][]string) {
step.Name = substitution.ApplyReplacements(step.Name, stringReplacements)
step.Image = substitution.ApplyReplacements(step.Image, stringReplacements)
step.Script = substitution.ApplyReplacements(step.Script, stringReplacements)

// Use ApplyArrayReplacements here, as additional args may be added via an array parameter.
var newArgs []string
for _, a := range step.Args {
newArgs = append(newArgs, substitution.ApplyArrayReplacements(a, stringReplacements, arrayReplacements)...)
}
step.Args = newArgs

for ie, e := range step.Env {
step.Env[ie].Value = substitution.ApplyReplacements(e.Value, stringReplacements)
if step.Env[ie].ValueFrom != nil {
if e.ValueFrom.SecretKeyRef != nil {
step.Env[ie].ValueFrom.SecretKeyRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.ValueFrom.SecretKeyRef.LocalObjectReference.Name, stringReplacements)
step.Env[ie].ValueFrom.SecretKeyRef.Key = substitution.ApplyReplacements(e.ValueFrom.SecretKeyRef.Key, stringReplacements)
}
if e.ValueFrom.ConfigMapKeyRef != nil {
step.Env[ie].ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name, stringReplacements)
step.Env[ie].ValueFrom.ConfigMapKeyRef.Key = substitution.ApplyReplacements(e.ValueFrom.ConfigMapKeyRef.Key, stringReplacements)
}
}
}

for ie, e := range step.EnvFrom {
step.EnvFrom[ie].Prefix = substitution.ApplyReplacements(e.Prefix, stringReplacements)
if e.ConfigMapRef != nil {
step.EnvFrom[ie].ConfigMapRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.ConfigMapRef.LocalObjectReference.Name, stringReplacements)
}
if e.SecretRef != nil {
step.EnvFrom[ie].SecretRef.LocalObjectReference.Name = substitution.ApplyReplacements(e.SecretRef.LocalObjectReference.Name, stringReplacements)
}
}
step.WorkingDir = substitution.ApplyReplacements(step.WorkingDir, stringReplacements)

// Use ApplyArrayReplacements here, as additional commands may be added via an array parameter.
var newCommand []string
for _, c := range step.Command {
newCommand = append(newCommand, substitution.ApplyArrayReplacements(c, stringReplacements, arrayReplacements)...)
}
step.Command = newCommand

for iv, v := range step.VolumeMounts {
step.VolumeMounts[iv].Name = substitution.ApplyReplacements(v.Name, stringReplacements)
step.VolumeMounts[iv].MountPath = substitution.ApplyReplacements(v.MountPath, stringReplacements)
step.VolumeMounts[iv].SubPath = substitution.ApplyReplacements(v.SubPath, stringReplacements)
}
ApplyContainerReplacements(&step.Container, stringReplacements, arrayReplacements)
}
21 changes: 0 additions & 21 deletions pkg/apis/pipeline/v1alpha1/step_replacements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,3 @@ func TestApplyStepReplacements(t *testing.T) {
t.Errorf("Container replacements failed: %s", d)
}
}

func TestApplyStepReplacements_NotDefined(t *testing.T) {
s := v1alpha1.Step{Container: corev1.Container{
Name: "$(params.not.defined)",
}}
replacements := map[string]string{
"replace.me": "replaced!",
}

arrayReplacements := map[string][]string{
"array.replace.me": {"val1", "val2"},
}

expected := v1alpha1.Step{Container: corev1.Container{
Name: "$(params.not.defined)",
}}
v1alpha1.ApplyStepReplacements(&s, replacements, arrayReplacements)
if d := cmp.Diff(s, expected); d != "" {
t.Errorf("Unexpected container replacement: %s", d)
}
}
6 changes: 6 additions & 0 deletions pkg/reconciler/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,11 @@ func ApplyReplacements(spec *v1alpha1.TaskSpec, stringReplacements map[string]st
}
}

// Apply variable substitution to the sidecar definitions
sidecars := spec.Sidecars
for i := range sidecars {
v1alpha1.ApplyContainerReplacements(&sidecars[i], stringReplacements, arrayReplacements)
}

return spec
}
Loading

0 comments on commit 6c8bf0a

Please sign in to comment.