-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable sidecars to partake in substitution #1770
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this doesn't need to be exported, except to be tested in container_replacements_test.go.
Can we either test this as an unexported method, or only test the exported surface in apply_test.go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imjasonh it's also used in https://github.com/tektoncd/pipeline/pull/1770/files#diff-768a3668fda546db3140aa35963e1247R25, not sure if that still warrants not exporting (I'm super new to Go, so the import/export semantics elude me a bit right now)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, looks like this is used by the resources package as well and so needs to live on as exported for the time being 👍