Skip to content

Commit

Permalink
cleaning up label keys
Browse files Browse the repository at this point in the history
We have defined GroupName as a constant which is set to tekon.dev in
register.go. We have other label keys defined in the same go file which
are used by the controller to set the appropriate labels. These keys
are TaskLabelKey, PipelineLabelKey, etc. These keys are set to
"/task", "/pipeline", etc. Now while adding such labels in the tekton
resources, we join GroupName to the key. Instead, this commit changes the way
we have defined a label key to include GroupName as part of the label key and
use just the label key instead of concatinating while using the key. This
cleansup the code and makes it simple to add tekton internal labels and
retrieve them.
  • Loading branch information
pritidesai authored and tekton-robot committed Aug 17, 2021
1 parent 281a7c3 commit f0a2f00
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 217 deletions.
20 changes: 10 additions & 10 deletions pkg/apis/pipeline/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ const (
GroupName = "tekton.dev"

// ClusterTaskLabelKey is used as the label identifier for a ClusterTask
ClusterTaskLabelKey = "/clusterTask"
ClusterTaskLabelKey = GroupName + "/clusterTask"

// TaskLabelKey is used as the label identifier for a Task
TaskLabelKey = "/task"
TaskLabelKey = GroupName + "/task"

// TaskRunLabelKey is used as the label identifier for a TaskRun
TaskRunLabelKey = "/taskRun"
TaskRunLabelKey = GroupName + "/taskRun"

// PipelineLabelKey is used as the label identifier for a Pipeline
PipelineLabelKey = "/pipeline"
PipelineLabelKey = GroupName + "/pipeline"

// PipelineRunLabelKey is used as the label identifier for a PipelineRun
PipelineRunLabelKey = "/pipelineRun"
PipelineRunLabelKey = GroupName + "/pipelineRun"

// PipelineTaskLabelKey is used as the label identifier for a PipelineTask
PipelineTaskLabelKey = "/pipelineTask"
PipelineTaskLabelKey = GroupName + "/pipelineTask"

// ConditionCheckKey is used as the label identifier for a ConditionCheck
ConditionCheckKey = "/conditionCheck"
ConditionCheckKey = GroupName + "/conditionCheck"

// ConditionNameKey is used as the label identifier for a Condition
ConditionNameKey = "/conditionName"
ConditionNameKey = GroupName + "/conditionName"

// RunKey is used as the label identifier for a Run
RunKey = "/run"
RunKey = GroupName + "/run"

// MemberOfLabelKey is used as the label identifier for a PipelineTask
// Set to Tasks/Finally depending on the position of the PipelineTask
MemberOfLabelKey = "/memberOf"
MemberOfLabelKey = GroupName + "/memberOf"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ func (tr *TaskRun) IsPartOfPipeline() (bool, string, string) {
return false, "", ""
}

if pl, ok := tr.Labels[pipeline.GroupName+pipeline.PipelineLabelKey]; ok {
return true, pl, tr.Labels[pipeline.GroupName+pipeline.PipelineRunLabelKey]
if pl, ok := tr.Labels[pipeline.PipelineLabelKey]; ok {
return true, pl, tr.Labels[pipeline.PipelineRunLabelKey]
}

return false, "", ""
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1alpha1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func TestTaskRunIsOfPipelinerun(t *testing.T) {
}{{
name: "yes",
tr: tb.TaskRun("taskrunname",
tb.TaskRunLabel(pipeline.GroupName+pipeline.PipelineLabelKey, "pipeline"),
tb.TaskRunLabel(pipeline.GroupName+pipeline.PipelineRunLabelKey, "pipelinerun"),
tb.TaskRunLabel(pipeline.PipelineLabelKey, "pipeline"),
tb.TaskRunLabel(pipeline.PipelineRunLabelKey, "pipelinerun"),
),
expectedValue: true,
expetectedPipeline: "pipeline",
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ func (tr *TaskRun) IsPartOfPipeline() (bool, string, string) {
return false, "", ""
}

if pl, ok := tr.Labels[pipeline.GroupName+pipeline.PipelineLabelKey]; ok {
return true, pl, tr.Labels[pipeline.GroupName+pipeline.PipelineRunLabelKey]
if pl, ok := tr.Labels[pipeline.PipelineLabelKey]; ok {
return true, pl, tr.Labels[pipeline.PipelineRunLabelKey]
}

return false, "", ""
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func TestTaskRunIsOfPipelinerun(t *testing.T) {
tr: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
pipeline.GroupName + pipeline.PipelineLabelKey: "pipeline",
pipeline.GroupName + pipeline.PipelineRunLabelKey: "pipelinerun",
pipeline.PipelineLabelKey: "pipeline",
pipeline.PipelineRunLabelKey: "pipelinerun",
},
},
},
Expand Down
5 changes: 1 addition & 4 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ import (
)

const (
// TaskRunLabelKey is the name of the label added to the Pod to identify the TaskRun
TaskRunLabelKey = pipeline.GroupName + pipeline.TaskRunLabelKey

// TektonHermeticEnvVar is the env var we set in containers to indicate they should be run hermetically
TektonHermeticEnvVar = "TEKTON_HERMETIC"

Expand Down Expand Up @@ -356,7 +353,7 @@ func makeLabels(s *v1beta1.TaskRun) map[string]string {

// NB: Set this *after* passing through TaskRun Labels. If the TaskRun
// specifies this label, it should be overridden by this value.
labels[TaskRunLabelKey] = s.Name
labels[pipeline.TaskRunLabelKey] = s.Name
return labels
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1806,9 +1806,9 @@ func TestPodBuildwithAlphaAPIEnabled(t *testing.T) {
func TestMakeLabels(t *testing.T) {
taskRunName := "task-run-name"
want := map[string]string{
TaskRunLabelKey: taskRunName,
"foo": "bar",
"hello": "world",
pipeline.TaskRunLabelKey: taskRunName,
"foo": "bar",
"hello": "world",
}
got := makeLabels(&v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/pipelinerun/affinity_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func getStatefulSetLabels(pr *v1beta1.PipelineRun, affinityAssistantName string)
for key, val := range pr.ObjectMeta.Labels {
labels[key] = val
}
labels[pipeline.GroupName+pipeline.PipelineRunLabelKey] = pr.Name
labels[pipeline.PipelineRunLabelKey] = pr.Name

// LabelInstance is used to configure PodAffinity for all TaskRuns belonging to this Affinity Assistant
// LabelComponent is used to configure PodAntiAffinity to other Affinity Assistants
Expand Down
22 changes: 11 additions & 11 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (c *Reconciler) reconcile(ctx context.Context, pr *v1beta1.PipelineRun, get
for key, value := range pipelineMeta.Labels {
pr.ObjectMeta.Labels[key] = value
}
pr.ObjectMeta.Labels[pipeline.GroupName+pipeline.PipelineLabelKey] = pipelineMeta.Name
pr.ObjectMeta.Labels[pipeline.PipelineLabelKey] = pipelineMeta.Name

// Propagate annotations from Pipeline to PipelineRun.
if pr.ObjectMeta.Annotations == nil {
Expand Down Expand Up @@ -939,22 +939,22 @@ func getTaskrunLabels(pr *v1beta1.PipelineRun, pipelineTaskName string, includeP
labels[key] = val
}
}
labels[pipeline.GroupName+pipeline.PipelineRunLabelKey] = pr.Name
labels[pipeline.PipelineRunLabelKey] = pr.Name
if pipelineTaskName != "" {
labels[pipeline.GroupName+pipeline.PipelineTaskLabelKey] = pipelineTaskName
labels[pipeline.PipelineTaskLabelKey] = pipelineTaskName
}
if pr.Status.PipelineSpec != nil {
// check if a task is part of the "tasks" section, add a label to identify it during the runtime
for _, f := range pr.Status.PipelineSpec.Tasks {
if pipelineTaskName == f.Name {
labels[pipeline.GroupName+pipeline.MemberOfLabelKey] = v1beta1.PipelineTasks
labels[pipeline.MemberOfLabelKey] = v1beta1.PipelineTasks
break
}
}
// check if a task is part of the "finally" section, add a label to identify it during the runtime
for _, f := range pr.Status.PipelineSpec.Finally {
if pipelineTaskName == f.Name {
labels[pipeline.GroupName+pipeline.MemberOfLabelKey] = v1beta1.PipelineFinallyTasks
labels[pipeline.MemberOfLabelKey] = v1beta1.PipelineFinallyTasks
break
}
}
Expand Down Expand Up @@ -1132,8 +1132,8 @@ func (c *Reconciler) updateLabelsAndAnnotations(ctx context.Context, pr *v1beta1

func (c *Reconciler) makeConditionCheckContainer(ctx context.Context, rprt *resources.ResolvedPipelineRunTask, rcc *resources.ResolvedConditionCheck, pr *v1beta1.PipelineRun) (*v1beta1.ConditionCheck, error) {
labels := getTaskrunLabels(pr, rprt.PipelineTask.Name, true)
labels[pipeline.GroupName+pipeline.ConditionCheckKey] = rcc.ConditionCheckName
labels[pipeline.GroupName+pipeline.ConditionNameKey] = rcc.Condition.Name
labels[pipeline.ConditionCheckKey] = rcc.ConditionCheckName
labels[pipeline.ConditionNameKey] = rcc.Condition.Name

for key, value := range rcc.Condition.ObjectMeta.Labels {
labels[key] = value
Expand Down Expand Up @@ -1233,8 +1233,8 @@ func updatePipelineRunStatusFromTaskRuns(logger *zap.SugaredLogger, pr *v1beta1.
continue
}
lbls := taskrun.GetLabels()
pipelineTaskName := lbls[pipeline.GroupName+pipeline.PipelineTaskLabelKey]
if _, ok := lbls[pipeline.GroupName+pipeline.ConditionCheckKey]; ok {
pipelineTaskName := lbls[pipeline.PipelineTaskLabelKey]
if _, ok := lbls[pipeline.ConditionCheckKey]; ok {
// Save condition for looping over them after this
if _, ok := conditionTaskRuns[pipelineTaskName]; !ok {
// If it's the first condition taskrun, initialise the slice
Expand Down Expand Up @@ -1283,7 +1283,7 @@ func updatePipelineRunStatusFromTaskRuns(logger *zap.SugaredLogger, pr *v1beta1.
// The condition check was not found, so we need to add it
// We only add the condition name, the status can now be gathered by the
// normal reconcile process
if conditionName, ok := lbls[pipeline.GroupName+pipeline.ConditionNameKey]; ok {
if conditionName, ok := lbls[pipeline.ConditionNameKey]; ok {
conditionChecks[foundTaskRun.Name] = &v1beta1.PipelineRunConditionCheckStatus{
ConditionName: fmt.Sprintf("%s-%s", conditionName, strconv.Itoa(i)),
}
Expand Down Expand Up @@ -1315,7 +1315,7 @@ func updatePipelineRunStatusFromRuns(logger *zap.SugaredLogger, pr *v1beta1.Pipe
continue
}
lbls := run.GetLabels()
pipelineTaskName := lbls[pipeline.GroupName+pipeline.PipelineTaskLabelKey]
pipelineTaskName := lbls[pipeline.PipelineTaskLabelKey]
if _, ok := pr.Status.Runs[run.Name]; !ok {
// This run was missing from the status.
pr.Status.Runs[run.Name] = &v1beta1.PipelineRunRunStatus{
Expand Down
Loading

0 comments on commit f0a2f00

Please sign in to comment.