Skip to content

Commit 68c3c42

Browse files
authored
fix(collector): remove prometheus annotations on disablePrometheusAnnotations update (#5069)
* fix(collector): track operator-stamped prometheus annotations for clean removal When spec.observability.metrics.disablePrometheusAnnotations is toggled from false to true on an existing OpenTelemetryCollector, the pod template mutate path previously preserved any annotation that existed on the current resource but was absent from the desired render. This left the prometheus.io/scrape, prometheus.io/port, and prometheus.io/path annotations stuck on the rolled pods. The operator now stamps an ownership marker (collector.opentelemetry.io/prometheus-annotations-added=true) whenever PodAnnotations adds at least one default prometheus.io/* annotation. mutatePodTemplate consults that marker on the existing pod template and strips the prometheus.io/* keys + the marker only when the marker is present AND the keys are absent from the desired pod template. This preserves prometheus.io/* annotations the user set out of band on the same collector — the design @swiatekm landed at the 2026-06-04 SIG meeting. If the user pre-sets all three default prometheus.io/* values via metadata.annotations or spec.podAnnotations, the operator adds nothing on top and the marker is not stamped — the operator claims nothing. Upgrade compatibility: pre-upgrade collectors have no marker. On the first reconcile after upgrade with DisablePrometheusAnnotations=false, the marker is stamped and subsequent toggles work normally. If a user upgrades and immediately sets DisablePrometheusAnnotations=true, the operator never stamps the marker on the existing pod template (the stamp path is gated on !Disable), so the pre-upgrade prom annotations survive that single toggle as a documented one-time cost. The mutate test covers this scenario. Fixes #5043 Signed-off-by: praveen9354 <praveen9354@gmail.com> * fix(collector): rename prometheus-annotations-added marker to operator. prefix Per @swiatekm review on #5069, align the ownership-marker key with the operator.opentelemetry.io/ prefix already used for Service-tracking annotations in this repo (collector-headless-service, collector-monitoring-service, collector-service-type) rather than the collector.opentelemetry.io/ prefix introduced in the previous revision. Signed-off-by: praveen9354 <praveen9354@gmail.com> --------- Signed-off-by: praveen9354 <praveen9354@gmail.com>
1 parent 8a1ba4d commit 68c3c42

13 files changed

Lines changed: 296 additions & 14 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Honor `spec.observability.metrics.disablePrometheusAnnotations: true` on update by removing the operator-stamped prometheus.io annotations from the pod template, not just stopping new ones from being added."
9+
10+
# One or more tracking issues related to the change
11+
issues: [5043]
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: |
17+
Previously the pod-template mutate path preserved any annotation that
18+
existed on the current resource but was absent from the desired render,
19+
so toggling disablePrometheusAnnotations from false to true on an
20+
existing OpenTelemetryCollector left the prometheus.io/scrape, port,
21+
and path annotations stuck on the rolled pods. The operator now stamps
22+
an ownership marker (operator.opentelemetry.io/prometheus-annotations-added)
23+
whenever it adds one of the default prometheus.io/* annotations, and
24+
the mutate path strips those annotations only when the marker is
25+
present on the existing pod template. This preserves prometheus.io/*
26+
annotations the user set out of band on the same collector.

internal/controllers/testdata/build_collector_base.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ spec:
2323
metadata:
2424
annotations:
2525
opentelemetry-operator-config/sha256: 2d266e55025628659355f1271b689d6fb53648ef6cd5595831f5835d18e59a25
26+
operator.opentelemetry.io/prometheus-annotations-added: "true"
2627
prometheus.io/path: /metrics
2728
prometheus.io/port: "8888"
2829
prometheus.io/scrape: "true"

internal/controllers/testdata/build_collector_ingress.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ spec:
2323
metadata:
2424
annotations:
2525
opentelemetry-operator-config/sha256: 2d266e55025628659355f1271b689d6fb53648ef6cd5595831f5835d18e59a25
26+
operator.opentelemetry.io/prometheus-annotations-added: "true"
2627
prometheus.io/path: /metrics
2728
prometheus.io/port: "8888"
2829
prometheus.io/scrape: "true"

internal/controllers/testdata/build_collector_service_account.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ spec:
2323
metadata:
2424
annotations:
2525
opentelemetry-operator-config/sha256: 2d266e55025628659355f1271b689d6fb53648ef6cd5595831f5835d18e59a25
26+
operator.opentelemetry.io/prometheus-annotations-added: "true"
2627
prometheus.io/path: /metrics
2728
prometheus.io/port: "8888"
2829
prometheus.io/scrape: "true"

internal/controllers/testdata/build_collector_ta_cr_base.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ spec:
2424
metadata:
2525
annotations:
2626
opentelemetry-operator-config/sha256: 42773025f65feaf30df59a306a9e38f1aaabe94c8310983beaddb7f648d699b0
27+
operator.opentelemetry.io/prometheus-annotations-added: "true"
2728
prometheus.io/path: /metrics
2829
prometheus.io/port: "8888"
2930
prometheus.io/scrape: "true"

internal/controllers/testdata/build_collector_ta_cr_enable_metrics.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ spec:
2424
metadata:
2525
annotations:
2626
opentelemetry-operator-config/sha256: 42773025f65feaf30df59a306a9e38f1aaabe94c8310983beaddb7f648d699b0
27+
operator.opentelemetry.io/prometheus-annotations-added: "true"
2728
prometheus.io/path: /metrics
2829
prometheus.io/port: "8888"
2930
prometheus.io/scrape: "true"

internal/manifests/collector/daemonset_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ func TestDaemonSetNewDefault(t *testing.T) {
4949

5050
// verify sha256 podAnnotation
5151
expectedAnnotations := map[string]string{
52-
"opentelemetry-operator-config/sha256": "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d",
53-
"prometheus.io/path": "/metrics",
54-
"prometheus.io/port": "8888",
55-
"prometheus.io/scrape": "true",
52+
"opentelemetry-operator-config/sha256": "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d",
53+
"prometheus.io/path": "/metrics",
54+
"prometheus.io/port": "8888",
55+
"prometheus.io/scrape": "true",
56+
"operator.opentelemetry.io/prometheus-annotations-added": "true",
5657
}
5758
assert.Equal(t, expectedAnnotations, d.Spec.Template.Annotations)
5859

@@ -294,11 +295,12 @@ func TestDaemonsetPodAnnotations(t *testing.T) {
294295
"prometheus.io/path": "/metrics",
295296
"prometheus.io/port": "8888",
296297
"prometheus.io/scrape": "true",
298+
"operator.opentelemetry.io/prometheus-annotations-added": "true",
297299
}
298300

299301
// verify
300302
assert.Equal(t, "my-instance-collector", ds.Name)
301-
assert.Len(t, ds.Spec.Template.Annotations, 5)
303+
assert.Len(t, ds.Spec.Template.Annotations, 6)
302304
assert.Equal(t, expectedAnnotations, ds.Spec.Template.Annotations)
303305
}
304306

internal/manifests/collector/deployment_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ func TestDeploymentNewDefault(t *testing.T) {
9292

9393
// verify sha256 podAnnotation
9494
expectedAnnotations := map[string]string{
95-
"opentelemetry-operator-config/sha256": "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d",
96-
"prometheus.io/path": "/metrics",
97-
"prometheus.io/port": "8888",
98-
"prometheus.io/scrape": "true",
95+
"opentelemetry-operator-config/sha256": "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d",
96+
"prometheus.io/path": "/metrics",
97+
"prometheus.io/port": "8888",
98+
"prometheus.io/scrape": "true",
99+
"operator.opentelemetry.io/prometheus-annotations-added": "true",
99100
}
100101
assert.Equal(t, expectedAnnotations, d.Spec.Template.Annotations)
101102

@@ -157,10 +158,11 @@ func TestDeploymentPodAnnotations(t *testing.T) {
157158
"prometheus.io/path": "/metrics",
158159
"prometheus.io/port": "8888",
159160
"prometheus.io/scrape": "true",
161+
"operator.opentelemetry.io/prometheus-annotations-added": "true",
160162
}
161163

162164
// verify
163-
assert.Len(t, d.Spec.Template.Annotations, 5)
165+
assert.Len(t, d.Spec.Template.Annotations, 6)
164166
assert.Equal(t, "my-instance-collector", d.Name)
165167
assert.Equal(t, expectedPodAnnotationValues, d.Spec.Template.Annotations)
166168
}

internal/manifests/collector/statefulset_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ func TestStatefulSetNewDefault(t *testing.T) {
5353

5454
// verify sha256 podAnnotation
5555
expectedAnnotations := map[string]string{
56-
"opentelemetry-operator-config/sha256": "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d",
57-
"prometheus.io/path": "/metrics",
58-
"prometheus.io/port": "8888",
59-
"prometheus.io/scrape": "true",
56+
"opentelemetry-operator-config/sha256": "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d",
57+
"prometheus.io/path": "/metrics",
58+
"prometheus.io/port": "8888",
59+
"prometheus.io/scrape": "true",
60+
"operator.opentelemetry.io/prometheus-annotations-added": "true",
6061
}
6162
assert.Equal(t, expectedAnnotations, ss.Spec.Template.Annotations)
6263

@@ -237,6 +238,7 @@ func TestStatefulSetPodAnnotations(t *testing.T) {
237238
"prometheus.io/path": "/metrics",
238239
"prometheus.io/port": "8888",
239240
"prometheus.io/scrape": "true",
241+
"operator.opentelemetry.io/prometheus-annotations-added": "true",
240242
}
241243
// verify
242244
assert.Equal(t, "my-instance-collector", ss.Name)

internal/manifests/manifestutils/annotations.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import (
1111
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
1212
)
1313

14+
// PrometheusAnnotationsAddedKey is the pod-template annotation the operator
15+
// stamps when it adds the default prometheus.io/* scrape annotations. The
16+
// mutate path uses its presence on an existing resource to decide whether the
17+
// operator is the owner of the prometheus.io/* annotations and may therefore
18+
// remove them when DisablePrometheusAnnotations is toggled to true. This
19+
// avoids clobbering prometheus.io/* annotations that the user set out of band.
20+
const PrometheusAnnotationsAddedKey = "operator.opentelemetry.io/prometheus-annotations-added"
21+
1422
// Annotations return the annotations for OpenTelemetryCollector resources.
1523
func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
1624
// new map every time, so that we don't touch the instance's annotations
@@ -59,11 +67,21 @@ func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations [
5967
"prometheus.io/path": "/metrics",
6068
}
6169
// Default Prometheus annotations do not override existing
70+
stamped := false
6271
for kMeta, vMeta := range prometheusAnnotations {
6372
if _, ok := podAnnotations[kMeta]; !ok {
6473
podAnnotations[kMeta] = vMeta
74+
stamped = true
6575
}
6676
}
77+
// Stamp a marker only when the operator actually added at least one
78+
// default prometheus.io/* annotation. The mutate path uses the marker
79+
// to distinguish operator-stamped prom annotations from prom
80+
// annotations the user supplied out of band. Both are removed together
81+
// when DisablePrometheusAnnotations is toggled to true.
82+
if stamped {
83+
podAnnotations[PrometheusAnnotationsAddedKey] = "true"
84+
}
6785
}
6886

6987
// make sure sha256 for configMap is always calculated

0 commit comments

Comments
 (0)