Skip to content

Commit fa2dc29

Browse files
vdemeestertekton-robot
authored andcommitted
Do not propagate managed-by annotation to Pods
Let's make sure we are not propagating `app.kubernetes.io/managed-by` annotation to Pods. Doing it can lead to weird behavior, for example with applied with Helm, or if some admission controller is looking for pipeline's Pods and do not get it if the `TaskRun` or `PipelineRun` was created with an `app.kubernetes.io/managed-by`. Signed-off-by: Vincent Demeester <[email protected]>
1 parent 03a3fa9 commit fa2dc29

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

pkg/pod/pod.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
3434
"github.com/tektoncd/pipeline/pkg/internal/computeresources/tasklevel"
3535
"github.com/tektoncd/pipeline/pkg/names"
36+
tknreconciler "github.com/tektoncd/pipeline/pkg/reconciler"
3637
"github.com/tektoncd/pipeline/pkg/spire"
3738
corev1 "k8s.io/api/core/v1"
3839
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,6 +42,7 @@ import (
4142
"k8s.io/client-go/kubernetes"
4243
"k8s.io/utils/strings/slices"
4344
"knative.dev/pkg/changeset"
45+
"knative.dev/pkg/kmap"
4446
"knative.dev/pkg/kmeta"
4547
)
4648

@@ -159,6 +161,7 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1.TaskRun, taskSpec v1.Ta
159161
enableKeepPodOnCancel := featureFlags.EnableKeepPodOnCancel
160162
setSecurityContext := config.FromContextOrDefaults(ctx).FeatureFlags.SetSecurityContext
161163
setSecurityContextReadOnlyRootFilesystem := config.FromContextOrDefaults(ctx).FeatureFlags.SetSecurityContextReadOnlyRootFilesystem
164+
defaultManagedByLabelValue := config.FromContextOrDefaults(ctx).Defaults.DefaultManagedByLabelValue
162165

163166
// Add our implicit volumes first, so they can be overridden by the user if they prefer.
164167
volumes = append(volumes, implicitVolumes...)
@@ -459,7 +462,7 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1.TaskRun, taskSpec v1.Ta
459462
priorityClassName = *podTemplate.PriorityClassName
460463
}
461464

462-
podAnnotations := kmeta.CopyMap(taskRun.Annotations)
465+
podAnnotations := kmap.ExcludeKeys(kmeta.CopyMap(taskRun.Annotations), tknreconciler.KubernetesManagedByAnnotationKey)
463466
podAnnotations[ReleaseAnnotation] = changeset.Get()
464467

465468
if readyImmediately {
@@ -491,7 +494,7 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1.TaskRun, taskSpec v1.Ta
491494
*metav1.NewControllerRef(taskRun, groupVersionKind),
492495
},
493496
Annotations: podAnnotations,
494-
Labels: makeLabels(taskRun),
497+
Labels: makeLabels(taskRun, defaultManagedByLabelValue),
495498
},
496499
Spec: corev1.PodSpec{
497500
RestartPolicy: corev1.RestartPolicyNever,
@@ -529,7 +532,7 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1.TaskRun, taskSpec v1.Ta
529532
}
530533

531534
// makeLabels constructs the labels we will propagate from TaskRuns to Pods.
532-
func makeLabels(s *v1.TaskRun) map[string]string {
535+
func makeLabels(s *v1.TaskRun, defaultManagedByLabelValue string) map[string]string {
533536
labels := make(map[string]string, len(s.ObjectMeta.Labels)+1)
534537
// NB: Set this *before* passing through TaskRun labels. If the TaskRun
535538
// has a managed-by label, it should override this default.
@@ -543,6 +546,8 @@ func makeLabels(s *v1.TaskRun) map[string]string {
543546
// specifies this label, it should be overridden by this value.
544547
labels[pipeline.TaskRunLabelKey] = s.Name
545548
labels[pipeline.TaskRunUIDLabelKey] = string(s.UID)
549+
// Enforce app.kubernetes.io/managed-by to be the value configured
550+
labels[tknreconciler.KubernetesManagedByAnnotationKey] = defaultManagedByLabelValue
546551
return labels
547552
}
548553

pkg/pod/pod_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
3131
"github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
3232
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
33+
tknreconciler "github.com/tektoncd/pipeline/pkg/reconciler"
3334
"github.com/tektoncd/pipeline/pkg/spire"
3435
"github.com/tektoncd/pipeline/test/diff"
3536
"github.com/tektoncd/pipeline/test/names"
@@ -2621,6 +2622,7 @@ _EOF_
26212622
} else {
26222623
trAnnotations = c.trAnnotation
26232624
trAnnotations[ReleaseAnnotation] = fakeVersion
2625+
26242626
}
26252627
testTaskRunName := taskRunName
26262628
if c.trName != "" {
@@ -3256,6 +3258,7 @@ func TestMakeLabels(t *testing.T) {
32563258
"foo": "bar",
32573259
"hello": "world",
32583260
pipeline.TaskRunUIDLabelKey: string(taskRunUID),
3261+
tknreconciler.KubernetesManagedByAnnotationKey: "foo",
32593262
}
32603263
got := makeLabels(&v1.TaskRun{
32613264
ObjectMeta: metav1.ObjectMeta{
@@ -3266,7 +3269,7 @@ func TestMakeLabels(t *testing.T) {
32663269
"hello": "world",
32673270
},
32683271
},
3269-
})
3272+
}, "foo")
32703273
if d := cmp.Diff(want, got); d != "" {
32713274
t.Errorf("Diff labels %s", diff.PrintWantGot(d))
32723275
}

pkg/reconciler/constant.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ package reconciler
1919
const (
2020
// KubectlLastAppliedAnnotationKey is the key used by kubectl to store its last applied configuration (using kubectl apply)
2121
KubectlLastAppliedAnnotationKey = "kubectl.kubernetes.io/last-applied-configuration"
22+
23+
// KubernetesLastAppliedAnnotationKey is the key used by tools to tell who is managing an object
24+
KubernetesManagedByAnnotationKey = "app.kubernetes.io/managed-by"
2225
)

0 commit comments

Comments
 (0)