Skip to content

Commit

Permalink
hub.extraEnv in dict format to support k8s EnvVar spec
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Aug 26, 2020
1 parent 3d017fe commit 9d1ad08
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
36 changes: 27 additions & 9 deletions jupyterhub/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,38 @@ properties:
description: |
Extra environment variables that should be set for the hub pod.
Environment variables are usually used to:
- Pass parameters to some custom code in `hub.extraConfig`.
- Configure code running in the hub pod, such as an authenticator or
spawner.
```yaml
hub:
extraEnv:
MY_ENV_VARS_NAME: "my env vars value"
# short notation, only for hardcoded string values
MY_ENV_VARS_NAME1: "my env var value 1"
# long notation, Kubernetes native syntax
dummyKey1:
name: HUB_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
dummyKey2:
name: PREFIXED_HUB_NAMESPACE
value: "my-prefix-$(HUB_NAMESPACE)"
dummyKey3:
name: SECRET_VALUE
valueFrom:
secretKeyRef:
name: my-k8s-secret
key: password
```
**NOTE**: We still support this field being a list of
[EnvVar](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#envvar-v1-core)
objects as well.
These are usually used in two circumstances:
- Passing parameters to some custom code specified with `extraConfig`
- Passing parameters to an authenticator or spawner that can be directly customized
by environment variables (rarer)
In the long notation, you can provide any [Kubernetes
EnvVar](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#envvar-v1-core)
specification, and the name of the key (`dummyKey1`) will have no
impact.
extraConfig:
type: object
description: |
Expand Down
15 changes: 13 additions & 2 deletions jupyterhub/templates/hub/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,25 @@ spec:
{{- end }}
{{- if .Values.hub.extraEnv }}
{{- $extraEnvType := typeOf .Values.hub.extraEnv }}
{{- /* If we have a list, embed that here directly. This allows for complex configuration from configmap, downward API, etc. */}}
{{- /* If hub.extraEnv is a list, we inject it as it is. */}}
{{- if eq $extraEnvType "[]interface {}" }}
{{- .Values.hub.extraEnv | toYaml | trimSuffix "\n" | nindent 12 }}
{{- /* If hub.extraEnv is a map, we differentiate two cases:
- If hub.extraEnv.someKey has a map value, then we add the value
as a list element and ignore the key.
- If hub.extraEnv.someKey has a string value, then we use the
key as the environment variable name for the value.
*/}}
{{- else if eq $extraEnvType "map[string]interface {}" }}
{{- /* If we have a map, treat those as key-value pairs. */}}
{{- range $key, $value := .Values.hub.extraEnv }}
{{- if eq (typeOf $value) "map[string]interface {}" }}
{{- $value | list | toYaml | nindent 12 }}
{{- else if eq (typeOf $value) "string" }}
- name: {{ $key | quote }}
value: {{ $value | quote }}
{{- else }}
{{ printf "hub.extraEnv.%s had an unexpected type (%s)" $key (typeOf $value) | fail }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
Expand Down
7 changes: 6 additions & 1 deletion tools/templates/lint-and-validate-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ hub:
extraConfigMap:
mock.entry: mock-config-map-entry
extraEnv:
MOCK_HUB_ENV: mock
dummyMergeKey1:
name: MOCK_ENV_VAR_NAME1
value: MOCK_ENV_VAR_VALUE1
dummyMergeKey2:
name: MOCK_ENV_VAR_NAME2
value: MOCK_ENV_VAR_VALUE2
extraContainers: []
extraVolumes: []
extraVolumeMounts: []
Expand Down

0 comments on commit 9d1ad08

Please sign in to comment.