Skip to content

Commit

Permalink
More places to use kmap.Union to merge maps
Browse files Browse the repository at this point in the history
Add on to tektoncd#5660.

Previously, we use too much self-invented code to merge two maps.
Now we use the existing [kmap.Union](https://pkg.go.dev/knative.dev/pkg@v0.0.0-20221014164553-b812affa3893/kmap#Union) to merge maps.

Signed-off-by: Chuang Wang <chuangw@google.com>
  • Loading branch information
chuangw6 authored and tekton-robot committed Oct 19, 2022
1 parent 2ed31e9 commit a132c7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
26 changes: 4 additions & 22 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,30 +1258,12 @@ func storePipelineSpecAndMergeMeta(pr *v1beta1.PipelineRun, ps *v1beta1.Pipeline
if pr.Status.PipelineSpec == nil {
pr.Status.PipelineSpec = ps

// Propagate labels from Pipeline to PipelineRun.
if pr.ObjectMeta.Labels == nil {
pr.ObjectMeta.Labels = make(map[string]string, len(meta.Labels)+1)
}
for key, value := range meta.Labels {
// Do not override duplicates between PipelineRun and Pipeline
// PipelineRun labels take precedences over Pipeline
if _, ok := pr.ObjectMeta.Labels[key]; !ok {
pr.ObjectMeta.Labels[key] = value
}
}
// Propagate labels from Pipeline to PipelineRun. PipelineRun labels take precedences over Pipeline.
pr.ObjectMeta.Labels = kmap.Union(meta.Labels, pr.ObjectMeta.Labels)
pr.ObjectMeta.Labels[pipeline.PipelineLabelKey] = meta.Name

// Propagate annotations from Pipeline to PipelineRun.
if pr.ObjectMeta.Annotations == nil {
pr.ObjectMeta.Annotations = make(map[string]string, len(meta.Annotations))
}
for key, value := range meta.Annotations {
// Do not override duplicates between PipelineRun and Pipeline
// PipelineRun labels take precedences over Pipeline
if _, ok := pr.ObjectMeta.Annotations[key]; !ok {
pr.ObjectMeta.Annotations[key] = value
}
}
// Propagate annotations from Pipeline to PipelineRun. PipelineRun annotations take precedences over Pipeline.
pr.ObjectMeta.Annotations = kmap.Union(meta.Annotations, pr.ObjectMeta.Annotations)

}
return nil
Expand Down
26 changes: 4 additions & 22 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,28 +913,10 @@ func storeTaskSpecAndMergeMeta(tr *v1beta1.TaskRun, ts *v1beta1.TaskSpec, meta *
// Only store the TaskSpec once, if it has never been set before.
if tr.Status.TaskSpec == nil {
tr.Status.TaskSpec = ts
// Propagate annotations from Task to TaskRun.
if tr.ObjectMeta.Annotations == nil {
tr.ObjectMeta.Annotations = make(map[string]string, len(meta.Annotations))
}
for key, value := range meta.Annotations {
// Do not override duplicates between TaskRun and Task
// TaskRun labels take precedences over Task
if _, ok := tr.ObjectMeta.Annotations[key]; !ok {
tr.ObjectMeta.Annotations[key] = value
}
}
// Propagate labels from Task to TaskRun.
if tr.ObjectMeta.Labels == nil {
tr.ObjectMeta.Labels = make(map[string]string, len(meta.Labels)+1)
}
for key, value := range meta.Labels {
// Do not override duplicates between TaskRun and Task
// TaskRun labels take precedences over Task
if _, ok := tr.ObjectMeta.Labels[key]; !ok {
tr.ObjectMeta.Labels[key] = value
}
}
// Propagate annotations from Task to TaskRun. TaskRun annotations take precedences over Task.
tr.ObjectMeta.Annotations = kmap.Union(meta.Annotations, tr.ObjectMeta.Annotations)
// Propagate labels from Task to TaskRun. TaskRun labels take precedences over Task.
tr.ObjectMeta.Labels = kmap.Union(meta.Labels, tr.ObjectMeta.Labels)
if tr.Spec.TaskRef != nil {
if tr.Spec.TaskRef.Kind == "ClusterTask" {
tr.ObjectMeta.Labels[pipeline.ClusterTaskLabelKey] = meta.Name
Expand Down

0 comments on commit a132c7a

Please sign in to comment.