Skip to content

Commit

Permalink
Optional annotations (#2586)
Browse files Browse the repository at this point in the history
* feat: Adding bool flag for pod annotations

* docs: Adding required docs

* nit: Adding optional kubebuilder validation comment and adding changelog
  • Loading branch information
Toaddyan authored Jan 31, 2024
1 parent 21a2af9 commit 3d76ba4
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .chloggen/optional-annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator, target allocator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adding a feature flag to disable default prometheus annotations

# One or more tracking issues related to the change
issues: [2554]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
6 changes: 6 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,12 @@ type MetricsConfigSpec struct {
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Create ServiceMonitors for OpenTelemetry Collector"
EnableMetrics bool `json:"enableMetrics,omitempty"`
// DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations
// ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
//
// +optional
// +kubebuilder:validation:Optional
DisablePrometheusAnnotations bool `json:"DisablePrometheusAnnotations,omitempty"`
}

// ObservabilitySpec defines how telemetry data gets handled.
Expand Down
10 changes: 10 additions & 0 deletions bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3867,6 +3867,11 @@ spec:
metrics:
description: Metrics defines the metrics configuration for operands.
properties:
DisablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the automatic
addition of default Prometheus annotations ('prometheus.io/scrape',
'prometheus.io/port', and 'prometheus.io/path')
type: boolean
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor or
PodMonitor(for sidecar mode) should be created for the service
Expand Down Expand Up @@ -5205,6 +5210,11 @@ spec:
description: Metrics defines the metrics configuration for
operands.
properties:
DisablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the
automatic addition of default Prometheus annotations
('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
type: boolean
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor
or PodMonitor(for sidecar mode) should be created for
Expand Down
10 changes: 10 additions & 0 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3864,6 +3864,11 @@ spec:
metrics:
description: Metrics defines the metrics configuration for operands.
properties:
DisablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the automatic
addition of default Prometheus annotations ('prometheus.io/scrape',
'prometheus.io/port', and 'prometheus.io/path')
type: boolean
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor or
PodMonitor(for sidecar mode) should be created for the service
Expand Down Expand Up @@ -5202,6 +5207,11 @@ spec:
description: Metrics defines the metrics configuration for
operands.
properties:
DisablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the
automatic addition of default Prometheus annotations
('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
type: boolean
enableMetrics:
description: EnableMetrics specifies if ServiceMonitor
or PodMonitor(for sidecar mode) should be created for
Expand Down
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17812,6 +17812,13 @@ Metrics defines the metrics configuration for operands.
</tr>
</thead>
<tbody><tr>
<td><b>DisablePrometheusAnnotations</b></td>
<td>boolean</td>
<td>
DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>enableMetrics</b></td>
<td>boolean</td>
<td>
Expand Down Expand Up @@ -20315,6 +20322,13 @@ Metrics defines the metrics configuration for operands.
</tr>
</thead>
<tbody><tr>
<td><b>DisablePrometheusAnnotations</b></td>
<td>boolean</td>
<td>
DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>enableMetrics</b></td>
<td>boolean</td>
<td>
Expand Down
11 changes: 7 additions & 4 deletions internal/manifests/collector/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
// new map every time, so that we don't touch the instance's annotations
annotations := map[string]string{}

// set default prometheus annotations
annotations["prometheus.io/scrape"] = "true"
annotations["prometheus.io/port"] = "8888"
annotations["prometheus.io/path"] = "/metrics"
// Enable Prometheus annotations by default if DisablePrometheusAnnotations is nil or true
if !instance.Spec.Observability.Metrics.DisablePrometheusAnnotations {
// Set default Prometheus annotations
annotations["prometheus.io/scrape"] = "true"
annotations["prometheus.io/port"] = "8888"
annotations["prometheus.io/path"] = "/metrics"
}

// allow override of prometheus annotations
if nil != instance.Annotations {
Expand Down
33 changes: 33 additions & 0 deletions internal/manifests/collector/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@ func TestDefaultAnnotations(t *testing.T) {
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
}

func TestNonDefaultPodAnnotation(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "my-ns",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Config: "test",
Observability: v1alpha1.ObservabilitySpec{
Metrics: v1alpha1.MetricsConfigSpec{
DisablePrometheusAnnotations: true,
},
},
},
}

// test
annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(otelcol)

//verify
assert.NotContains(t, annotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist")
assert.NotContains(t, annotations, "prometheus.io/port", "Prometheus port annotation should not exist")
assert.NotContains(t, annotations, "prometheus.io/path", "Prometheus path annotation should not exist")
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"])
//verify propagation from metadata.annotations to spec.template.spec.metadata.annotations
assert.NotContains(t, podAnnotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist in pod annotations")
assert.NotContains(t, podAnnotations, "prometheus.io/port", "Prometheus port annotation should not exist in pod annotations")
assert.NotContains(t, podAnnotations, "prometheus.io/path", "Prometheus path annotation should not exist in pod annotations")
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
}

func TestUserAnnotations(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
Expand Down

0 comments on commit 3d76ba4

Please sign in to comment.