Skip to content

Commit 1573cd9

Browse files
authored
Optional annotations (#2586)
* feat: Adding bool flag for pod annotations * docs: Adding required docs * nit: Adding optional kubebuilder validation comment and adding changelog
1 parent 8eadb99 commit 1573cd9

File tree

7 files changed

+96
-4
lines changed

7 files changed

+96
-4
lines changed

.chloggen/optional-annotations.yaml

+16
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: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
5+
component: operator, 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: Adding a feature flag to disable default prometheus annotations
9+
10+
# One or more tracking issues related to the change
11+
issues: [2554]
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:

apis/v1alpha1/opentelemetrycollector_types.go

+6
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,12 @@ type MetricsConfigSpec struct {
521521
// +kubebuilder:validation:Optional
522522
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Create ServiceMonitors for OpenTelemetry Collector"
523523
EnableMetrics bool `json:"enableMetrics,omitempty"`
524+
// DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations
525+
// ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
526+
//
527+
// +optional
528+
// +kubebuilder:validation:Optional
529+
DisablePrometheusAnnotations bool `json:"DisablePrometheusAnnotations,omitempty"`
524530
}
525531

526532
// ObservabilitySpec defines how telemetry data gets handled.

bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -3867,6 +3867,11 @@ spec:
38673867
metrics:
38683868
description: Metrics defines the metrics configuration for operands.
38693869
properties:
3870+
DisablePrometheusAnnotations:
3871+
description: DisablePrometheusAnnotations controls the automatic
3872+
addition of default Prometheus annotations ('prometheus.io/scrape',
3873+
'prometheus.io/port', and 'prometheus.io/path')
3874+
type: boolean
38703875
enableMetrics:
38713876
description: EnableMetrics specifies if ServiceMonitor or
38723877
PodMonitor(for sidecar mode) should be created for the service
@@ -5205,6 +5210,11 @@ spec:
52055210
description: Metrics defines the metrics configuration for
52065211
operands.
52075212
properties:
5213+
DisablePrometheusAnnotations:
5214+
description: DisablePrometheusAnnotations controls the
5215+
automatic addition of default Prometheus annotations
5216+
('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
5217+
type: boolean
52085218
enableMetrics:
52095219
description: EnableMetrics specifies if ServiceMonitor
52105220
or PodMonitor(for sidecar mode) should be created for

config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -3864,6 +3864,11 @@ spec:
38643864
metrics:
38653865
description: Metrics defines the metrics configuration for operands.
38663866
properties:
3867+
DisablePrometheusAnnotations:
3868+
description: DisablePrometheusAnnotations controls the automatic
3869+
addition of default Prometheus annotations ('prometheus.io/scrape',
3870+
'prometheus.io/port', and 'prometheus.io/path')
3871+
type: boolean
38673872
enableMetrics:
38683873
description: EnableMetrics specifies if ServiceMonitor or
38693874
PodMonitor(for sidecar mode) should be created for the service
@@ -5202,6 +5207,11 @@ spec:
52025207
description: Metrics defines the metrics configuration for
52035208
operands.
52045209
properties:
5210+
DisablePrometheusAnnotations:
5211+
description: DisablePrometheusAnnotations controls the
5212+
automatic addition of default Prometheus annotations
5213+
('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
5214+
type: boolean
52055215
enableMetrics:
52065216
description: EnableMetrics specifies if ServiceMonitor
52075217
or PodMonitor(for sidecar mode) should be created for

docs/api.md

+14
Original file line numberDiff line numberDiff line change
@@ -17812,6 +17812,13 @@ Metrics defines the metrics configuration for operands.
1781217812
</tr>
1781317813
</thead>
1781417814
<tbody><tr>
17815+
<td><b>DisablePrometheusAnnotations</b></td>
17816+
<td>boolean</td>
17817+
<td>
17818+
DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')<br/>
17819+
</td>
17820+
<td>false</td>
17821+
</tr><tr>
1781517822
<td><b>enableMetrics</b></td>
1781617823
<td>boolean</td>
1781717824
<td>
@@ -20315,6 +20322,13 @@ Metrics defines the metrics configuration for operands.
2031520322
</tr>
2031620323
</thead>
2031720324
<tbody><tr>
20325+
<td><b>DisablePrometheusAnnotations</b></td>
20326+
<td>boolean</td>
20327+
<td>
20328+
DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')<br/>
20329+
</td>
20330+
<td>false</td>
20331+
</tr><tr>
2031820332
<td><b>enableMetrics</b></td>
2031920333
<td>boolean</td>
2032020334
<td>

internal/manifests/collector/annotations.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
2626
// new map every time, so that we don't touch the instance's annotations
2727
annotations := map[string]string{}
2828

29-
// set default prometheus annotations
30-
annotations["prometheus.io/scrape"] = "true"
31-
annotations["prometheus.io/port"] = "8888"
32-
annotations["prometheus.io/path"] = "/metrics"
29+
// Enable Prometheus annotations by default if DisablePrometheusAnnotations is nil or true
30+
if !instance.Spec.Observability.Metrics.DisablePrometheusAnnotations {
31+
// Set default Prometheus annotations
32+
annotations["prometheus.io/scrape"] = "true"
33+
annotations["prometheus.io/port"] = "8888"
34+
annotations["prometheus.io/path"] = "/metrics"
35+
}
3336

3437
// allow override of prometheus annotations
3538
if nil != instance.Annotations {

internal/manifests/collector/annotations_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ func TestDefaultAnnotations(t *testing.T) {
5151
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
5252
}
5353

54+
func TestNonDefaultPodAnnotation(t *testing.T) {
55+
// prepare
56+
otelcol := v1alpha1.OpenTelemetryCollector{
57+
ObjectMeta: metav1.ObjectMeta{
58+
Name: "my-instance",
59+
Namespace: "my-ns",
60+
},
61+
Spec: v1alpha1.OpenTelemetryCollectorSpec{
62+
Config: "test",
63+
Observability: v1alpha1.ObservabilitySpec{
64+
Metrics: v1alpha1.MetricsConfigSpec{
65+
DisablePrometheusAnnotations: true,
66+
},
67+
},
68+
},
69+
}
70+
71+
// test
72+
annotations := Annotations(otelcol)
73+
podAnnotations := PodAnnotations(otelcol)
74+
75+
//verify
76+
assert.NotContains(t, annotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist")
77+
assert.NotContains(t, annotations, "prometheus.io/port", "Prometheus port annotation should not exist")
78+
assert.NotContains(t, annotations, "prometheus.io/path", "Prometheus path annotation should not exist")
79+
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"])
80+
//verify propagation from metadata.annotations to spec.template.spec.metadata.annotations
81+
assert.NotContains(t, podAnnotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist in pod annotations")
82+
assert.NotContains(t, podAnnotations, "prometheus.io/port", "Prometheus port annotation should not exist in pod annotations")
83+
assert.NotContains(t, podAnnotations, "prometheus.io/path", "Prometheus path annotation should not exist in pod annotations")
84+
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
85+
}
86+
5487
func TestUserAnnotations(t *testing.T) {
5588
// prepare
5689
otelcol := v1alpha1.OpenTelemetryCollector{

0 commit comments

Comments
 (0)