Skip to content

Commit a437fff

Browse files
authored
Fix embedded TargetAllocator CR labels (#4026)
* Fix embedded TargetAllocator CR labels * Add changelog entry
1 parent 651da45 commit a437fff

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: target allocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Add app.kubernetes.io/managed-by label to Target Allocator CRs created by the Collector CR
9+
10+
# One or more tracking issues related to the change
11+
issues: [4025]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

internal/controllers/builder_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,9 @@ service:
31363136
ObjectMeta: metav1.ObjectMeta{
31373137
Name: "test",
31383138
Namespace: "test",
3139-
Labels: nil,
3139+
Labels: map[string]string{
3140+
"app.kubernetes.io/managed-by": "opentelemetry-operator",
3141+
},
31403142
},
31413143
Spec: v1alpha1.TargetAllocatorSpec{
31423144
FilterStrategy: v1beta1.TargetAllocatorFilterStrategyRelabelConfig,
@@ -3378,7 +3380,9 @@ service:
33783380
ObjectMeta: metav1.ObjectMeta{
33793381
Name: "test",
33803382
Namespace: "test",
3381-
Labels: nil,
3383+
Labels: map[string]string{
3384+
"app.kubernetes.io/managed-by": "opentelemetry-operator",
3385+
},
33823386
},
33833387
Spec: v1alpha1.TargetAllocatorSpec{
33843388
FilterStrategy: v1beta1.TargetAllocatorFilterStrategyRelabelConfig,

internal/controllers/reconcile_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,9 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
533533
ObjectMeta: metav1.ObjectMeta{
534534
Name: params.Name,
535535
Namespace: params.Namespace,
536-
Labels: nil,
536+
Labels: map[string]string{
537+
"app.kubernetes.io/managed-by": "opentelemetry-operator",
538+
},
537539
},
538540
Spec: v1alpha1.TargetAllocatorSpec{
539541
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{},

internal/manifests/collector/targetallocator.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package collector
55

66
import (
7+
"maps"
8+
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810

911
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
@@ -13,18 +15,25 @@ import (
1315

1416
// TargetAllocator builds the TargetAllocator CR for the given instance.
1517
func TargetAllocator(params manifests.Params) (*v1alpha1.TargetAllocator, error) {
16-
1718
taSpec := params.OtelCol.Spec.TargetAllocator
1819
if !taSpec.Enabled {
1920
return nil, nil
2021
}
2122

23+
// setting all the labels normally here leads to some undesirable results, like the name label being wrong
24+
// instead, only set managed-by and leave everything else as-is
25+
labels := maps.Clone(params.OtelCol.Labels)
26+
if labels == nil {
27+
labels = make(map[string]string, 1)
28+
}
29+
labels["app.kubernetes.io/managed-by"] = "opentelemetry-operator"
30+
2231
return &v1alpha1.TargetAllocator{
2332
ObjectMeta: metav1.ObjectMeta{
2433
Name: params.OtelCol.Name,
2534
Namespace: params.OtelCol.Namespace,
2635
Annotations: params.OtelCol.Annotations,
27-
Labels: params.OtelCol.Labels,
36+
Labels: labels,
2837
},
2938
Spec: v1alpha1.TargetAllocatorSpec{
3039
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{

internal/manifests/collector/targetallocator_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ func TestTargetAllocator(t *testing.T) {
2929
"label_key": "label_value",
3030
},
3131
}
32+
expectedObjectMetadata := metav1.ObjectMeta{
33+
Name: "name",
34+
Namespace: "namespace",
35+
Annotations: map[string]string{
36+
"annotation_key": "annotation_value",
37+
},
38+
Labels: map[string]string{
39+
"app.kubernetes.io/managed-by": "opentelemetry-operator",
40+
"label_key": "label_value",
41+
},
42+
}
3243
replicas := int32(2)
3344
runAsNonRoot := true
3445
privileged := true
@@ -63,7 +74,7 @@ func TestTargetAllocator(t *testing.T) {
6374
},
6475
},
6576
want: &v1alpha1.TargetAllocator{
66-
ObjectMeta: objectMetadata,
77+
ObjectMeta: expectedObjectMetadata,
6778
Spec: v1alpha1.TargetAllocatorSpec{
6879
GlobalConfig: v1beta1.AnyConfig{},
6980
},
@@ -172,7 +183,7 @@ func TestTargetAllocator(t *testing.T) {
172183
},
173184
},
174185
want: &v1alpha1.TargetAllocator{
175-
ObjectMeta: objectMetadata,
186+
ObjectMeta: expectedObjectMetadata,
176187
Spec: v1alpha1.TargetAllocatorSpec{
177188
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
178189
Replicas: &replicas,

0 commit comments

Comments
 (0)