From bde5c7f3561325d1f968f2bf847e1379ca14d765 Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Fri, 13 Aug 2021 06:22:19 +0530 Subject: [PATCH] Allow running daemonset in hostNetwork mode --- api/v1alpha1/opentelemetrycollector_types.go | 5 +++++ ...telemetry-operator.clusterserviceversion.yaml | 4 ++-- ...opentelemetry.io_opentelemetrycollectors.yaml | 4 ++++ ...opentelemetry.io_opentelemetrycollectors.yaml | 4 ++++ docs/otelcol_cr_spec.md | 3 +++ pkg/collector/daemonset.go | 1 + pkg/collector/daemonset_test.go | 16 ++++++++++++++++ 7 files changed, 35 insertions(+), 2 deletions(-) diff --git a/api/v1alpha1/opentelemetrycollector_types.go b/api/v1alpha1/opentelemetrycollector_types.go index d8528fdb39..e74e76c4ae 100644 --- a/api/v1alpha1/opentelemetrycollector_types.go +++ b/api/v1alpha1/opentelemetrycollector_types.go @@ -56,6 +56,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-operator.clusterserviceversion.yaml b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml index cfa252920e..90f7ac682f 100644 --- a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml @@ -206,7 +206,7 @@ spec: - --enable-leader-election command: - /manager - image: quay.io/opentelemetry/opentelemetry-operator:v0.31.0 + image: controller name: manager ports: - containerPort: 9443 @@ -298,7 +298,7 @@ spec: maturity: alpha provider: name: OpenTelemetry Community - version: 0.31.0 + version: 0.29.0-27-g7027b86 webhookdefinitions: - admissionReviewVersions: - v1 diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index 817fa2573d..caf823a567 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 13ef39a70b..d985b584f6 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) +}