Skip to content

Commit a6394e4

Browse files
authored
feat: add envFrom to collector spec (open-telemetry#419)
* feat: add `envFrom` to collector spec * chore: add docs and tests
1 parent 7ff0412 commit a6394e4

7 files changed

+122
-0
lines changed

api/v1alpha1/opentelemetrycollector_types.go

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ type OpenTelemetryCollectorSpec struct {
103103
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
104104
Env []v1.EnvVar `json:"env,omitempty"`
105105

106+
// List of sources to populate environment variables on the OpenTelemetry Collector's Pods.
107+
// These can then in certain cases be consumed in the config file for the Collector.
108+
// +optional
109+
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
110+
EnvFrom []v1.EnvFromSource `json:"envFrom,omitempty"`
111+
106112
// Resources to set on the OpenTelemetry Collector pods.
107113
// +optional
108114
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true

api/v1alpha1/zz_generated.deepcopy.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,41 @@ spec:
182182
- name
183183
type: object
184184
type: array
185+
envFrom:
186+
description: List of sources to populate environment variables on
187+
the OpenTelemetry Collector's Pods. These can then in certain cases
188+
be consumed in the config file for the Collector.
189+
items:
190+
description: EnvFromSource represents the source of a set of ConfigMaps
191+
properties:
192+
configMapRef:
193+
description: The ConfigMap to select from
194+
properties:
195+
name:
196+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
197+
TODO: Add other useful fields. apiVersion, kind, uid?'
198+
type: string
199+
optional:
200+
description: Specify whether the ConfigMap must be defined
201+
type: boolean
202+
type: object
203+
prefix:
204+
description: An optional identifier to prepend to each key in
205+
the ConfigMap. Must be a C_IDENTIFIER.
206+
type: string
207+
secretRef:
208+
description: The Secret to select from
209+
properties:
210+
name:
211+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
212+
TODO: Add other useful fields. apiVersion, kind, uid?'
213+
type: string
214+
optional:
215+
description: Specify whether the Secret must be defined
216+
type: boolean
217+
type: object
218+
type: object
219+
type: array
185220
hostNetwork:
186221
description: HostNetwork indicates if the pod should run in the host
187222
networking namespace.

config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,41 @@ spec:
170170
- name
171171
type: object
172172
type: array
173+
envFrom:
174+
description: List of sources to populate environment variables on
175+
the OpenTelemetry Collector's Pods. These can then in certain cases
176+
be consumed in the config file for the Collector.
177+
items:
178+
description: EnvFromSource represents the source of a set of ConfigMaps
179+
properties:
180+
configMapRef:
181+
description: The ConfigMap to select from
182+
properties:
183+
name:
184+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
185+
TODO: Add other useful fields. apiVersion, kind, uid?'
186+
type: string
187+
optional:
188+
description: Specify whether the ConfigMap must be defined
189+
type: boolean
190+
type: object
191+
prefix:
192+
description: An optional identifier to prepend to each key in
193+
the ConfigMap. Must be a C_IDENTIFIER.
194+
type: string
195+
secretRef:
196+
description: The Secret to select from
197+
properties:
198+
name:
199+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
200+
TODO: Add other useful fields. apiVersion, kind, uid?'
201+
type: string
202+
optional:
203+
description: Specify whether the Secret must be defined
204+
type: boolean
205+
type: object
206+
type: object
207+
type: array
173208
hostNetwork:
174209
description: HostNetwork indicates if the pod should run in the host
175210
networking namespace.

docs/otelcol_cr_spec.md

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ spec:
6262
// consumed in the config file for the Collector.
6363
env: []
6464
65+
// +optional List of sources to populate environment variables on the OpenTelemetry Collector's Pods.
66+
// These can then in certain cases be consumed in the config file for the Collector.
67+
envFrom: []
68+
6569
// +optional Resources to set on the OpenTelemetry Collector pods.
6670
resources: {}
6771

pkg/collector/container.go

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
8484
VolumeMounts: volumeMounts,
8585
Args: args,
8686
Env: envVars,
87+
EnvFrom: otelcol.Spec.EnvFrom,
8788
Resources: otelcol.Spec.Resources,
8889
SecurityContext: otelcol.Spec.SecurityContext,
8990
}

pkg/collector/container_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,37 @@ func TestContainerImagePullPolicy(t *testing.T) {
239239
// verify
240240
assert.Equal(t, c.ImagePullPolicy, corev1.PullIfNotPresent)
241241
}
242+
243+
func TestContainerEnvFrom(t *testing.T) {
244+
//prepare
245+
envFrom1 := corev1.EnvFromSource{
246+
SecretRef: &corev1.SecretEnvSource{
247+
LocalObjectReference: corev1.LocalObjectReference{
248+
Name: "env-as-secret",
249+
},
250+
},
251+
}
252+
envFrom2 := corev1.EnvFromSource{
253+
ConfigMapRef: &corev1.ConfigMapEnvSource{
254+
LocalObjectReference: corev1.LocalObjectReference{
255+
Name: "env-as-configmap",
256+
},
257+
},
258+
}
259+
otelcol := v1alpha1.OpenTelemetryCollector{
260+
Spec: v1alpha1.OpenTelemetryCollectorSpec{
261+
EnvFrom: []corev1.EnvFromSource{
262+
envFrom1,
263+
envFrom2,
264+
},
265+
},
266+
}
267+
cfg := config.New()
268+
269+
// test
270+
c := Container(cfg, logger, otelcol)
271+
272+
// verify
273+
assert.Contains(t, c.EnvFrom, envFrom1)
274+
assert.Contains(t, c.EnvFrom, envFrom2)
275+
}

0 commit comments

Comments
 (0)