From b4941e8c3268fac25e41b80090e16444c9ece01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=84=B6?= Date: Thu, 25 Jul 2024 14:45:02 +0800 Subject: [PATCH 1/3] feat: configmap, rbac, service, serviceaccount apply annotations-filter --- controllers/builder_test.go | 86 ++++++++++++------- controllers/reconcile_test.go | 12 ++- internal/manifests/collector/configmap.go | 7 +- .../manifests/collector/daemonset_test.go | 17 ++-- .../manifests/collector/deployment_test.go | 17 ++-- internal/manifests/collector/rbac.go | 14 ++- internal/manifests/collector/service.go | 14 ++- internal/manifests/collector/service_test.go | 7 +- .../manifests/collector/serviceaccount.go | 7 +- .../manifests/collector/statefulset_test.go | 17 ++-- .../manifests/manifestutils/annotations.go | 28 +++--- .../manifestutils/annotations_test.go | 7 +- 12 files changed, 143 insertions(+), 90 deletions(-) diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 3735b3c76c..89505ad01e 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -135,9 +135,6 @@ service: }, Annotations: map[string]string{ "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", }, }, Spec: appsv1.DeploymentSpec{ @@ -239,7 +236,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, 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 +256,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, }, &corev1.Service{ @@ -273,7 +274,9 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -302,6 +305,7 @@ service: }, Annotations: map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": "test-collector-headless-tls", + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", }, }, Spec: corev1.ServiceSpec{ @@ -330,7 +334,9 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -386,9 +392,6 @@ service: }, Annotations: map[string]string{ "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", }, }, Spec: appsv1.DeploymentSpec{ @@ -490,7 +493,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, 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 +513,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, }, &corev1.Service{ @@ -524,7 +531,9 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -553,6 +562,7 @@ service: }, Annotations: map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": "test-collector-headless-tls", + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", }, }, Spec: corev1.ServiceSpec{ @@ -581,7 +591,9 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -673,9 +685,6 @@ service: }, Annotations: map[string]string{ "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", }, }, Spec: appsv1.DeploymentSpec{ @@ -777,7 +786,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, 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 +807,9 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -825,6 +838,7 @@ service: }, Annotations: map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": "test-collector-headless-tls", + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", }, }, Spec: corev1.ServiceSpec{ @@ -853,7 +867,9 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -1203,9 +1219,6 @@ service: }, Annotations: map[string]string{ "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", }, }, Spec: appsv1.StatefulSetSpec{ @@ -1308,7 +1321,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", + }, }, 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 +1341,9 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", + }, }, }, &corev1.Service{ @@ -1343,7 +1360,9 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -1599,9 +1618,6 @@ prometheus_cr: }, Annotations: map[string]string{ "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - "prometheus.io/path": "/metrics", - "prometheus.io/port": "8888", - "prometheus.io/scrape": "true", }, }, Spec: appsv1.StatefulSetSpec{ @@ -1704,7 +1720,9 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", + }, }, 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 +1740,9 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", + }, }, }, &corev1.Service{ @@ -1739,7 +1759,9 @@ prometheus_cr: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", + }, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index 38205f0be9..9fc2ff83b9 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -172,7 +172,10 @@ 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", + "opentelemetry-operator-config/sha256": "11d309a64c15c034827053656adb6be67c0885d11092dc63c976a65328bbbbe1", + }, sa.Annotations) saPatch := sa.DeepCopy() saPatch.Annotations["user-defined-annotation"] = "value" err = k8sClient.Patch(ctx, saPatch, client.MergeFrom(sa)) @@ -213,7 +216,12 @@ 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", + "opentelemetry-operator-config/sha256": "11d309a64c15c034827053656adb6be67c0885d11092dc63c976a65328bbbbe1", + }, 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..cce903bf60 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, 1) 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..c352aa99f6 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, 1) 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..529d8f47fc 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, 1) 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..6b0993d639 100644 --- a/internal/manifests/manifestutils/annotations.go +++ b/internal/manifests/manifestutils/annotations.go @@ -27,15 +27,6 @@ func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []st // 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) { @@ -78,12 +69,21 @@ func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations [ } } - hash, err := GetConfigMapSHA(instance.Spec.Config) - if err != nil { - return nil, err + // 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 - 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..4d610768a1 100644 --- a/internal/manifests/manifestutils/annotations_test.go +++ b/internal/manifests/manifestutils/annotations_test.go @@ -50,9 +50,6 @@ func TestDefaultAnnotations(t *testing.T) { 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"]) @@ -153,7 +150,7 @@ func TestAnnotationsPropagateDown(t *testing.T) { require.NoError(t, err) // verify - assert.Len(t, annotations, 5) + assert.Len(t, annotations, 2) assert.Equal(t, "mycomponent", annotations["myapp"]) assert.Equal(t, "mycomponent", podAnnotations["myapp"]) assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"]) @@ -178,7 +175,7 @@ func TestAnnotationsFilter(t *testing.T) { // verify require.NoError(t, err) - assert.Len(t, annotations, 6) + assert.Len(t, annotations, 3) assert.NotContains(t, annotations, "test.bar.io") assert.Equal(t, "1234", annotations["test.io/port"]) } From b7e757dd982830b400db133864fea51bd18b4909 Mon Sep 17 00:00:00 2001 From: Alan Wang Date: Thu, 25 Jul 2024 22:52:32 +0800 Subject: [PATCH 2/3] chore: add change log entry --- .chloggen/3151-feat-annotations-filter.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .chloggen/3151-feat-annotations-filter.yaml 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: From e8e568ae1516031cadd831fc9313e4910ac2e678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=84=B6?= Date: Tue, 30 Jul 2024 09:17:36 +0800 Subject: [PATCH 3/3] feat: Adding the ConfigMap Hash only to PodAnnotations --- controllers/builder_test.go | 91 +++++-------------- controllers/reconcile_test.go | 10 +- .../manifests/collector/daemonset_test.go | 2 +- .../manifests/collector/deployment_test.go | 2 +- .../manifests/collector/statefulset_test.go | 2 +- .../manifests/manifestutils/annotations.go | 19 ++-- .../manifestutils/annotations_test.go | 15 +-- 7 files changed, 43 insertions(+), 98 deletions(-) diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 89505ad01e..d6ec935023 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -133,9 +133,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: appsv1.DeploymentSpec{ Replicas: &one, @@ -236,9 +234,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + 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", @@ -256,9 +252,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -274,9 +268,7 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -305,7 +297,6 @@ service: }, Annotations: map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": "test-collector-headless-tls", - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", }, }, Spec: corev1.ServiceSpec{ @@ -334,9 +325,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -390,9 +379,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: appsv1.DeploymentSpec{ Replicas: &one, @@ -493,9 +480,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + 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", @@ -513,9 +498,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -531,9 +514,7 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -562,7 +543,6 @@ service: }, Annotations: map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": "test-collector-headless-tls", - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", }, }, Spec: corev1.ServiceSpec{ @@ -591,9 +571,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -683,9 +661,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: appsv1.DeploymentSpec{ Replicas: &one, @@ -786,9 +762,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + 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", @@ -807,9 +781,7 @@ service: "app.kubernetes.io/version": "latest", "operator.opentelemetry.io/collector-service-type": "base", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -838,7 +810,6 @@ service: }, Annotations: map[string]string{ "service.beta.openshift.io/serving-cert-secret-name": "test-collector-headless-tls", - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", }, }, Spec: corev1.ServiceSpec{ @@ -867,9 +838,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "6f6f11da374b2c1e42fc78fbe55e2d9bcc2f5998ab63a631b49c478e8c0f6af8", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -1217,9 +1186,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + Annotations: map[string]string{}, }, Spec: appsv1.StatefulSetSpec{ ServiceName: "test-collector", @@ -1321,9 +1288,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + 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", @@ -1341,9 +1306,7 @@ service: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -1360,9 +1323,7 @@ service: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -1616,9 +1577,7 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + Annotations: map[string]string{}, }, Spec: appsv1.StatefulSetSpec{ ServiceName: "test-collector", @@ -1720,9 +1679,7 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + 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", @@ -1740,9 +1697,7 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + Annotations: map[string]string{}, }, }, &corev1.Service{ @@ -1759,9 +1714,7 @@ prometheus_cr: "operator.opentelemetry.io/collector-service-type": "monitoring", "operator.opentelemetry.io/collector-monitoring-service": "Exists", }, - Annotations: map[string]string{ - "opentelemetry-operator-config/sha256": "39cae697770f9d7e183e8fa9ba56043315b62e19c7231537870acfaaabc30a43", - }, + Annotations: map[string]string{}, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index 9fc2ff83b9..20e5e0822b 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -173,8 +173,7 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { assert.NoError(t, err) assert.True(t, exists) assert.Equal(t, map[string]string{ - annotationName: "true", - "opentelemetry-operator-config/sha256": "11d309a64c15c034827053656adb6be67c0885d11092dc63c976a65328bbbbe1", + annotationName: "true", }, sa.Annotations) saPatch := sa.DeepCopy() saPatch.Annotations["user-defined-annotation"] = "value" @@ -217,10 +216,9 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { assert.NoError(t, err) assert.True(t, exists) assert.Equal(t, map[string]string{ - annotationName: "true", - "user-defined-annotation": "value", - "new-annotation": "new-value", - "opentelemetry-operator-config/sha256": "11d309a64c15c034827053656adb6be67c0885d11092dc63c976a65328bbbbe1", + annotationName: "true", + "user-defined-annotation": "value", + "new-annotation": "new-value", }, sa.Annotations) }, }, diff --git a/internal/manifests/collector/daemonset_test.go b/internal/manifests/collector/daemonset_test.go index cce903bf60..7b52066224 100644 --- a/internal/manifests/collector/daemonset_test.go +++ b/internal/manifests/collector/daemonset_test.go @@ -266,7 +266,7 @@ func TestDaemonsetFilterAnnotations(t *testing.T) { d, err := DaemonSet(params) require.NoError(t, err) - assert.Len(t, d.ObjectMeta.Annotations, 1) + assert.Len(t, d.ObjectMeta.Annotations, 0) for k := range excludedAnnotations { assert.NotContains(t, d.ObjectMeta.Annotations, k) } diff --git a/internal/manifests/collector/deployment_test.go b/internal/manifests/collector/deployment_test.go index c352aa99f6..c36ec5852e 100644 --- a/internal/manifests/collector/deployment_test.go +++ b/internal/manifests/collector/deployment_test.go @@ -348,7 +348,7 @@ func TestDeploymentFilterAnnotations(t *testing.T) { d, err := Deployment(params) require.NoError(t, err) - assert.Len(t, d.ObjectMeta.Annotations, 1) + assert.Len(t, d.ObjectMeta.Annotations, 0) for k := range excludedAnnotations { assert.NotContains(t, d.ObjectMeta.Annotations, k) } diff --git a/internal/manifests/collector/statefulset_test.go b/internal/manifests/collector/statefulset_test.go index 529d8f47fc..916e25e4bb 100644 --- a/internal/manifests/collector/statefulset_test.go +++ b/internal/manifests/collector/statefulset_test.go @@ -358,7 +358,7 @@ func TestStatefulSetFilterAnnotations(t *testing.T) { d, err := StatefulSet(params) require.NoError(t, err) - assert.Len(t, d.ObjectMeta.Annotations, 1) + assert.Len(t, d.ObjectMeta.Annotations, 0) for k := range excludedAnnotations { assert.NotContains(t, d.ObjectMeta.Annotations, k) } diff --git a/internal/manifests/manifestutils/annotations.go b/internal/manifests/manifestutils/annotations.go index 6b0993d639..c3c52049ce 100644 --- a/internal/manifests/manifestutils/annotations.go +++ b/internal/manifests/manifestutils/annotations.go @@ -22,7 +22,7 @@ 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{} @@ -35,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 } @@ -85,6 +77,15 @@ func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations [ } } + // make sure sha256 for configMap is always calculated + hash, err := GetConfigMapSHA(instance.Spec.Config) + if err != nil { + return nil, err + } + + // 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 4d610768a1..818adff3f7 100644 --- a/internal/manifests/manifestutils/annotations_test.go +++ b/internal/manifests/manifestutils/annotations_test.go @@ -44,13 +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, "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"]) @@ -84,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") @@ -99,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{ @@ -126,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"]) } @@ -150,7 +143,7 @@ func TestAnnotationsPropagateDown(t *testing.T) { require.NoError(t, err) // verify - assert.Len(t, annotations, 2) + 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"]) @@ -175,7 +168,7 @@ func TestAnnotationsFilter(t *testing.T) { // verify require.NoError(t, err) - assert.Len(t, annotations, 3) + assert.Len(t, annotations, 2) assert.NotContains(t, annotations, "test.bar.io") assert.Equal(t, "1234", annotations["test.io/port"]) }