From 214c3ca229620b50465b12e38ff51d8155c60e57 Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Thu, 19 Aug 2021 12:58:08 +0530 Subject: [PATCH] Allow running daemonset in hostNetwork mode (#393) * Allow running daemonset in hostNetwork mode * Added daemonset e2e tests --- api/v1alpha1/opentelemetrycollector_types.go | 5 +++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 4 ++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 4 ++++ docs/otelcol_cr_spec.md | 3 +++ pkg/collector/daemonset.go | 1 + pkg/collector/daemonset_test.go | 16 ++++++++++++++ tests/e2e/daemonset-features/00-assert.yaml | 14 +++++++++++++ tests/e2e/daemonset-features/00-install.yaml | 21 +++++++++++++++++++ 8 files changed, 68 insertions(+) create mode 100644 tests/e2e/daemonset-features/00-assert.yaml create mode 100644 tests/e2e/daemonset-features/00-install.yaml diff --git a/api/v1alpha1/opentelemetrycollector_types.go b/api/v1alpha1/opentelemetrycollector_types.go index 63921453a2..4df3028536 100644 --- a/api/v1alpha1/opentelemetrycollector_types.go +++ b/api/v1alpha1/opentelemetrycollector_types.go @@ -61,6 +61,11 @@ type OpenTelemetryCollectorSpec struct { // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true SecurityContext *v1.SecurityContext `json:"securityContext,omitempty"` + // HostNetwork indicates if the pod should run in the host networking namespace. + // +optional + // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true + HostNetwork bool `json:"hostNetwork,omitempty"` + // VolumeClaimTemplates will provide stable storage using PersistentVolumes. Only available when the mode=statefulset. // +optional // +listType=atomic diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index 56ac10808b..cdeb69596d 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -182,6 +182,10 @@ spec: - name type: object type: array + hostNetwork: + description: HostNetwork indicates if the pod should run in the host + networking namespace. + type: boolean image: description: Image indicates the container image to use for the OpenTelemetry Collector. diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index 462f10a997..80da912278 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -170,6 +170,10 @@ spec: - name type: object type: array + hostNetwork: + description: HostNetwork indicates if the pod should run in the host + networking namespace. + type: boolean image: description: Image indicates the container image to use for the OpenTelemetry Collector. diff --git a/docs/otelcol_cr_spec.md b/docs/otelcol_cr_spec.md index 79538808cb..fbca33397c 100644 --- a/docs/otelcol_cr_spec.md +++ b/docs/otelcol_cr_spec.md @@ -64,6 +64,9 @@ spec: // +optional SecurityContext will be set as the container security context. securityContext: {} + + // +optional HostNetwork indicates if the pod should run in the host networking namespace. + hostNetwork: false // +optional Toleration to schedule OpenTelemetry Collector pods. // This is only relevant to daemonsets, statefulsets and deployments diff --git a/pkg/collector/daemonset.go b/pkg/collector/daemonset.go index 7c981ee104..719e1e6af8 100644 --- a/pkg/collector/daemonset.go +++ b/pkg/collector/daemonset.go @@ -53,6 +53,7 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem Containers: []corev1.Container{Container(cfg, logger, otelcol)}, Volumes: Volumes(cfg, otelcol), Tolerations: otelcol.Spec.Tolerations, + HostNetwork: otelcol.Spec.HostNetwork, }, }, }, diff --git a/pkg/collector/daemonset_test.go b/pkg/collector/daemonset_test.go index 79bf9cffdd..35791574a9 100644 --- a/pkg/collector/daemonset_test.go +++ b/pkg/collector/daemonset_test.go @@ -56,3 +56,19 @@ func TestDaemonSetNewDefault(t *testing.T) { // the pod selector should match the pod spec's labels assert.Equal(t, d.Spec.Selector.MatchLabels, d.Spec.Template.Labels) } + +func TestDaemonsetHostNetwork(t *testing.T) { + // test + d1 := DaemonSet(config.New(), logger, v1alpha1.OpenTelemetryCollector{ + Spec: v1alpha1.OpenTelemetryCollectorSpec{}, + }) + assert.False(t, d1.Spec.Template.Spec.HostNetwork) + + // verify custom + d2 := DaemonSet(config.New(), logger, v1alpha1.OpenTelemetryCollector{ + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + HostNetwork: true, + }, + }) + assert.True(t, d2.Spec.Template.Spec.HostNetwork) +} diff --git a/tests/e2e/daemonset-features/00-assert.yaml b/tests/e2e/daemonset-features/00-assert.yaml new file mode 100644 index 0000000000..d862c1f6a2 --- /dev/null +++ b/tests/e2e/daemonset-features/00-assert.yaml @@ -0,0 +1,14 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: daemonset-collector +spec: + template: + spec: + hostNetwork: true + containers: + - args: + - --config=/conf/collector.yaml + name: otc-container +status: + numberReady: 1 diff --git a/tests/e2e/daemonset-features/00-install.yaml b/tests/e2e/daemonset-features/00-install.yaml new file mode 100644 index 0000000000..3dde940cf4 --- /dev/null +++ b/tests/e2e/daemonset-features/00-install.yaml @@ -0,0 +1,21 @@ +apiVersion: opentelemetry.io/v1alpha1 +kind: OpenTelemetryCollector +metadata: + name: daemonset +spec: + mode: daemonset + hostNetwork: true + config: | + receivers: + jaeger: + protocols: + grpc: + processors: + exporters: + logging: + service: + pipelines: + traces: + receivers: [jaeger] + processors: [] + exporters: [logging]