diff --git a/CHANGELOG.md b/CHANGELOG.md index 53cc4e4c90..e512c3fcc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Changes by Version ================== +Unreleased +------------------- +* Allow changing Pod annotations using `podAnnotations` ([#451](https://github.com/open-telemetry/opentelemetry-operator/pull/451), [@indrekj](https://github.com/indrekj)) + 0.35.0 ------------------- * Bumped OpenTelemetry Collector to v0.35.0 diff --git a/api/v1alpha1/opentelemetrycollector_types.go b/api/v1alpha1/opentelemetrycollector_types.go index ff94b8d7eb..05c557f345 100644 --- a/api/v1alpha1/opentelemetrycollector_types.go +++ b/api/v1alpha1/opentelemetrycollector_types.go @@ -119,6 +119,12 @@ type OpenTelemetryCollectorSpec struct { // +optional // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true Tolerations []v1.Toleration `json:"tolerations,omitempty"` + + // PodAnnotations is the set of annotations that will be attached to + // Collector and Target Allocator pods. + // +optional + // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true + PodAnnotations map[string]string `json:"podAnnotations,omitempty"` } // OpenTelemetryCollectorStatus defines the observed state of OpenTelemetryCollector. diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 1b0dd399fb..c3fe7b0b3e 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -154,6 +154,13 @@ func (in *OpenTelemetryCollectorSpec) DeepCopyInto(out *OpenTelemetryCollectorSp (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.PodAnnotations != nil { + in, out := &in.PodAnnotations, &out.PodAnnotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenTelemetryCollectorSpec. diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index ff6ab740ed..663a9b354e 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -238,6 +238,12 @@ spec: - sidecar - statefulset type: string + podAnnotations: + additionalProperties: + type: string + description: PodAnnotations is the set of annotations that will be + attached to Collector and Target Allocator pods. + type: object ports: description: Ports allows a set of ports to be exposed by the underlying v1.Service. By default, the operator will attempt to infer the required diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index ea627cba03..6f91cf34e5 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -226,6 +226,12 @@ spec: - sidecar - statefulset type: string + podAnnotations: + additionalProperties: + type: string + description: PodAnnotations is the set of annotations that will be + attached to Collector and Target Allocator pods. + type: object ports: description: Ports allows a set of ports to be exposed by the underlying v1.Service. By default, the operator will attempt to infer the required diff --git a/pkg/collector/daemonset.go b/pkg/collector/daemonset.go index 719e1e6af8..0a85b7ad00 100644 --- a/pkg/collector/daemonset.go +++ b/pkg/collector/daemonset.go @@ -46,7 +46,7 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, - Annotations: otelcol.Annotations, + Annotations: otelcol.Spec.PodAnnotations, }, Spec: corev1.PodSpec{ ServiceAccountName: ServiceAccountName(otelcol), diff --git a/pkg/collector/daemonset_test.go b/pkg/collector/daemonset_test.go index 35791574a9..ce236707a1 100644 --- a/pkg/collector/daemonset_test.go +++ b/pkg/collector/daemonset_test.go @@ -72,3 +72,24 @@ func TestDaemonsetHostNetwork(t *testing.T) { }) assert.True(t, d2.Spec.Template.Spec.HostNetwork) } + +func TestDaemonsetPodAnnotations(t *testing.T) { + // prepare + testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"} + otelcol := v1alpha1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + }, + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + PodAnnotations: testPodAnnotationValues, + }, + } + cfg := config.New() + + // test + ds := DaemonSet(cfg, logger, otelcol) + + // verify + assert.Equal(t, "my-instance-collector", ds.Name) + assert.Equal(t, testPodAnnotationValues, ds.Spec.Template.Annotations) +} diff --git a/pkg/collector/deployment.go b/pkg/collector/deployment.go index 4d79a2b1cd..f0508218a8 100644 --- a/pkg/collector/deployment.go +++ b/pkg/collector/deployment.go @@ -47,7 +47,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, - Annotations: otelcol.Annotations, + Annotations: otelcol.Spec.PodAnnotations, }, Spec: corev1.PodSpec{ ServiceAccountName: ServiceAccountName(otelcol), diff --git a/pkg/collector/deployment_test.go b/pkg/collector/deployment_test.go index 1cfe13594e..6bc08e377f 100644 --- a/pkg/collector/deployment_test.go +++ b/pkg/collector/deployment_test.go @@ -65,3 +65,24 @@ func TestDeploymentNewDefault(t *testing.T) { // the pod selector should match the pod spec's labels assert.Equal(t, d.Spec.Template.Labels, d.Spec.Selector.MatchLabels) } + +func TestDeploymentPodAnnotations(t *testing.T) { + // prepare + testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"} + otelcol := v1alpha1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + }, + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + PodAnnotations: testPodAnnotationValues, + }, + } + cfg := config.New() + + // test + d := Deployment(cfg, logger, otelcol) + + // verify + assert.Equal(t, "my-instance-collector", d.Name) + assert.Equal(t, testPodAnnotationValues, d.Spec.Template.Annotations) +} diff --git a/pkg/collector/statefulset.go b/pkg/collector/statefulset.go index 925dc66c18..5751b0fcf1 100644 --- a/pkg/collector/statefulset.go +++ b/pkg/collector/statefulset.go @@ -47,7 +47,7 @@ func StatefulSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTel Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, - Annotations: otelcol.Annotations, + Annotations: otelcol.Spec.PodAnnotations, }, Spec: corev1.PodSpec{ ServiceAccountName: ServiceAccountName(otelcol), diff --git a/pkg/collector/statefulset_test.go b/pkg/collector/statefulset_test.go index 4262a62026..3723596969 100644 --- a/pkg/collector/statefulset_test.go +++ b/pkg/collector/statefulset_test.go @@ -123,3 +123,24 @@ func TestStatefulSetVolumeClaimTemplates(t *testing.T) { // assert correct pvc storage assert.Equal(t, resource.MustParse("1Gi"), ss.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests["storage"]) } + +func TestStatefulSetPodAnnotations(t *testing.T) { + // prepare + testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"} + otelcol := v1alpha1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + }, + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + PodAnnotations: testPodAnnotationValues, + }, + } + cfg := config.New() + + // test + ss := StatefulSet(cfg, logger, otelcol) + + // verify + assert.Equal(t, "my-instance-collector", ss.Name) + assert.Equal(t, testPodAnnotationValues, ss.Spec.Template.Annotations) +} diff --git a/pkg/targetallocator/deployment.go b/pkg/targetallocator/deployment.go index d05052dae3..1b79b9d3c8 100644 --- a/pkg/targetallocator/deployment.go +++ b/pkg/targetallocator/deployment.go @@ -46,7 +46,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, - Annotations: otelcol.Annotations, + Annotations: otelcol.Spec.PodAnnotations, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{Container(cfg, logger, otelcol)}, diff --git a/pkg/targetallocator/deployment_test.go b/pkg/targetallocator/deployment_test.go index 302c78ac74..11e2b05db1 100644 --- a/pkg/targetallocator/deployment_test.go +++ b/pkg/targetallocator/deployment_test.go @@ -48,3 +48,24 @@ func TestDeploymentNewDefault(t *testing.T) { // the pod selector should match the pod spec's labels assert.Equal(t, d.Spec.Template.Labels, d.Spec.Selector.MatchLabels) } + +func TestDeploymentPodAnnotations(t *testing.T) { + // prepare + testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"} + otelcol := v1alpha1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + }, + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + PodAnnotations: testPodAnnotationValues, + }, + } + cfg := config.New() + + // test + ds := Deployment(cfg, logger, otelcol) + + // verify + assert.Equal(t, "my-instance-targetallocator", ds.Name) + assert.Equal(t, testPodAnnotationValues, ds.Spec.Template.Annotations) +} diff --git a/tests/e2e/smoke-pod-annotations/00-assert.yaml b/tests/e2e/smoke-pod-annotations/00-assert.yaml new file mode 100644 index 0000000000..a8d1af4fc3 --- /dev/null +++ b/tests/e2e/smoke-pod-annotations/00-assert.yaml @@ -0,0 +1,14 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: pa-collector + annotations: + regular-annotation: regular-value +spec: + template: + metadata: + annotations: + pod-annotation1: value1 + pod-annotation2: value2 +status: + readyReplicas: 1 diff --git a/tests/e2e/smoke-pod-annotations/00-install.yaml b/tests/e2e/smoke-pod-annotations/00-install.yaml new file mode 100644 index 0000000000..6313db8871 --- /dev/null +++ b/tests/e2e/smoke-pod-annotations/00-install.yaml @@ -0,0 +1,29 @@ +apiVersion: opentelemetry.io/v1alpha1 +kind: OpenTelemetryCollector +metadata: + name: pa + annotations: + regular-annotation: regular-value +spec: + podAnnotations: + pod-annotation1: value1 + pod-annotation2: value2 + config: | + receivers: + jaeger: + protocols: + grpc: + processors: + + exporters: + logging: + + service: + pipelines: + traces: + receivers: [jaeger] + processors: [] + exporters: [logging] + args: + metrics-level: detailed + log-level: debug