From a6394e422912c759c0bde9470e3deccb60bd402c Mon Sep 17 00:00:00 2001 From: Charles Tison Date: Mon, 4 Oct 2021 11:20:02 +0200 Subject: [PATCH] feat: add `envFrom` to collector spec (#419) * feat: add `envFrom` to collector spec * chore: add docs and tests --- api/v1alpha1/opentelemetrycollector_types.go | 6 ++++ api/v1alpha1/zz_generated.deepcopy.go | 7 ++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 35 +++++++++++++++++++ ...ntelemetry.io_opentelemetrycollectors.yaml | 35 +++++++++++++++++++ docs/otelcol_cr_spec.md | 4 +++ pkg/collector/container.go | 1 + pkg/collector/container_test.go | 34 ++++++++++++++++++ 7 files changed, 122 insertions(+) diff --git a/api/v1alpha1/opentelemetrycollector_types.go b/api/v1alpha1/opentelemetrycollector_types.go index eff35f8422..ff94b8d7eb 100644 --- a/api/v1alpha1/opentelemetrycollector_types.go +++ b/api/v1alpha1/opentelemetrycollector_types.go @@ -103,6 +103,12 @@ type OpenTelemetryCollectorSpec struct { // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true Env []v1.EnvVar `json:"env,omitempty"` + // List of sources to populate environment variables on the OpenTelemetry Collector's Pods. + // These can then in certain cases be consumed in the config file for the Collector. + // +optional + // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true + EnvFrom []v1.EnvFromSource `json:"envFrom,omitempty"` + // Resources to set on the OpenTelemetry Collector pods. // +optional // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 091428561c..1b0dd399fb 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -139,6 +139,13 @@ func (in *OpenTelemetryCollectorSpec) DeepCopyInto(out *OpenTelemetryCollectorSp (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.EnvFrom != nil { + in, out := &in.EnvFrom, &out.EnvFrom + *out = make([]v1.EnvFromSource, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } in.Resources.DeepCopyInto(&out.Resources) if in.Tolerations != nil { in, out := &in.Tolerations, &out.Tolerations diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index e30f13fbb7..ff6ab740ed 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -182,6 +182,41 @@ spec: - name type: object type: array + envFrom: + description: List of sources to populate environment variables on + the OpenTelemetry Collector's Pods. These can then in certain cases + be consumed in the config file for the Collector. + items: + description: EnvFromSource represents the source of a set of ConfigMaps + properties: + configMapRef: + description: The ConfigMap to select from + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + optional: + description: Specify whether the ConfigMap must be defined + type: boolean + type: object + prefix: + description: An optional identifier to prepend to each key in + the ConfigMap. Must be a C_IDENTIFIER. + type: string + secretRef: + description: The Secret to select from + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + optional: + description: Specify whether the Secret must be defined + type: boolean + type: object + type: object + type: array hostNetwork: description: HostNetwork indicates if the pod should run in the host networking namespace. diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index 3fc5dc89ad..ea627cba03 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -170,6 +170,41 @@ spec: - name type: object type: array + envFrom: + description: List of sources to populate environment variables on + the OpenTelemetry Collector's Pods. These can then in certain cases + be consumed in the config file for the Collector. + items: + description: EnvFromSource represents the source of a set of ConfigMaps + properties: + configMapRef: + description: The ConfigMap to select from + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + optional: + description: Specify whether the ConfigMap must be defined + type: boolean + type: object + prefix: + description: An optional identifier to prepend to each key in + the ConfigMap. Must be a C_IDENTIFIER. + type: string + secretRef: + description: The Secret to select from + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + optional: + description: Specify whether the Secret must be defined + type: boolean + type: object + type: object + type: array hostNetwork: description: HostNetwork indicates if the pod should run in the host networking namespace. diff --git a/docs/otelcol_cr_spec.md b/docs/otelcol_cr_spec.md index fd59381a44..434d69f3f3 100644 --- a/docs/otelcol_cr_spec.md +++ b/docs/otelcol_cr_spec.md @@ -62,6 +62,10 @@ spec: // consumed in the config file for the Collector. env: [] + // +optional List of sources to populate environment variables on the OpenTelemetry Collector's Pods. + // These can then in certain cases be consumed in the config file for the Collector. + envFrom: [] + // +optional Resources to set on the OpenTelemetry Collector pods. resources: {} diff --git a/pkg/collector/container.go b/pkg/collector/container.go index 2a984550e1..17db543898 100644 --- a/pkg/collector/container.go +++ b/pkg/collector/container.go @@ -84,6 +84,7 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem VolumeMounts: volumeMounts, Args: args, Env: envVars, + EnvFrom: otelcol.Spec.EnvFrom, Resources: otelcol.Spec.Resources, SecurityContext: otelcol.Spec.SecurityContext, } diff --git a/pkg/collector/container_test.go b/pkg/collector/container_test.go index 750ae767c6..2ec9e8f61c 100644 --- a/pkg/collector/container_test.go +++ b/pkg/collector/container_test.go @@ -239,3 +239,37 @@ func TestContainerImagePullPolicy(t *testing.T) { // verify assert.Equal(t, c.ImagePullPolicy, corev1.PullIfNotPresent) } + +func TestContainerEnvFrom(t *testing.T) { + //prepare + envFrom1 := corev1.EnvFromSource{ + SecretRef: &corev1.SecretEnvSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: "env-as-secret", + }, + }, + } + envFrom2 := corev1.EnvFromSource{ + ConfigMapRef: &corev1.ConfigMapEnvSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: "env-as-configmap", + }, + }, + } + otelcol := v1alpha1.OpenTelemetryCollector{ + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + EnvFrom: []corev1.EnvFromSource{ + envFrom1, + envFrom2, + }, + }, + } + cfg := config.New() + + // test + c := Container(cfg, logger, otelcol) + + // verify + assert.Contains(t, c.EnvFrom, envFrom1) + assert.Contains(t, c.EnvFrom, envFrom2) +}