Skip to content

Commit

Permalink
Support changing pod collector annotations
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 #426
  • Loading branch information
indrekj committed Oct 5, 2021
1 parent 53281c7 commit efad706
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 4 deletions.
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 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 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)
}

0 comments on commit efad706

Please sign in to comment.