From efad7067c17550c99f65709d28ce5e68e1ee0965 Mon Sep 17 00:00:00 2001 From: Indrek Juhkam Date: Tue, 5 Oct 2021 15:11:14 +0300 Subject: [PATCH] Support changing pod collector annotations This allows setting podAnnotations, e.g: ``` --- apiVersion: opentelemetry.io/v1alpha1 kind: OpenTelemetryCollector metadata: name: opentelemetry annotations: foo: "this won't be applied to pods" spec: mode: deployment podAnnotations: ad.datadoghq.com/otc-container.check_names: '["openmetrics"]' ad.datadoghq.com/otc-container.init_configs: '[{}]' ad.datadoghq.com/otc-container.instances: |- [ { "prometheus_url": "http://%%host%%:8888/metrics", "namespace": "opentelemetry.collector", "metrics": [ {"otelcol_exporter_queue_size": "exporter.queue_size"}, {"otelcol_exporter_send_failed_spans": "exporter.send_failed_spans"}, {"otelcol_exporter_sent_spans": "exporter.sent_spans"}, ] } ] # ... ``` Fixes #426 --- api/v1alpha1/opentelemetrycollector_types.go | 6 ++++++ api/v1alpha1/zz_generated.deepcopy.go | 7 +++++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 6 ++++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 6 ++++++ pkg/collector/daemonset.go | 2 +- pkg/collector/daemonset_test.go | 21 +++++++++++++++++++ pkg/collector/deployment.go | 2 +- pkg/collector/deployment_test.go | 21 +++++++++++++++++++ pkg/collector/statefulset.go | 2 +- pkg/collector/statefulset_test.go | 21 +++++++++++++++++++ pkg/targetallocator/deployment.go | 2 +- pkg/targetallocator/deployment_test.go | 21 +++++++++++++++++++ 12 files changed, 113 insertions(+), 4 deletions(-) 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..c44d76d745 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 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..30e25670ad 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 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) +}