Skip to content

Commit

Permalink
Add managed-by label to Pods created from TaskRuns
Browse files Browse the repository at this point in the history
Also annotate the controllers' Deployments and created Pods with labels
denoting their purpose.

This is a Kubernetes best practice
(https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/)
and will allow operators to understand at a high level where these Pods
come from.
  • Loading branch information
imjasonh committed Sep 18, 2019
1 parent 6747f35 commit 85411e9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 24 deletions.
17 changes: 9 additions & 8 deletions config/config-observability.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ data:
# metrics.backend-destination field specifies the system metrics destination.
# It supports either prometheus (the default) or stackdriver.
# Note: Using stackdriver will incur additional charges
# Note: Using Stackdriver will incur additional charges.
metrics.backend-destination: prometheus
# metrics.stackdriver-project-id field specifies the stackdriver project ID. This
# metrics.stackdriver-project-id field specifies the Stackdriver project ID. This
# field is optional. When running on GCE, application default credentials will be
# used if this field is not provided.
# used and metrics will be sent to the cluster's project if this field is
# not provided.
metrics.stackdriver-project-id: "<your stackdriver project id>"
# metrics.allow-stackdriver-custom-metrics indicates whether it is allowed to send metrics to
# Stackdriver using "global" resource type and custom metric type if the
# metrics are not supported by "knative_revision" resource type. Setting this
# flag to "true" could cause extra Stackdriver charge.
# If metrics.backend-destination is not Stackdriver, this is ignored.
# metrics.allow-stackdriver-custom-metrics indicates whether it is allowed
# to send metrics to Stackdriver using "global" resource type and custom
# metric type. Setting this flag to "true" could cause extra Stackdriver
# charge. If metrics.backend-destination is not Stackdriver, this is
# ignored.
metrics.allow-stackdriver-custom-metrics: "false"
5 changes: 5 additions & 0 deletions config/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ kind: Deployment
metadata:
name: tekton-pipelines-controller
namespace: tekton-pipelines
labels:
app.kubernetes.io/name: tekton-pipelines
app.kubernetes.io/component: controller
spec:
replicas: 1
template:
Expand All @@ -24,6 +27,8 @@ spec:
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
labels:
app: tekton-pipelines-controller
app.kubernetes.io/name: tekton-pipelines
app.kubernetes.io/component: controller
spec:
serviceAccountName: tekton-pipelines-controller
containers:
Expand Down
5 changes: 5 additions & 0 deletions config/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ kind: Deployment
metadata:
name: tekton-pipelines-webhook
namespace: tekton-pipelines
labels:
app.kubernetes.io/name: tekton-pipelines
app.kubernetes.io/component: webhook-controller
spec:
replicas: 1
template:
Expand All @@ -25,6 +28,8 @@ spec:
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
labels:
app: tekton-pipelines-webhook
app.kubernetes.io/name: tekton-pipelines
app.kubernetes.io/component: webhook-controller
spec:
serviceAccountName: tekton-pipelines-controller
containers:
Expand Down
14 changes: 13 additions & 1 deletion pkg/reconciler/taskrun/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import (
const (
workspaceDir = "/workspace"
homeDir = "/builder/home"

taskRunLabelKey = pipeline.GroupName + pipeline.TaskRunLabelKey
ManagedByLabelKey = "app.kubernetes.io/managed-by"
ManagedByLabelValue = "tekton-pipelines"
)

// These are effectively const, but Go doesn't have such an annotation.
Expand Down Expand Up @@ -387,10 +391,18 @@ func IsContainerStep(name string) bool {
// makeLabels constructs the labels we will propagate from TaskRuns to Pods.
func makeLabels(s *v1alpha1.TaskRun) map[string]string {
labels := make(map[string]string, len(s.ObjectMeta.Labels)+1)
// NB: Set this *before* passing through TaskRun labels. If the TaskRun
// has a managed-by label, it should override this default.

// Copy through the TaskRun's labels to the underlying Pod's.
labels[ManagedByLabelKey] = ManagedByLabelValue
for k, v := range s.ObjectMeta.Labels {
labels[k] = v
}
labels[pipeline.GroupName+pipeline.TaskRunLabelKey] = s.Name

// 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
return labels
}

Expand Down
75 changes: 60 additions & 15 deletions pkg/reconciler/taskrun/resources/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ func TestMakePod(t *testing.T) {
defer func() { randReader = rand.Reader }()

for _, c := range []struct {
desc string
trs v1alpha1.TaskRunSpec
ts v1alpha1.TaskSpec
bAnnotations map[string]string
want *corev1.PodSpec
wantErr error
desc string
trs v1alpha1.TaskRunSpec
ts v1alpha1.TaskSpec
annotations map[string]string
labels map[string]string
want *corev1.PodSpec
wantErr error
}{{
desc: "simple",
ts: v1alpha1.TaskSpec{
Expand All @@ -133,7 +134,7 @@ func TestMakePod(t *testing.T) {
Image: "image",
}}},
},
bAnnotations: map[string]string{
annotations: map[string]string{
"simple-annotation-key": "simple-annotation-val",
},
want: &corev1.PodSpec{
Expand Down Expand Up @@ -215,7 +216,7 @@ func TestMakePod(t *testing.T) {
Image: "image",
}}},
},
bAnnotations: map[string]string{
annotations: map[string]string{
"simple-annotation-key": "simple-annotation-val",
},
want: &corev1.PodSpec{
Expand Down Expand Up @@ -253,7 +254,7 @@ func TestMakePod(t *testing.T) {
Image: "image",
}}},
},
bAnnotations: map[string]string{
annotations: map[string]string{
"simple-annotation-key": "simple-annotation-val",
},
want: &corev1.PodSpec{
Expand Down Expand Up @@ -403,7 +404,7 @@ func TestMakePod(t *testing.T) {
tr := &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "taskrun-name",
Annotations: c.bAnnotations,
Annotations: c.annotations,
},
Spec: c.trs,
}
Expand All @@ -423,8 +424,8 @@ func TestMakePod(t *testing.T) {
}

wantAnnotations := map[string]string{ReadyAnnotation: ""}
if c.bAnnotations != nil {
for key, val := range c.bAnnotations {
if c.annotations != nil {
for key, val := range c.annotations {
wantAnnotations[key] = val
}
}
Expand All @@ -433,7 +434,51 @@ func TestMakePod(t *testing.T) {
}
})
}
}

