diff --git a/.chloggen/3151-feat-annotations-filter.yaml b/.chloggen/3151-feat-annotations-filter.yaml new file mode 100644 index 0000000000..c8d8133838 --- /dev/null +++ b/.chloggen/3151-feat-annotations-filter.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. collector, target allocator, auto-instrumentation, opamp, github action) +component: collector + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Configmap, rbac, service, serviceaccount apply annotations-filter." + +# One or more tracking issues related to the change +issues: [3151] + +# (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/controllers/builder_test.go b/controllers/builder_test.go index 3735b3c76c..d6ec935023 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -133,12 +133,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", - }, + Annotations: map[string]string{}, }, Spec: appsv1.DeploymentSpec{ Replicas: &one, @@ -239,7 +234,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, Data: map[string]string{ "collector.yaml": "receivers:\n examplereceiver:\n endpoint: 0.0.0.0:12345\nexporters:\n logging: null\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - examplereceiver\n", @@ -257,7 +252,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -273,7 +268,7 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -330,7 +325,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -384,12 +379,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", - }, + Annotations: map[string]string{}, }, Spec: appsv1.DeploymentSpec{ Replicas: &one, @@ -490,7 +480,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, Data: map[string]string{ "collector.yaml": "receivers:\n examplereceiver:\n endpoint: 0.0.0.0:12345\nexporters:\n logging: null\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - examplereceiver\n", @@ -508,7 +498,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -524,7 +514,7 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -581,7 +571,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -671,12 +661,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", - }, + Annotations: map[string]string{}, }, Spec: appsv1.DeploymentSpec{ Replicas: &one, @@ -777,7 +762,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, Data: map[string]string{ "collector.yaml": "receivers:\n examplereceiver:\n endpoint: 0.0.0.0:12345\nexporters:\n logging: null\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - examplereceiver\n", @@ -796,7 +781,7 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -853,7 +838,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -1201,12 +1186,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", - }, + Annotations: map[string]string{}, }, Spec: appsv1.StatefulSetSpec{ ServiceName: "test-collector", @@ -1308,7 +1288,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, Data: map[string]string{ "collector.yaml": "exporters:\n logging: null\nreceivers:\n prometheus:\n config: {}\n target_allocator:\n collector_id: ${POD_NAME}\n endpoint: http://test-targetallocator:80\n interval: 30s\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - prometheus\n", @@ -1326,7 +1306,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -1343,7 +1323,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -1597,12 +1577,7 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", - }, + Annotations: map[string]string{}, }, Spec: appsv1.StatefulSetSpec{ ServiceName: "test-collector", @@ -1704,7 +1679,7 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, Data: map[string]string{ "collector.yaml": "exporters:\n logging: null\nreceivers:\n prometheus:\n config: {}\n target_allocator:\n collector_id: ${POD_NAME}\n endpoint: http://test-targetallocator:80\n interval: 30s\nservice:\n pipelines:\n metrics:\n exporters:\n - logging\n receivers:\n - prometheus\n", @@ -1722,7 +1697,7 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -1739,7 +1714,7 @@ prometheus_cr: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index 38205f0be9..20e5e0822b 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -172,7 +172,9 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { exists, err = populateObjectIfExists(t, sa, namespacedObjectName(naming.ServiceAccount(params.Name), params.Namespace)) assert.NoError(t, err) assert.True(t, exists) - assert.Equal(t, map[string]string{annotationName: "true"}, sa.Annotations) + assert.Equal(t, map[string]string{ + annotationName: "true", + }, sa.Annotations) saPatch := sa.DeepCopy() saPatch.Annotations["user-defined-annotation"] = "value" err = k8sClient.Patch(ctx, saPatch, client.MergeFrom(sa)) @@ -213,7 +215,11 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { exists, err = populateObjectIfExists(t, sa, namespacedObjectName(naming.ServiceAccount(params.Name), params.Namespace)) assert.NoError(t, err) assert.True(t, exists) - assert.Equal(t, map[string]string{annotationName: "true", "user-defined-annotation": "value", "new-annotation": "new-value"}, sa.Annotations) + assert.Equal(t, map[string]string{ + annotationName: "true", + "user-defined-annotation": "value", + "new-annotation": "new-value", + }, sa.Annotations) }, }, wantErr: assert.NoError, diff --git a/internal/manifests/collector/configmap.go b/internal/manifests/collector/configmap.go index 934dd2fe36..54362549ad 100644 --- a/internal/manifests/collector/configmap.go +++ b/internal/manifests/collector/configmap.go @@ -32,6 +32,11 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { collectorName := naming.Collector(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, collectorName, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return nil, err + } + replacedConf, err := ReplaceConfig(params.OtelCol, params.TargetAllocator) if err != nil { params.Log.V(2).Info("failed to update prometheus config to use sharded targets: ", "err", err) @@ -43,7 +48,7 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { Name: name, Namespace: params.OtelCol.Namespace, Labels: labels, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, }, Data: map[string]string{ "collector.yaml": replacedConf, diff --git a/internal/manifests/collector/daemonset_test.go b/internal/manifests/collector/daemonset_test.go index 82e42c739c..7b52066224 100644 --- a/internal/manifests/collector/daemonset_test.go +++ b/internal/manifests/collector/daemonset_test.go @@ -55,9 +55,6 @@ func TestDaemonSetNewDefault(t *testing.T) { // verify assert.Equal(t, "my-instance-collector", d.Name) assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", d.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", d.Annotations["prometheus.io/path"]) assert.Equal(t, testTolerationValues, d.Spec.Template.Spec.Tolerations) assert.Len(t, d.Spec.Template.Spec.Containers, 1) @@ -269,7 +266,7 @@ func TestDaemonsetFilterAnnotations(t *testing.T) { d, err := DaemonSet(params) require.NoError(t, err) - assert.Len(t, d.ObjectMeta.Annotations, 4) + assert.Len(t, d.ObjectMeta.Annotations, 0) for k := range excludedAnnotations { assert.NotContains(t, d.ObjectMeta.Annotations, k) } @@ -443,9 +440,9 @@ func TestDaemonSetInitContainer(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-instance-collector", d.Name) assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", d.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", d.Annotations["prometheus.io/path"]) + assert.Equal(t, "true", d.Spec.Template.Annotations["prometheus.io/scrape"]) + assert.Equal(t, "8888", d.Spec.Template.Annotations["prometheus.io/port"]) + assert.Equal(t, "/metrics", d.Spec.Template.Annotations["prometheus.io/path"]) assert.Len(t, d.Spec.Template.Spec.InitContainers, 1) } @@ -479,9 +476,9 @@ func TestDaemonSetAdditionalContainer(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-instance-collector", d.Name) assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", d.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", d.Annotations["prometheus.io/path"]) + assert.Equal(t, "true", d.Spec.Template.Annotations["prometheus.io/scrape"]) + assert.Equal(t, "8888", d.Spec.Template.Annotations["prometheus.io/port"]) + assert.Equal(t, "/metrics", d.Spec.Template.Annotations["prometheus.io/path"]) assert.Len(t, d.Spec.Template.Spec.Containers, 2) assert.Equal(t, v1.Container{Name: "test"}, d.Spec.Template.Spec.Containers[0]) } diff --git a/internal/manifests/collector/deployment_test.go b/internal/manifests/collector/deployment_test.go index e4922913a6..c36ec5852e 100644 --- a/internal/manifests/collector/deployment_test.go +++ b/internal/manifests/collector/deployment_test.go @@ -97,9 +97,6 @@ func TestDeploymentNewDefault(t *testing.T) { // verify assert.Equal(t, "my-instance-collector", d.Name) assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", d.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", d.Annotations["prometheus.io/path"]) assert.Equal(t, testTolerationValues, d.Spec.Template.Spec.Tolerations) assert.Len(t, d.Spec.Template.Spec.Containers, 1) @@ -351,7 +348,7 @@ func TestDeploymentFilterAnnotations(t *testing.T) { d, err := Deployment(params) require.NoError(t, err) - assert.Len(t, d.ObjectMeta.Annotations, 4) + assert.Len(t, d.ObjectMeta.Annotations, 0) for k := range excludedAnnotations { assert.NotContains(t, d.ObjectMeta.Annotations, k) } @@ -571,9 +568,9 @@ func TestDeploymentSetInitContainer(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-instance-collector", d.Name) assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", d.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", d.Annotations["prometheus.io/path"]) + assert.Equal(t, "true", d.Spec.Template.Annotations["prometheus.io/scrape"]) + assert.Equal(t, "8888", d.Spec.Template.Annotations["prometheus.io/port"]) + assert.Equal(t, "/metrics", d.Spec.Template.Annotations["prometheus.io/path"]) assert.Len(t, d.Spec.Template.Spec.InitContainers, 1) } @@ -654,9 +651,9 @@ func TestDeploymentAdditionalContainers(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-instance-collector", d.Name) assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", d.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", d.Annotations["prometheus.io/path"]) + assert.Equal(t, "true", d.Spec.Template.Annotations["prometheus.io/scrape"]) + assert.Equal(t, "8888", d.Spec.Template.Annotations["prometheus.io/port"]) + assert.Equal(t, "/metrics", d.Spec.Template.Annotations["prometheus.io/path"]) assert.Len(t, d.Spec.Template.Spec.Containers, 2) assert.Equal(t, v1.Container{Name: "test"}, d.Spec.Template.Spec.Containers[0]) } diff --git a/internal/manifests/collector/rbac.go b/internal/manifests/collector/rbac.go index 25bad9dcb1..70bac31c75 100644 --- a/internal/manifests/collector/rbac.go +++ b/internal/manifests/collector/rbac.go @@ -44,10 +44,15 @@ func ClusterRole(params manifests.Params) (*rbacv1.ClusterRole, error) { name := naming.ClusterRole(params.OtelCol.Name, params.OtelCol.Namespace) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter()) + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return nil, err + } + return &rbacv1.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, Labels: labels, }, Rules: rules, @@ -73,10 +78,15 @@ func ClusterRoleBinding(params manifests.Params) (*rbacv1.ClusterRoleBinding, er name := naming.ClusterRoleBinding(params.OtelCol.Name, params.OtelCol.Namespace) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter()) + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return nil, err + } + return &rbacv1.ClusterRoleBinding{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, Labels: labels, }, Subjects: []rbacv1.Subject{ diff --git a/internal/manifests/collector/service.go b/internal/manifests/collector/service.go index 2cf7616732..2747d7560f 100644 --- a/internal/manifests/collector/service.go +++ b/internal/manifests/collector/service.go @@ -78,6 +78,11 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) { labels[monitoringLabel] = valueExists labels[serviceTypeLabel] = MonitoringServiceType.String() + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return nil, err + } + metricsPort, err := params.OtelCol.Spec.Config.Service.MetricsPort() if err != nil { return nil, err @@ -88,7 +93,7 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) { Name: name, Namespace: params.OtelCol.Namespace, Labels: labels, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, }, Spec: corev1.ServiceSpec{ Selector: manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector), @@ -106,6 +111,11 @@ func Service(params manifests.Params) (*corev1.Service, error) { labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) labels[serviceTypeLabel] = BaseServiceType.String() + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return nil, err + } + ports, err := params.OtelCol.Spec.Config.GetAllPorts(params.Log) if err != nil { return nil, err @@ -156,7 +166,7 @@ func Service(params manifests.Params) (*corev1.Service, error) { Name: naming.Service(params.OtelCol.Name), Namespace: params.OtelCol.Namespace, Labels: labels, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, }, Spec: corev1.ServiceSpec{ InternalTrafficPolicy: &trafficPolicy, diff --git a/internal/manifests/collector/service_test.go b/internal/manifests/collector/service_test.go index 7ee7dfc4a0..75e15e3c65 100644 --- a/internal/manifests/collector/service_test.go +++ b/internal/manifests/collector/service_test.go @@ -330,6 +330,11 @@ func serviceWithInternalTrafficPolicy(name string, ports []v1beta1.PortsSpec, in labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) labels[serviceTypeLabel] = BaseServiceType.String() + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return v1.Service{} + } + svcPorts := []v1.ServicePort{} for _, p := range ports { p.ServicePort.TargetPort = intstr.FromInt32(p.Port) @@ -341,7 +346,7 @@ func serviceWithInternalTrafficPolicy(name string, ports []v1beta1.PortsSpec, in Name: name, Namespace: "default", Labels: labels, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, }, Spec: v1.ServiceSpec{ InternalTrafficPolicy: &internalTrafficPolicy, diff --git a/internal/manifests/collector/serviceaccount.go b/internal/manifests/collector/serviceaccount.go index 4ad29bf696..2b8043fd21 100644 --- a/internal/manifests/collector/serviceaccount.go +++ b/internal/manifests/collector/serviceaccount.go @@ -42,12 +42,17 @@ func ServiceAccount(params manifests.Params) (*corev1.ServiceAccount, error) { name := naming.ServiceAccount(params.OtelCol.Name) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) + annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter()) + if err != nil { + return nil, err + } + return &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: params.OtelCol.Namespace, Labels: labels, - Annotations: params.OtelCol.Annotations, + Annotations: annotations, }, }, nil } diff --git a/internal/manifests/collector/statefulset_test.go b/internal/manifests/collector/statefulset_test.go index 69ad9b1649..916e25e4bb 100644 --- a/internal/manifests/collector/statefulset_test.go +++ b/internal/manifests/collector/statefulset_test.go @@ -60,9 +60,6 @@ func TestStatefulSetNewDefault(t *testing.T) { // verify assert.Equal(t, "my-instance-collector", ss.Name) assert.Equal(t, "my-instance-collector", ss.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", ss.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", ss.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", ss.Annotations["prometheus.io/path"]) assert.Equal(t, testTolerationValues, ss.Spec.Template.Spec.Tolerations) assert.Len(t, ss.Spec.Template.Spec.Containers, 1) @@ -361,7 +358,7 @@ func TestStatefulSetFilterAnnotations(t *testing.T) { d, err := StatefulSet(params) require.NoError(t, err) - assert.Len(t, d.ObjectMeta.Annotations, 4) + assert.Len(t, d.ObjectMeta.Annotations, 0) for k := range excludedAnnotations { assert.NotContains(t, d.ObjectMeta.Annotations, k) } @@ -535,9 +532,9 @@ func TestStatefulSetInitContainer(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-instance-collector", s.Name) assert.Equal(t, "my-instance-collector", s.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", s.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", s.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", s.Annotations["prometheus.io/path"]) + assert.Equal(t, "true", s.Spec.Template.Annotations["prometheus.io/scrape"]) + assert.Equal(t, "8888", s.Spec.Template.Annotations["prometheus.io/port"]) + assert.Equal(t, "/metrics", s.Spec.Template.Annotations["prometheus.io/path"]) assert.Len(t, s.Spec.Template.Spec.InitContainers, 1) } @@ -619,9 +616,9 @@ func TestStatefulSetAdditionalContainers(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-instance-collector", s.Name) assert.Equal(t, "my-instance-collector", s.Labels["app.kubernetes.io/name"]) - assert.Equal(t, "true", s.Annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", s.Annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", s.Annotations["prometheus.io/path"]) + assert.Equal(t, "true", s.Spec.Template.Annotations["prometheus.io/scrape"]) + assert.Equal(t, "8888", s.Spec.Template.Annotations["prometheus.io/port"]) + assert.Equal(t, "/metrics", s.Spec.Template.Annotations["prometheus.io/path"]) assert.Len(t, s.Spec.Template.Spec.Containers, 2) assert.Equal(t, v1.Container{Name: "test"}, s.Spec.Template.Spec.Containers[0]) } diff --git a/internal/manifests/manifestutils/annotations.go b/internal/manifests/manifestutils/annotations.go index c4243e350d..c3c52049ce 100644 --- a/internal/manifests/manifestutils/annotations.go +++ b/internal/manifests/manifestutils/annotations.go @@ -22,20 +22,11 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" ) -// Annotations return the annotations for OpenTelemetryCollector pod. +// Annotations return the annotations for OpenTelemetryCollector resources. func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) { // new map every time, so that we don't touch the instance's annotations annotations := map[string]string{} - // 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.ObjectMeta.Annotations { for k, v := range instance.ObjectMeta.Annotations { if !IsFilteredSet(k, filterAnnotations) { @@ -44,14 +35,6 @@ func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []st } } - hash, err := GetConfigMapSHA(instance.Spec.Config) - if err != nil { - return nil, err - } - - // make sure sha256 for configMap is always calculated - annotations["opentelemetry-operator-config/sha256"] = hash - return annotations, nil } @@ -78,11 +61,29 @@ func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations [ } } + // Enable Prometheus annotations by default if DisablePrometheusAnnotations is nil or true + if !instance.Spec.Observability.Metrics.DisablePrometheusAnnotations { + // Set default Prometheus annotations + prometheusAnnotations := map[string]string{ + "prometheus.io/scrape": "true", + "prometheus.io/port": "8888", + "prometheus.io/path": "/metrics", + } + // Default Prometheus annotations do not override existing + for kMeta, vMeta := range prometheusAnnotations { + if _, ok := podAnnotations[kMeta]; !ok { + podAnnotations[kMeta] = vMeta + } + } + } + + // make sure sha256 for configMap is always calculated hash, err := GetConfigMapSHA(instance.Spec.Config) if err != nil { return nil, err } - // make sure sha256 for configMap is always calculated + + // Adding the ConfigMap Hash only to PodAnnotations podAnnotations["opentelemetry-operator-config/sha256"] = hash return podAnnotations, nil diff --git a/internal/manifests/manifestutils/annotations_test.go b/internal/manifests/manifestutils/annotations_test.go index 3d1926eeae..818adff3f7 100644 --- a/internal/manifests/manifestutils/annotations_test.go +++ b/internal/manifests/manifestutils/annotations_test.go @@ -44,16 +44,9 @@ func TestDefaultAnnotations(t *testing.T) { } // test - annotations, err := Annotations(otelcol, []string{}) - require.NoError(t, err) podAnnotations, err := PodAnnotations(otelcol, []string{}) require.NoError(t, err) - //verify - assert.Equal(t, "true", annotations["prometheus.io/scrape"]) - assert.Equal(t, "8888", annotations["prometheus.io/port"]) - assert.Equal(t, "/metrics", annotations["prometheus.io/path"]) - assert.Equal(t, "5b3b62aa5e0a3c7250084c2b49190e30b72fc2ad352ffbaa699224e1aa900834", annotations["opentelemetry-operator-config/sha256"]) //verify propagation from metadata.annotations to spec.template.spec.metadata.annotations assert.Equal(t, "true", podAnnotations["prometheus.io/scrape"]) assert.Equal(t, "8888", podAnnotations["prometheus.io/port"]) @@ -87,7 +80,6 @@ func TestNonDefaultPodAnnotation(t *testing.T) { 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, "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d", 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") @@ -102,9 +94,8 @@ func TestUserAnnotations(t *testing.T) { Name: "my-instance", Namespace: "my-ns", Annotations: map[string]string{"prometheus.io/scrape": "false", - "prometheus.io/port": "1234", - "prometheus.io/path": "/test", - "opentelemetry-operator-config/sha256": "shouldBeOverwritten", + "prometheus.io/port": "1234", + "prometheus.io/path": "/test", }, }, Spec: v1beta1.OpenTelemetryCollectorSpec{ @@ -129,7 +120,6 @@ func TestUserAnnotations(t *testing.T) { assert.Equal(t, "false", annotations["prometheus.io/scrape"]) assert.Equal(t, "1234", annotations["prometheus.io/port"]) assert.Equal(t, "/test", annotations["prometheus.io/path"]) - assert.Equal(t, "29cb15a4b87f8c6284e7c3377f6b6c5c74519f5aee8ca39a90b3cf3ca2043c4d", annotations["opentelemetry-operator-config/sha256"]) assert.Equal(t, "29cb15a4b87f8c6284e7c3377f6b6c5c74519f5aee8ca39a90b3cf3ca2043c4d", podAnnotations["opentelemetry-operator-config/sha256"]) } @@ -153,7 +143,7 @@ func TestAnnotationsPropagateDown(t *testing.T) { require.NoError(t, err) // verify - assert.Len(t, annotations, 5) + assert.Len(t, annotations, 1) assert.Equal(t, "mycomponent", annotations["myapp"]) assert.Equal(t, "mycomponent", podAnnotations["myapp"]) assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"]) @@ -178,7 +168,7 @@ func TestAnnotationsFilter(t *testing.T) { // verify require.NoError(t, err) - assert.Len(t, annotations, 6) + assert.Len(t, annotations, 2) assert.NotContains(t, annotations, "test.bar.io") assert.Equal(t, "1234", annotations["test.io/port"]) }