Skip to content
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

Add namespace variable interpolation for pipelinerun namespace and #2926

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This page documents the variable substitions supported by `Tasks` and `Pipelines
| `params.<param name>` | The value of the parameter at runtime. |
| `tasks.<taskName>.results.<resultName>` | The value of the `Task's` result. Can alter `Task` execution order within a `Pipeline`.) |
| `context.pipelineRun.name` | The name of the `PipelineRun` that this `Pipeline` is running in. |
| `context.pipelineRun.namespace` | The namespace of the `PipelineRun` that this `Pipeline` is running in. |
| `context.pipeline.name` | The name of this `Pipeline` . |


Expand All @@ -31,6 +32,7 @@ This page documents the variable substitions supported by `Tasks` and `Pipelines
| `workspaces.<workspaceName>.volume` | The name of the volume populating the `Workspace`. |
| `credentials.path` | The path to credentials injected from Secrets with matching annotations. |
| `context.taskRun.name` | The name of the `TaskRun` that this `Task` is running in. |
| `context.taskRun.namespace` | The namespace of the `TaskRun` that this `Task` is running in. |
| `context.task.name` | The name of this `Task`. |

### `PipelineResource` variables available in a `Task`
Expand Down
8 changes: 3 additions & 5 deletions pkg/reconciler/pipelinerun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ func ApplyParameters(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *v1beta1.
// ApplyContexts applies the substitution from $(context.(pipelineRun|pipeline).*) with the specified values.
// Currently supports only name substitution. Uses "" as a default if name is not specified.
func ApplyContexts(spec *v1beta1.PipelineSpec, pipelineName string, pr *v1beta1.PipelineRun) *v1beta1.PipelineSpec {
stringReplacements := map[string]string{}
stringReplacements["context.pipelineRun.name"] = pr.Name
stringReplacements["context.pipeline.name"] = pipelineName

return ApplyReplacements(spec,
map[string]string{"context.pipelineRun.name": pr.Name, "context.pipeline.name": pipelineName},
map[string]string{"context.pipelineRun.name": pr.Name,
"context.pipeline.name": pipelineName,
"context.pipelineRun.namespace": pr.Namespace},
map[string][]string{})
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/reconciler/pipelinerun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,19 @@ func TestContext(t *testing.T) {
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "pipelineRunName-1"),
))),
}, {
description: "context pipelineRunNameNamespace replacement with defined pipelineRunNamepsace in spec",
pr: tb.PipelineRun("pipelineRunName", tb.PipelineRunNamespace("prns")),
original: tb.Pipeline("test-pipeline",
tb.PipelineSpec(
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "$(context.pipelineRun.namespace)-1"),
))),
expected: tb.Pipeline("test-pipeline",
tb.PipelineSpec(
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "prns-1"),
))),
}, {
description: "context pipelineRunName replacement with no defined pipeline in spec",
pr: &v1beta1.PipelineRun{},
Expand All @@ -464,6 +477,19 @@ func TestContext(t *testing.T) {
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "-1"),
))),
}, {
description: "context pipelineRunNamespace replacement with no defined pipelineRunNamespace in spec",
pr: tb.PipelineRun("pipelineRunName"),
original: tb.Pipeline("test-pipeline",
tb.PipelineSpec(
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "$(context.pipelineRun.namespace)-1"),
))),
expected: tb.Pipeline("test-pipeline",
tb.PipelineSpec(
tb.PipelineTask("first-task-1", "first-task",
tb.PipelineTaskParam("first-task-first-param", "-1"),
))),
}} {
t.Run(tc.description, func(t *testing.T) {
got := ApplyContexts(&tc.original.Spec, tc.original.Name, tc.pr)
Expand Down
6 changes: 1 addition & 5 deletions pkg/reconciler/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ func ApplyResources(spec *v1beta1.TaskSpec, resolvedResources map[string]v1beta1
// ApplyContexts applies the substitution from $(context.(taskRun|task).*) with the specified values.
// Currently supports only name substitution. Uses "" as a default if name is not specified.
func ApplyContexts(spec *v1beta1.TaskSpec, rtr *ResolvedTaskResources, tr *v1beta1.TaskRun) *v1beta1.TaskSpec {
stringReplacements := map[string]string{}
stringReplacements["context.taskRun.name"] = tr.Name
stringReplacements["context.task.name"] = rtr.TaskName

return ApplyReplacements(spec,
map[string]string{"context.taskRun.name": tr.Name, "context.task.name": rtr.TaskName},
map[string]string{"context.taskRun.name": tr.Name, "context.task.name": rtr.TaskName, "context.taskRun.namespace": tr.Namespace},
map[string][]string{})
}

Expand Down
51 changes: 50 additions & 1 deletion pkg/reconciler/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ func TestContext(t *testing.T) {
}},
},
}, {
description: "context taskRunName replacement with no defined taskRun in spec container",
description: "context taskRunName replacement with no defined taskRun name in spec container",
rtr: resources.ResolvedTaskResources{
TaskName: "Task1",
},
Expand All @@ -860,6 +860,55 @@ func TestContext(t *testing.T) {
},
}},
},
}, {
description: "context taskRun namespace replacement with no defined namepsace in spec container",
rtr: resources.ResolvedTaskResources{
TaskName: "Task1",
},
tr: v1beta1.TaskRun{},
spec: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Container: corev1.Container{
Name: "ImageName",
Image: "$(context.taskRun.namespace)-1",
},
}},
},
want: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Container: corev1.Container{
Name: "ImageName",
Image: "-1",
},
}},
},
}, {
description: "context taskRun namespace replacement with defined namepsace in spec container",
rtr: resources.ResolvedTaskResources{
TaskName: "Task1",
},
tr: v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "taskrunName",
Namespace: "trNamespace",
},
},
spec: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Container: corev1.Container{
Name: "ImageName",
Image: "$(context.taskRun.namespace)-1",
},
}},
},
want: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Container: corev1.Container{
Name: "ImageName",
Image: "trNamespace-1",
},
}},
},
}, {
description: "context taskRunName replacement with no defined taskName in spec container",
rtr: resources.ResolvedTaskResources{},
Expand Down