func TestMakeLabels(t *testing.T) {
taskRunName := "task-run-name"
for _, c := range []struct {
desc string
trLabels map[string]string
want map[string]string
}{{
desc: "taskrun labels pass through",
trLabels: map[string]string{
"foo": "bar",
"hello": "world",
},
want: map[string]string{
taskRunLabelKey: taskRunName,
ManagedByLabelKey: ManagedByLabelValue,
"foo": "bar",
"hello": "world",
},
}, {
desc: "taskrun managed-by overrides; taskrun label key doesn't",
trLabels: map[string]string{
"foo": "bar",
taskRunLabelKey: "override-me",
ManagedByLabelKey: "managed-by-something-else",
},
want: map[string]string{
taskRunLabelKey: taskRunName,
ManagedByLabelKey: "managed-by-something-else",
"foo": "bar",
},
}} {
t.Run(c.desc, func(t *testing.T) {
got := makeLabels(&v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: taskRunName,
Labels: c.trLabels,
},
})
if d := cmp.Diff(got, c.want); d != "" {
t.Errorf("Diff labels:\n%s", d)
}
})
}
}

func TestAddReadyAnnotation(t *testing.T) {
Expand Down Expand Up @@ -528,7 +573,7 @@ func TestInitOutputResourcesDefaultDir(t *testing.T) {
}},
},
}
bAnnotations := map[string]string{
annotations := map[string]string{
"simple-annotation-key": "simple-annotation-val",
}
want := &corev1.PodSpec{
Expand Down Expand Up @@ -590,7 +635,7 @@ func TestInitOutputResourcesDefaultDir(t *testing.T) {
tr := &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "taskrun-name",
Annotations: bAnnotations,
Annotations: annotations,
},
Spec: trs,
}
Expand All @@ -610,7 +655,7 @@ func TestInitOutputResourcesDefaultDir(t *testing.T) {
}

wantAnnotations := map[string]string{"simple-annotation-key": "simple-annotation-val", ReadyAnnotation: ""}
for key, val := range bAnnotations {
for key, val := range annotations {
wantAnnotations[key] = val
}
if d := cmp.Diff(got.Annotations, wantAnnotations); d != "" {
Expand Down
12 changes: 12 additions & 0 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-task"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-success"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-run-success",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -462,6 +463,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-with-sa"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-sa-run-success",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -494,6 +496,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-task-with-substitution"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-substitution"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-substitution",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -584,6 +587,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-output-task"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-input-output"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-input-output",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -716,6 +720,7 @@ func TestReconcile(t *testing.T) {
wantPod: tb.Pod("test-taskrun-with-taskspec-pod-123456", "foo",
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-taskspec"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-taskspec",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -763,6 +768,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-cluster-task"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-cluster-task"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-cluster-task",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -793,6 +799,7 @@ func TestReconcile(t *testing.T) {
wantPod: tb.Pod("test-taskrun-with-resource-spec-pod-123456", "foo",
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-resource-spec"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-resource-spec",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -841,6 +848,7 @@ func TestReconcile(t *testing.T) {
tb.PodLabel("TaskRunLabel", "TaskRunValue"),
tb.PodLabel(taskNameLabelKey, "test-task"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-labels"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-labels",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -873,6 +881,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("TaskRunAnnotation", "TaskRunValue"),
tb.PodLabel(taskNameLabelKey, "test-task"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-annotations"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-annotations",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -904,6 +913,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-task-env"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-task-env"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-task-env",
tb.OwnerReferenceAPIVersion("tekton.dev/v1alpha1")),
tb.PodSpec(
Expand Down Expand Up @@ -936,6 +946,7 @@ func TestReconcile(t *testing.T) {
wantPod: tb.Pod("test-taskrun-with-resource-requests-pod-123456", "foo",
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-resource-requests"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-resource-requests",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down Expand Up @@ -1004,6 +1015,7 @@ func TestReconcile(t *testing.T) {
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-task"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-pod"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-pod",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
Expand Down

0 comments on commit 85411e9

Please sign in to comment.