Skip to content

Commit

Permalink
Support changing pod collector annotations (open-telemetry#451)
Browse files Browse the repository at this point in the history
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 open-telemetry#426
  • Loading branch information
indrekj authored and hero committed Dec 12, 2021
1 parent 76423b2 commit bd5f83c
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/collector/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions pkg/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions pkg/collector/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/collector/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions pkg/collector/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/targetallocator/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
21 changes: 21 additions & 0 deletions pkg/targetallocator/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions tests/e2e/smoke-pod-annotations/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions tests/e2e/smoke-pod-annotations/00-install.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bd5f83c

Please sign in to comment.