-
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.
Add context variables for PipelineRun and TaskRun UIDs
A user may want to tag an oci image with the TaskRun or PipelineRun UIDs. Currently, they can't do that because `metadata.uid` for TaskRuns and PipelineRuns are not exposed. In this PR, we add the UID context variable for TaskRuns and PipelineRun. Users can now use `$(context.taskRun.uid)` and `$(context.pipelineRun.uid)` to access and use UIDs. In addition, we add validation for all context variables that are supported so far -- `context.task.name`, `context.taskRun.name`, `context.taskRun.namespace`, `context.taskRun.uid`, `context.pipeline.name`, `context.pipelineRun.name`, `context.pipelineRun.namespace`, `context.pipelineRun.uid`. Partially fixes #2958
- Loading branch information
Showing
16 changed files
with
618 additions
and
13 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
35 changes: 35 additions & 0 deletions
35
examples/v1beta1/pipelineruns/using_context_variables.yaml
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,35 @@ | ||
kind: PipelineRun | ||
apiVersion: tekton.dev/v1beta1 | ||
metadata: | ||
generateName: test-pipelinerun- | ||
spec: | ||
pipelineSpec: | ||
tasks: | ||
- name: task1 | ||
params: | ||
- name: pipeline-uid | ||
value: "$(context.pipelineRun.uid)" | ||
- name: pipeline-name | ||
value: "$(context.pipeline.name)" | ||
- name: pipelineRun-name | ||
value: "$(context.pipelineRun.name)" | ||
- name: pipelineRun-name-uid | ||
value: ["$(context.pipelineRun.name)", "$(context.pipelineRun.uid)"] | ||
taskSpec: | ||
params: | ||
- name: pipeline-uid | ||
- name: pipeline-name | ||
- name: pipelineRun-name | ||
steps: | ||
- image: ubuntu | ||
name: print-uid | ||
script: | | ||
echo "TaskRun UID: $(context.taskRun.uid)" | ||
echo "PipelineRun UID from params: $(params.pipeline-uid)" | ||
- image: ubuntu | ||
name: print-names | ||
script: | | ||
echo "Task name: $(context.task.name)" | ||
echo "TaskRun name: $(context.taskRun.name)" | ||
echo "Pipeline name from params: $(params.pipeline-name)" | ||
echo "PipelineRun name from params: $(params.pipelineRun-name)" |
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,16 @@ | ||
kind: TaskRun | ||
apiVersion: tekton.dev/v1beta1 | ||
metadata: | ||
generateName: test-taskrun- | ||
spec: | ||
taskSpec: | ||
steps: | ||
- image: ubuntu | ||
name: print-uid | ||
script: | | ||
echo "TaskRunUID name: $(context.taskRun.uid)" | ||
- image: ubuntu | ||
name: print-names | ||
script: | | ||
echo "Task name: $(context.task.name)" | ||
echo "TaskRun name: $(context.taskRun.name)" |
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
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,87 @@ | ||
/* | ||
Copyright 2020 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 resources | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
"github.com/tektoncd/pipeline/pkg/substitution" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
// ValidateContextVariables validate that context variables used in Pipelines and PipelineRuns are valid | ||
func ValidateContextVariables(ps *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) *apis.FieldError { | ||
var paramValues []string | ||
|
||
for _, param := range(pr.Spec.Params) { | ||
newParamValues := getParameterValues(param) | ||
paramValues = append(paramValues, newParamValues...) | ||
} | ||
|
||
if err := validatePipelineRunContextVariables(paramValues); err != nil { | ||
return err | ||
} | ||
if err := validatePipelineContextVariables(¶mValues); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
|
||
func validatePipelineRunContextVariables(paramValues []string) *apis.FieldError { | ||
pipelineRunContextNames := sets.NewString().Insert( | ||
"name", | ||
"namespace", | ||
"uid", | ||
) | ||
|
||
for _, paramValue := range(paramValues) { | ||
if err := substitution.ValidateVariable(fmt.Sprintf("param[%s]", paramValue), paramValue, "context\\.pipelineRun", "params", "pipelinespec.params", pipelineRunContextNames); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validatePipelineContextVariables(paramValues *[]string) *apis.FieldError { | ||
pipelineContextNames := sets.NewString().Insert( | ||
"name", | ||
) | ||
|
||
for _, paramValue := range(*paramValues) { | ||
if err := substitution.ValidateVariable(fmt.Sprintf("param[%s]", paramValue), paramValue, "context\\.pipeline", "params", "pipelinespec.params", pipelineContextNames); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getParameterValues(param v1beta1.Param) []string{ | ||
var paramValues []string | ||
|
||
if param.Value.Type == v1beta1.ParamTypeString { | ||
paramValues = append(paramValues, param.Value.StringVal) | ||
} else { | ||
paramValues = append(paramValues, param.Value.ArrayVal...) | ||
} | ||
|
||
return paramValues | ||
} |
110 changes: 110 additions & 0 deletions
110
pkg/reconciler/pipelinerun/resources/validate_contexts_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,110 @@ | ||
/* | ||
Copyright 2020 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 resources | ||
|
||
import ( | ||
"testing" | ||
|
||
tb "github.com/tektoncd/pipeline/internal/builder/v1beta1" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
) | ||
|
||
func TestContextValid(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
p *v1beta1.Pipeline | ||
original *v1beta1.PipelineRun | ||
}{{ | ||
name: "param pipeline name context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipeline-name", v1beta1.ParamTypeString))), | ||
original: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipeline-name", "$(context.pipeline.name)"))), | ||
}, { | ||
name: "param pipelinerun name context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipelinerun-name", v1beta1.ParamTypeString))), | ||
original: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipelinerun-name", "$(context.pipelineRun.name)"))), | ||
}, { | ||
name: "param pipelinerun uid context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipelinerun-uid", v1beta1.ParamTypeString))), | ||
original: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipelinerun-uid", "$(context.pipelineRun.uid)"))), | ||
}, { | ||
name: "param pipelinerun name, uid context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipelinerun-name-uid", v1beta1.ParamTypeString))), | ||
original: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipelinerun-name-uid", "$(context.pipelineRun.name)", "$(context.pipelineRun.uid)"))), | ||
}} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if err := ValidateContextVariables(&tc.p.Spec, tc.original); err != nil { | ||
t.Errorf("Did not expect to see error when validating valid contexts but saw %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestContextInvalid(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
p *v1beta1.Pipeline | ||
pr *v1beta1.PipelineRun | ||
}{{ | ||
name: "param pipeline name context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipeline-name", v1beta1.ParamTypeString))), | ||
pr: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipeline-name", "$(context.pipeline.missing)"))), | ||
}, { | ||
name: "param pipelinerun name context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipelinerun-name", v1beta1.ParamTypeString))), | ||
pr: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipelinerun-name", "$(context.pipelineRun.missing)"))), | ||
}, { | ||
name: "param pipelinerun uid context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipelinerun-uid", v1beta1.ParamTypeString))), | ||
pr: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipelinerun-uid", "$(context.pipelineRun.missing)"))), | ||
}, { | ||
name: "param pipelinerun name, uid context variable", | ||
p: tb.Pipeline("a-pipeline", tb.PipelineSpec( | ||
tb.PipelineParamSpec("pipelinerun-name-uid", v1beta1.ParamTypeString))), | ||
pr: tb.PipelineRun("a-pipelinerun", tb.PipelineRunSpec( | ||
"test-pipeline", | ||
tb.PipelineRunParam("pipelinerun-name-uid", "$(context.pipelineRun.missing)", "$(context.pipelineRun.missing)"))), | ||
}} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if err := ValidateContextVariables(&tc.p.Spec, tc.pr); err == nil { | ||
t.Errorf("expected to see error when validating invalid contexts") | ||
} | ||
}) | ||
} | ||
} |
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.