From 87912b5e330d6195574833a18210344e89aad001 Mon Sep 17 00:00:00 2001 From: Kenneth Huddleston Date: Fri, 11 Oct 2024 13:47:18 -0700 Subject: [PATCH] [nr-k8s-otel-collector] - exposing pod EVs to configuration in values file (#1489) #### Is this a new chart No. This is a modification to 'nr-otel-for-k8s', which is currently maintained by @csongnr and @dbudziwojskiNR #### What this PR does / why we need it: This PR exposes the option to add additional pod EVs to the deployment and daemonset running on kubernetes. I ran into a use case where I needed to be able to customize the 'nr-otel-for-k8s' service with a custom collector config, but once I did so I was unable to securely pass values to that config. In order to securely pass values to a custom config, having access from the values file to provide additional EVs is helpful. #### Checklist - [x] Chart Version bumped - [x] Variables are documented in the README.md - [x] Title of the PR starts with chart name (e.g. `[mychartname]`) --------- Co-authored-by: Daniel Budziwojski --- charts/nr-k8s-otel-collector/Chart.yaml | 2 +- charts/nr-k8s-otel-collector/README.md | 5 +- .../templates/daemonset.yaml | 7 ++ .../templates/deployment.yaml | 7 ++ .../tests/env_vars_from_test.yaml | 114 ++++++++++++++++++ .../tests/env_vars_test.yaml | 94 +++++++++++++++ charts/nr-k8s-otel-collector/values.yaml | 8 ++ 7 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 charts/nr-k8s-otel-collector/tests/env_vars_from_test.yaml create mode 100644 charts/nr-k8s-otel-collector/tests/env_vars_test.yaml diff --git a/charts/nr-k8s-otel-collector/Chart.yaml b/charts/nr-k8s-otel-collector/Chart.yaml index 59930423a..a5319206a 100644 --- a/charts/nr-k8s-otel-collector/Chart.yaml +++ b/charts/nr-k8s-otel-collector/Chart.yaml @@ -17,7 +17,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.7.6 +version: 0.7.7 dependencies: - name: common-library diff --git a/charts/nr-k8s-otel-collector/README.md b/charts/nr-k8s-otel-collector/README.md index 08c697d5d..748efb670 100644 --- a/charts/nr-k8s-otel-collector/README.md +++ b/charts/nr-k8s-otel-collector/README.md @@ -87,6 +87,8 @@ daemonset: | daemonset.configMap | object | See `values.yaml` | Settings for daemonset configmap | | daemonset.configMap.config | object | `{}` | OpenTelemetry config for the daemonset. If set, overrides default config and disables configuration parameters for the daemonset. | | daemonset.containerSecurityContext | object | `{"privileged":true}` | Sets security context (at container level) for the daemonset. Overrides `containerSecurityContext` and `global.containerSecurityContext` | +| daemonset.envs | list | `[]` | Sets additional environment variables for the daemonset. | +| daemonset.envsFrom | list | `[]` | Sets additional environment variable sources for the daemonset. | | daemonset.nodeSelector | object | `{}` | Sets daemonset pod node selector. Overrides `nodeSelector` and `global.nodeSelector` | | daemonset.podAnnotations | object | `{}` | Annotations to be added to the daemonset. | | daemonset.podSecurityContext | object | `{}` | Sets security context (at pod level) for the daemonset. Overrides `podSecurityContext` and `global.podSecurityContext` | @@ -96,6 +98,8 @@ daemonset: | deployment.configMap | object | See `values.yaml` | Settings for deployment configmap | | deployment.configMap.config | object | `{}` | OpenTelemetry config for the deployment. If set, overrides default config and disables configuration parameters for the deployment. | | deployment.containerSecurityContext | object | `{}` | Sets security context (at container level) for the deployment. Overrides `containerSecurityContext` and `global.containerSecurityContext` | +| deployment.envs | list | `[]` | Sets additional environment variables for the deployment. | +| deployment.envsFrom | list | `[]` | Sets additional environment variable sources for the deployment. | | deployment.nodeSelector | object | `{}` | Sets deployment pod node selector. Overrides `nodeSelector` and `global.nodeSelector` | | deployment.podAnnotations | object | `{}` | Annotations to be added to the deployment. | | deployment.podSecurityContext | object | `{}` | Sets security context (at pod level) for the deployment. Overrides `podSecurityContext` and `global.podSecurityContext` | @@ -163,6 +167,5 @@ Error scraping metrics {"kind": "receiver", "name": "hostmetrics", "data_type": ## Maintainers -* [juanjjaramillo](https://github.com/juanjjaramillo) * [csongnr](https://github.com/csongnr) * [dbudziwojskiNR](https://github.com/dbudziwojskiNR) diff --git a/charts/nr-k8s-otel-collector/templates/daemonset.yaml b/charts/nr-k8s-otel-collector/templates/daemonset.yaml index c6c79deec..396eeda26 100644 --- a/charts/nr-k8s-otel-collector/templates/daemonset.yaml +++ b/charts/nr-k8s-otel-collector/templates/daemonset.yaml @@ -44,6 +44,10 @@ spec: args: ["--config", "/config/daemonset-config.yaml"] resources: {{- toYaml .Values.daemonset.resources | nindent 12 }} + {{- with .Values.daemonset.envsFrom }} + envFrom: + {{- . | toYaml | nindent 12 }} + {{- end }} env: - name: HOST_IP valueFrom: @@ -71,6 +75,9 @@ spec: secretKeyRef: name: {{ include "newrelic.common.license.secretName" . }} key: {{ include "newrelic.common.license.secretKeyName" . }} + {{- with .Values.daemonset.envs }} + {{- . | toYaml | nindent 12 }} + {{- end }} volumeMounts: # TODO (chris): this is a linux specific configuration {{- if include "newrelic.common.privileged" . }} diff --git a/charts/nr-k8s-otel-collector/templates/deployment.yaml b/charts/nr-k8s-otel-collector/templates/deployment.yaml index 50583406d..c088d6db3 100644 --- a/charts/nr-k8s-otel-collector/templates/deployment.yaml +++ b/charts/nr-k8s-otel-collector/templates/deployment.yaml @@ -46,6 +46,10 @@ spec: args: ["--config", "/config/deployment-config.yaml"] resources: {{- toYaml .Values.deployment.resources | nindent 12 }} + {{- with .Values.deployment.envsFrom }} + envFrom: + {{- . | toYaml | nindent 12 }} + {{- end }} env: - name: KUBE_NODE_NAME valueFrom: @@ -62,6 +66,9 @@ spec: secretKeyRef: name: {{ include "newrelic.common.license.secretName" . }} key: {{ include "newrelic.common.license.secretKeyName" . }} + {{- with .Values.deployment.envs }} + {{- . | toYaml | nindent 12 }} + {{- end }} ports: - name: http containerPort: 4318 diff --git a/charts/nr-k8s-otel-collector/tests/env_vars_from_test.yaml b/charts/nr-k8s-otel-collector/tests/env_vars_from_test.yaml new file mode 100644 index 000000000..1f4e6b2a2 --- /dev/null +++ b/charts/nr-k8s-otel-collector/tests/env_vars_from_test.yaml @@ -0,0 +1,114 @@ +suite: envVarsFrom +templates: + - templates/deployment.yaml + - templates/deployment-configmap.yaml + - templates/daemonset.yaml + - templates/daemonset-configmap.yaml +release: + name: my-release + namespace: my-namespace +tests: + - it: does not set envFrom by default + set: + cluster: my-cluster + licenseKey: us-whatever + asserts: + - notExists: + path: spec.template.spec.containers[0].envFrom + template: templates/deployment.yaml + - notExists: + path: spec.template.spec.containers[0].envFrom + template: templates/daemonset.yaml + - it: adds additional deployment env sources from Values file using 'nr-otel-for-k8s-license' secret + set: + cluster: my-cluster + licenseKey: us-whatever + deployment: + envsFrom: + - secretRef: + name: test-secret-dep + - configMapRef: + name: test-configmap-dep + asserts: + - contains: + path: spec.template.spec.containers[0].envFrom + content: + secretRef: + name: test-secret-dep + template: templates/deployment.yaml + - contains: + path: spec.template.spec.containers[0].envFrom + content: + configMapRef: + name: test-configmap-dep + template: templates/deployment.yaml + - notExists: + path: spec.template.spec.containers[0].envFrom + template: templates/daemonset.yaml + - it: adds additional daemonset env sources from Values file using 'nr-otel-for-k8s-license' secret + set: + cluster: my-cluster + licenseKey: us-whatever + daemonset: + envsFrom: + - secretRef: + name: test-secret-dae + - configMapRef: + name: test-configmap-dae + asserts: + - contains: + path: spec.template.spec.containers[0].envFrom + content: + secretRef: + name: test-secret-dae + template: templates/daemonset.yaml + - contains: + path: spec.template.spec.containers[0].envFrom + content: + configMapRef: + name: test-configmap-dae + template: templates/daemonset.yaml + - notExists: + path: spec.template.spec.containers[0].envFrom + template: templates/deployment.yaml + - it: adds additional deployment and daemonset env sources from Values file using 'nr-otel-for-k8s-license' secret + set: + cluster: my-cluster + licenseKey: us-whatever + deployment: + envsFrom: + - secretRef: + name: test-secret-dep + - configMapRef: + name: test-configmap-dep + daemonset: + envsFrom: + - secretRef: + name: test-secret-dae + - configMapRef: + name: test-configmap-dae + asserts: + - contains: + path: spec.template.spec.containers[0].envFrom + content: + secretRef: + name: test-secret-dep + template: templates/deployment.yaml + - contains: + path: spec.template.spec.containers[0].envFrom + content: + configMapRef: + name: test-configmap-dep + template: templates/deployment.yaml + - contains: + path: spec.template.spec.containers[0].envFrom + content: + secretRef: + name: test-secret-dae + template: templates/daemonset.yaml + - contains: + path: spec.template.spec.containers[0].envFrom + content: + configMapRef: + name: test-configmap-dae + template: templates/daemonset.yaml \ No newline at end of file diff --git a/charts/nr-k8s-otel-collector/tests/env_vars_test.yaml b/charts/nr-k8s-otel-collector/tests/env_vars_test.yaml new file mode 100644 index 000000000..cdf1984aa --- /dev/null +++ b/charts/nr-k8s-otel-collector/tests/env_vars_test.yaml @@ -0,0 +1,94 @@ +suite: envVars +templates: + - templates/deployment.yaml + - templates/deployment-configmap.yaml + - templates/daemonset.yaml + - templates/daemonset-configmap.yaml +release: + name: my-release + namespace: my-namespace +tests: + - it: does not set pod environment variables by default + set: + cluster: my-cluster + licenseKey: us-whatever + asserts: + - lengthEqual: + path: spec.template.spec.containers[0].env + count: 3 + template: templates/deployment.yaml + - lengthEqual: + path: spec.template.spec.containers[0].env + count: 7 + template: templates/daemonset.yaml + - it: sets deployment pod environment variables from values + set: + cluster: my-cluster + licenseKey: us-whatever + deployment: + envs: + - name: "TEST_DEP_ENV_VAR" + value: "my-dep-value" + asserts: + - lengthEqual: + path: spec.template.spec.containers[0].env + count: 4 + template: templates/deployment.yaml + - contains: + path: spec.template.spec.containers[0].env + content: + name: "TEST_DEP_ENV_VAR" + value: "my-dep-value" + template: templates/deployment.yaml + - it: sets daemonset pod environment variables from values + set: + cluster: my-cluster + licenseKey: us-whatever + daemonset: + envs: + - name: "TEST_DAE_ENV_VAR" + value: "my-dae-value" + asserts: + - lengthEqual: + path: spec.template.spec.containers[0].env + count: 8 + template: templates/daemonset.yaml + - contains: + path: spec.template.spec.containers[0].env + content: + name: "TEST_DAE_ENV_VAR" + value: "my-dae-value" + template: templates/daemonset.yaml + - it: sets deployment and daemonset pod environment variables from values + set: + cluster: my-cluster + licenseKey: us-whatever + deployment: + envs: + - name: "TEST_DEP_ENV_VAR" + value: "my-dep-value" + daemonset: + envs: + - name: "TEST_DAE_ENV_VAR" + value: "my-dae-value" + asserts: + - lengthEqual: + path: spec.template.spec.containers[0].env + count: 4 + template: templates/deployment.yaml + - contains: + path: spec.template.spec.containers[0].env + content: + name: "TEST_DEP_ENV_VAR" + value: "my-dep-value" + template: templates/deployment.yaml + - lengthEqual: + path: spec.template.spec.containers[0].env + count: 8 + template: templates/daemonset.yaml + - contains: + path: spec.template.spec.containers[0].env + content: + name: "TEST_DAE_ENV_VAR" + value: "my-dae-value" + template: templates/daemonset.yaml \ No newline at end of file diff --git a/charts/nr-k8s-otel-collector/values.yaml b/charts/nr-k8s-otel-collector/values.yaml index a296909a4..055723f67 100644 --- a/charts/nr-k8s-otel-collector/values.yaml +++ b/charts/nr-k8s-otel-collector/values.yaml @@ -60,6 +60,10 @@ daemonset: privileged: true # -- Sets resources for the daemonset. resources: {} + # -- Sets additional environment variables for the daemonset. + envs: [] + # -- Sets additional environment variable sources for the daemonset. + envsFrom: [] # -- Settings for daemonset configmap # @default -- See `values.yaml` configMap: @@ -81,6 +85,10 @@ deployment: containerSecurityContext: {} # -- Sets resources for the deployment. resources: {} + # -- Sets additional environment variables for the deployment. + envs: [] + # -- Sets additional environment variable sources for the deployment. + envsFrom: [] # -- Settings for deployment configmap # @default -- See `values.yaml` configMap: