Skip to content

Commit

Permalink
feat(env) add support for envFrom (#987)
Browse files Browse the repository at this point in the history
Add envFrom and ingressController.envFrom values. These populate the
standard container envFrom field, which contains a list of ConfigMap or
Secret refs. Keys and values from the referenced resources are set as
environment variables in the containers.
  • Loading branch information
rainest authored Jan 19, 2024
1 parent 61c07d7 commit 5e7c7ce
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 2 deletions.
10 changes: 10 additions & 0 deletions charts/kong/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased

### Added

* The `envFrom` and `ingressController.envFrom` values.yaml keys now populate
the container field of the same name. This loads environment variables from
ConfigMap or Secret resource keys in bulk:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables
[#987](https://github.com/Kong/charts/pull/987)

## 2.33.3

### Fixed
Expand Down
6 changes: 4 additions & 2 deletions charts/kong/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,11 @@ directory.
| image.effectiveSemver | Semantic version to use for version-dependent features (if `tag` is not a semver) | |
| image.pullPolicy | Image pull policy | `IfNotPresent` |
| image.pullSecrets | Image pull secrets | `null` |
| replicaCount | Kong instance count. It has no effect when `autoscaling.enabled` is set to true | `1` |
| replicaCount | Kong instance count. It has no effect when `autoscaling.enabled` is set to true | `1` |
| plugins | Install custom plugins into Kong via ConfigMaps or Secrets | `{}` |
| env | Additional [Kong configurations](https://getkong.org/docs/latest/configuration/) | |
| customEnv | Custom Environment variables without `KONG_` prefix | |
| customEnv | Custom Environment variables without `KONG_` prefix | |
| envFrom | Populate environment variables from ConfigMap or Secret keys | |
| migrations.preUpgrade | Run "kong migrations up" jobs | `true` |
| migrations.postUpgrade | Run "kong migrations finish" jobs | `true` |
| migrations.annotations | Annotations for migration job pods | `{"sidecar.istio.io/inject": "false" |
Expand Down Expand Up @@ -741,6 +742,7 @@ section of `values.yaml` file:
| installCRDs | Legacy toggle for Helm 2-style CRD management. Should not be set [unless necessary due to cluster permissions](#removing-cluster-scoped-permissions). | false |
| env | Specify Kong Ingress Controller configuration via environment variables | |
| customEnv | Specify custom environment variables (without the CONTROLLER_ prefix) | |
| envFrom | Populate environment variables from ConfigMap or Secret keys | |
| ingressClass | The name of this controller's ingressClass | kong |
| ingressClassAnnotations | The ingress-class value for controller | kong |
| args | List of ingress-controller cli arguments | [] |
Expand Down
34 changes: 34 additions & 0 deletions charts/kong/ci/__snapshots__/test2-values.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions charts/kong/ci/test2-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ ingressController:
timeoutSeconds: 5
env:
anonymous_reports: "false"
envFrom:
- configMapRef:
name: env-config
customEnv:
TZ: "Europe/Berlin"
watchNamespaces:
Expand All @@ -23,6 +26,9 @@ postgresql:
env:
anonymous_reports: "off"
database: "postgres"
envFrom:
- configMapRef:
name: env-config
# - ingress resources are created without hosts
admin:
ingress:
Expand Down Expand Up @@ -63,3 +69,11 @@ deployment:
requests:
cpu: "100m"
memory: "64Mi"

extraObjects:
- apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
data:
test-env: test
11 changes: 11 additions & 0 deletions charts/kong/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ The name of the Service which will be used by the controller to update the Ingre
{{ toYaml .Values.containerSecurityContext | nindent 4 }}
env:
{{- include "kong.env" . | nindent 2 }}
{{- include "kong.envFrom" .Values.envFrom | nindent 2 }}
{{/* TODO the prefix override is to work around https://github.com/Kong/charts/issues/295
Note that we use args instead of command here to /not/ override the standard image entrypoint. */}}
args: [ "/bin/bash", "-c", "export KONG_NGINX_DAEMON=on KONG_PREFIX=`mktemp -d` KONG_KEYRING_ENABLED=off; until kong start; do echo 'waiting for db'; sleep 1; done; kong stop"]
Expand Down Expand Up @@ -891,6 +892,7 @@ The name of the Service which will be used by the controller to update the Ingre
apiVersion: v1
fieldPath: metadata.namespace
{{- include "kong.ingressController.env" . | indent 2 }}
{{ include "kong.envFrom" .Values.ingressController.envFrom | indent 2 }}
image: {{ include "kong.getRepoTag" .Values.ingressController.image }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{/* disableReadiness is a hidden setting to drop this block entirely for use with a debugger
Expand Down Expand Up @@ -1222,6 +1224,7 @@ Environment variables are sorted alphabetically
imagePullPolicy: {{ .Values.waitImage.pullPolicy }}
env:
{{- include "kong.no_daemon_env" . | nindent 2 }}
{{- include "kong.envFrom" .Values.envFrom | nindent 2 }}
command: [ "bash", "/wait_postgres/wait.sh" ]
volumeMounts:
- name: {{ template "kong.fullname" . }}-bash-wait-for-postgres
Expand Down Expand Up @@ -1738,3 +1741,11 @@ extensions/v1beta1
{{- end -}}
{{- (toYaml $proxyReadiness) -}}
{{- end -}}
{{- define "kong.envFrom" -}}
{{- if (gt (len .) 0) -}}
envFrom:
{{- toYaml . | nindent 2 -}}
{{- else -}}
{{- end -}}
{{- end -}}
1 change: 1 addition & 0 deletions charts/kong/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ spec:
- "$KONG_PREFIX/pids"
env:
{{- include "kong.env" . | nindent 8 }}
{{- include "kong.envFrom" .Values.envFrom | nindent 8 }}
volumeMounts:
{{- include "kong.volumeMounts" . | nindent 8 }}
{{- if .Values.deployment.initContainers }}
Expand Down
1 change: 1 addition & 0 deletions charts/kong/templates/migrations-post-upgrade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ spec:
{{ toYaml .Values.containerSecurityContext | nindent 10 }}
env:
{{- include "kong.no_daemon_env" . | nindent 8 }}
{{- include "kong.envFrom" .Values.envFrom | nindent 8 }}
args: [ "kong", "migrations", "finish" ]
volumeMounts:
{{- include "kong.volumeMounts" . | nindent 8 }}
Expand Down
1 change: 1 addition & 0 deletions charts/kong/templates/migrations-pre-upgrade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ spec:
{{ toYaml .Values.containerSecurityContext | nindent 10 }}
env:
{{- include "kong.no_daemon_env" . | nindent 8 }}
{{- include "kong.envFrom" .Values.envFrom | nindent 8 }}
args: [ "kong", "migrations", "up" ]
volumeMounts:
{{- include "kong.volumeMounts" . | nindent 8 }}
Expand Down
1 change: 1 addition & 0 deletions charts/kong/templates/migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ spec:
{{ toYaml .Values.containerSecurityContext | nindent 10 }}
env:
{{- include "kong.no_daemon_env" . | nindent 8 }}
{{- include "kong.envFrom" .Values.envFrom | nindent 8 }}
args: [ "kong", "migrations", "bootstrap" ]
volumeMounts:
{{- include "kong.volumeMounts" . | nindent 8 }}
Expand Down
8 changes: 8 additions & 0 deletions charts/kong/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ env:
# name: api_key
# client_name: testClient

# Load all ConfigMap or Secret keys as environment variables:
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables
envFrom: []

# This section can be used to configure some extra labels that will be added to each Kubernetes object generated.
extraLabels: {}

Expand Down Expand Up @@ -565,6 +569,10 @@ ingressController:
# customEnv:
# TZ: "Europe/Berlin"

# Load all ConfigMap or Secret keys as environment variables:
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables
envFrom: []

admissionWebhook:
enabled: true
failurePolicy: Ignore
Expand Down

0 comments on commit 5e7c7ce

Please sign in to comment.