Skip to content

Commit

Permalink
TEP-0106: Support Specifying Metadata per Task in Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
austinzhao-go committed May 5, 2022
1 parent a3caabf commit 1f9a5d0
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 24 deletions.
8 changes: 7 additions & 1 deletion pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ type PipelineTaskRunSpec struct {
StepOverrides []TaskRunStepOverride `json:"stepOverrides,omitempty"`
// +listType=atomic
SidecarOverrides []TaskRunSidecarOverride `json:"sidecarOverrides,omitempty"`

// +optional
Metadata PipelineTaskMetadata `json:"metadata,omitempty"`
}

// GetTaskRunSpec returns the task specific spec for a given
Expand All @@ -621,6 +624,7 @@ func (pr *PipelineRun) GetTaskRunSpec(pipelineTaskName string) PipelineTaskRunSp
}
s.StepOverrides = task.StepOverrides
s.SidecarOverrides = task.SidecarOverrides
s.Metadata = task.Metadata
}
}
return s
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,10 @@
"description": "PipelineTaskRunSpec can be used to configure specific specs for a concrete Task",
"type": "object",
"properties": {
"metadata": {
"default": {},
"$ref": "#/definitions/v1beta1.PipelineTaskMetadata"
},
"pipelineTaskName": {
"type": "string"
},
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 23 additions & 23 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,43 +988,43 @@ func getTaskrunLabels(pr *v1beta1.PipelineRun, pipelineTaskName string, includeP
}

func combineTaskRunAndTaskSpecLabels(pr *v1beta1.PipelineRun, pipelineTask *v1beta1.PipelineTask) map[string]string {
var tsLabels map[string]string
trLabels := getTaskrunLabels(pr, pipelineTask.Name, true)
labels := make(map[string]string)

taskRunSpec := pr.GetTaskRunSpec(pipelineTask.Name)
addMetadataByPrecedence(labels, taskRunSpec.Metadata.Labels)

addMetadataByPrecedence(labels, getTaskrunLabels(pr, pipelineTask.Name, true))

if pipelineTask.TaskSpec != nil {
tsLabels = pipelineTask.TaskSpecMetadata().Labels
addMetadataByPrecedence(labels, pipelineTask.TaskSpecMetadata().Labels)
}

// labels from TaskRun takes higher precedence over the ones specified in Pipeline through TaskSpec
// initialize labels with TaskRun labels
labels := trLabels
for key, value := range tsLabels {
// add labels from TaskSpec if the label does not exist
if _, ok := labels[key]; !ok {
labels[key] = value
}
}
return labels
}

func combineTaskRunAndTaskSpecAnnotations(pr *v1beta1.PipelineRun, pipelineTask *v1beta1.PipelineTask) map[string]string {
var tsAnnotations map[string]string
trAnnotations := getTaskrunAnnotations(pr)
annotations := make(map[string]string)

taskRunSpec := pr.GetTaskRunSpec(pipelineTask.Name)
addMetadataByPrecedence(annotations, taskRunSpec.Metadata.Annotations)

addMetadataByPrecedence(annotations, getTaskrunAnnotations(pr))

if pipelineTask.TaskSpec != nil {
tsAnnotations = pipelineTask.TaskSpecMetadata().Annotations
addMetadataByPrecedence(annotations, pipelineTask.TaskSpecMetadata().Annotations)
}

// annotations from TaskRun takes higher precedence over the ones specified in Pipeline through TaskSpec
// initialize annotations with TaskRun annotations
annotations := trAnnotations
for key, value := range tsAnnotations {
// add annotations from TaskSpec if the annotation does not exist
if _, ok := annotations[key]; !ok {
annotations[key] = value
return annotations
}

// Metadata Precedence Order: PipelineTaskRunSpec > PipelineRun > PipelineTaskSpec
func addMetadataByPrecedence(metadata map[string]string, addedMetadata map[string]string) {
for key, value := range addedMetadata {
// add new annotations if the key not exists in current ones
if _, ok := metadata[key]; !ok {
metadata[key] = value
}
}
return annotations
}

// getFinallyTaskRunTimeout returns the timeout to set when creating the ResolvedPipelineRunTask, which is a finally Task.
Expand Down
123 changes: 123 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7395,3 +7395,126 @@ func checkPipelineRunConditionStatusAndReason(t *testing.T, reconciledRun *v1bet
t.Errorf("Expected reason %s but was %s", conditionReason, condition.Reason)
}
}

func TestPropagatePipelineTaskRunSpecMetadata(t *testing.T) {
names.TestingSeed()
prName := "test-pipeline-run"
ps := []*v1beta1.Pipeline{simpleHelloWorldPipeline}
prs := []*v1beta1.PipelineRun{parse.MustParsePipelineRun(t, `
metadata:
name: test-pipeline-run
namespace: foo
spec:
pipelineRef:
name: test-pipeline
taskRunSpecs:
- pipelineTaskName: hello-world-1
metadata:
labels:
PipelineTaskRunSpecLabel: PipelineTaskRunSpecValue
annotations:
PipelineTaskRunSpecAnnotation: PipelineTaskRunSpecValue
taskServiceAccountName: custom-sa
`)}
ts := []*v1beta1.Task{simpleHelloWorldTask}

d := test.Data{
PipelineRuns: prs,
Pipelines: ps,
Tasks: ts,
}
prt := newPipelineRunTest(d, t)
defer prt.Cancel()

_, clients := prt.reconcileRun("foo", prName, []string{}, false)

actual := getTaskRunCreations(t, clients.Pipeline.Actions(), 2)[0]
expectedTaskRunObjectMeta := taskRunObjectMeta("test-pipeline-run-hello-world-1", "foo", "test-pipeline-run", "test-pipeline", "hello-world-1", false)
expectedTaskRunObjectMeta.Labels["PipelineTaskRunSpecLabel"] = "PipelineTaskRunSpecValue"
expectedTaskRunObjectMeta.Annotations["PipelineTaskRunSpecAnnotation"] = "PipelineTaskRunSpecValue"
expectedTaskRun := mustParseTaskRunWithObjectMeta(t, expectedTaskRunObjectMeta, `
spec:
resources: {}
serviceAccountName: custom-sa
taskRef:
name: hello-world
timeout: 1h0m0s
`)

if d := cmp.Diff(actual, expectedTaskRun, ignoreTypeMeta); d != "" {
t.Errorf("expected to see propagated metadata from PipelineTaskRunSpec in TaskRun %v created. Diff %s", expectedTaskRun, diff.PrintWantGot(d))
}
}

func TestMetadataPrecedence(t *testing.T) {
names.TestingSeed()
prName := "test-pipeline-run"
ps := []*v1beta1.Pipeline{parse.MustParsePipeline(t, `
metadata:
name: test-pipeline
namespace: foo
spec:
tasks:
- name: hello-world-1
taskSpec:
steps:
- name: foo-step
image: foo-image
metadata:
labels:
TestPrecedenceLabel: PipelineTaskSpecValue
annotations:
TestPrecedenceAnnotation: PipelineTaskSpecValue
`)}
prs := []*v1beta1.PipelineRun{parse.MustParsePipelineRun(t, `
metadata:
name: test-pipeline-run
namespace: foo
metadata:
labels:
TestPrecedenceLabel: PipelineRunValue
annotations:
TestPrecedenceAnnotation: PipelineRunValue
spec:
pipelineRef:
name: test-pipeline
taskRunSpecs:
- pipelineTaskName: hello-world-1
metadata:
labels:
TestPrecedenceLabel: PipelineTaskRunSpecValue
annotations:
TestPrecedenceAnnotation: PipelineTaskRunSpecValue
taskServiceAccountName: custom-sa
`)}
ts := []*v1beta1.Task{simpleHelloWorldTask}

d := test.Data{
PipelineRuns: prs,
Pipelines: ps,
Tasks: ts,
}
prt := newPipelineRunTest(d, t)
defer prt.Cancel()

_, clients := prt.reconcileRun("foo", prName, []string{}, false)

actual := getTaskRunCreations(t, clients.Pipeline.Actions(), 2)[0]
expectedTaskRunObjectMeta := taskRunObjectMeta("test-pipeline-run-hello-world-1", "foo", "test-pipeline-run", "test-pipeline", "hello-world-1", false)
expectedTaskRunObjectMeta.Labels["TestPrecedenceLabel"] = "PipelineTaskRunSpecValue"
expectedTaskRunObjectMeta.Annotations["TestPrecedenceAnnotation"] = "PipelineTaskRunSpecValue"
expectedTaskRun := mustParseTaskRunWithObjectMeta(t, expectedTaskRunObjectMeta, `
spec:
resources: {}
serviceAccountName: custom-sa
taskSpec:
steps:
- name: foo-step
image: foo-image
timeout: 1h0m0s
`)

if d := cmp.Diff(actual, expectedTaskRun, ignoreTypeMeta); d != "" {
t.Errorf("expected to see propagated metadata by the precedence from PipelineTaskRunSpec in TaskRun %v created. Diff %s", expectedTaskRun, diff.PrintWantGot(d))
}
}

0 comments on commit 1f9a5d0

Please sign in to comment.