From 3d76ba47ee9117646f21c29f1454063fd3930dfa Mon Sep 17 00:00:00 2001 From: Todd Yan <38701187+Toaddyan@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:16:48 -0800 Subject: [PATCH] Optional annotations (#2586) * feat: Adding bool flag for pod annotations * docs: Adding required docs * nit: Adding optional kubebuilder validation comment and adding changelog --- .chloggen/optional-annotations.yaml | 16 +++++++++ apis/v1alpha1/opentelemetrycollector_types.go | 6 ++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 10 ++++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 10 ++++++ docs/api.md | 14 ++++++++ internal/manifests/collector/annotations.go | 11 ++++--- .../manifests/collector/annotations_test.go | 33 +++++++++++++++++++ 7 files changed, 96 insertions(+), 4 deletions(-) create mode 100755 .chloggen/optional-annotations.yaml diff --git a/.chloggen/optional-annotations.yaml b/.chloggen/optional-annotations.yaml new file mode 100755 index 0000000000..9c2dff4327 --- /dev/null +++ b/.chloggen/optional-annotations.yaml @@ -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: diff --git a/apis/v1alpha1/opentelemetrycollector_types.go b/apis/v1alpha1/opentelemetrycollector_types.go index 22e9d8b68c..48bad7dd53 100644 --- a/apis/v1alpha1/opentelemetrycollector_types.go +++ b/apis/v1alpha1/opentelemetrycollector_types.go @@ -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. diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index d156b4ae2a..c143aa9318 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -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 @@ -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 diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index 6bc7ab7a5e..40cc76ab39 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -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 @@ -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 diff --git a/docs/api.md b/docs/api.md index 77094268c2..8fb054e25b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -17812,6 +17812,13 @@ Metrics defines the metrics configuration for operands. + DisablePrometheusAnnotations + boolean + + DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
+ + false + enableMetrics boolean @@ -20315,6 +20322,13 @@ Metrics defines the metrics configuration for operands. + DisablePrometheusAnnotations + boolean + + DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
+ + false + enableMetrics boolean diff --git a/internal/manifests/collector/annotations.go b/internal/manifests/collector/annotations.go index 95d25f71d6..e3f39c773c 100644 --- a/internal/manifests/collector/annotations.go +++ b/internal/manifests/collector/annotations.go @@ -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 { diff --git a/internal/manifests/collector/annotations_test.go b/internal/manifests/collector/annotations_test.go index ade8f7fbab..ddac166ff2 100644 --- a/internal/manifests/collector/annotations_test.go +++ b/internal/manifests/collector/annotations_test.go @@ -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{