-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable sidecars to partake in substitution
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
Showing
8 changed files
with
298 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
147
pkg/apis/pipeline/v1alpha1/container_replacements_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.