Skip to content

Commit

Permalink
singleuser/hub.extraEnv with new syntax support
Browse files Browse the repository at this point in the history
Note this commit relies on a KubeSpawner PR currently not merged. See
jupyterhub/kubespawner#426.
  • Loading branch information
consideRatio committed Sep 1, 2020
1 parent 9d1ad08 commit a316b05
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 38 deletions.
60 changes: 49 additions & 11 deletions jupyterhub/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -313,33 +313,34 @@ properties:
- Configure code running in the hub pod, such as an authenticator or
spawner.
String literals with $(ENV_VAR_NAME) will be expanded by Kubelet which
is a part of Kubernetes.
```yaml
hub:
extraEnv:
# short notation, only for hardcoded string values
# basic notation (for literal values only)
MY_ENV_VARS_NAME1: "my env var value 1"
# long notation, Kubernetes native syntax
dummyKey1:
# explicit notation (the "name" field takes precedence)
HUB_NAMESPACE:
name: HUB_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
dummyKey2:
name: PREFIXED_HUB_NAMESPACE
# implicit notation (the "name" field is implied)
PREFIXED_HUB_NAMESPACE:
value: "my-prefix-$(HUB_NAMESPACE)"
dummyKey3:
name: SECRET_VALUE
SECRET_VALUE:
valueFrom:
secretKeyRef:
name: my-k8s-secret
key: password
```
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.
For more information, see the [Kubernetes EnvVar
specification](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#envvar-v1-core).
extraConfig:
type: object
description: |
Expand Down Expand Up @@ -890,6 +891,43 @@ properties:
description: |
Deprecated and no longer does anything. Use the user-scheduler instead
in order to accomplish a good packing of the user pods.
extraEnv:
type: object
description: |
Extra environment variables that should be set for the user pods.
String literals with $(ENV_VAR_NAME) will be expanded by Kubelet which
is a part of Kubernetes. Note that the user pods will already have
access to a set of environment variables that you can use, like
JUPYTERHUB_USER and JUPYTERHUB_HOST. For more information about these
inspect [this source
code](https://github.com/jupyterhub/jupyterhub/blob/cc8e7806530466dce8968567d1bbd2b39a7afa26/jupyterhub/spawner.py#L763).
```yaml
hub:
extraEnv:
# basic notation (for literal values only)
MY_ENV_VARS_NAME1: "my env var value 1"
# explicit notation (the "name" field takes precedence)
USER_NAMESPACE:
name: USER_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
# implicit notation (the "name" field is implied)
PREFIXED_USER_NAMESPACE:
value: "my-prefix-$(USER_NAMESPACE)"
SECRET_VALUE:
valueFrom:
secretKeyRef:
name: my-k8s-secret
key: password
```
For more information, see the [Kubernetes EnvVar
specification](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#envvar-v1-core).
extraTolerations:
type: list
description: |
Expand Down
39 changes: 39 additions & 0 deletions jupyterhub/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,42 @@ limits:
{{- end }}
{{- end }}
{{- end }}

{{- /*
jupyterhub.extraEnv:
Output YAML formatted EnvVar entries for use in a containers env field.
*/}}
{{- define "jupyterhub.extraEnv" -}}
{{- include "jupyterhub.extraEnv.withTrailingNewLine" . | trimSuffix "\n" }}
{{- end }}

{{- define "jupyterhub.extraEnv.withTrailingNewLine" -}}
{{- if . }}
{{- /* If extraEnv is a list, we inject it as it is. */}}
{{- if eq (typeOf .) "[]interface {}" }}
{{- . | toYaml }}

{{- /* If extraEnv is a map, we differentiate two cases: */}}
{{- else if eq (typeOf .) "map[string]interface {}" }}
{{- range $key, $value := . }}
{{- /*
- If extraEnv.someKey has a map value, then we add the value as a YAML
parsed list element and use the key as the name value unless its
explicitly set.
*/}}
{{- if eq (typeOf $value) "map[string]interface {}" }}
{{- merge (dict) $value (dict "name" $key) | list | toYaml | println }}
{{- /*
- If extraEnv.someKey has a string value, then we use the key as the
environment variable name for the value.
*/}}
{{- else if eq (typeOf $value) "string" -}}
- name: {{ $key | quote }}
value: {{ $value | quote | println }}
{{- else }}
{{- printf "?.extraEnv.%s had an unexpected type (%s)" $key (typeOf $value) | fail }}
{{- end }}
{{- end }} {{- /* end of range */}}
{{- end }}
{{- end }} {{- /* end of: if . */}}
{{- end }} {{- /* end of definition */}}
25 changes: 1 addition & 24 deletions jupyterhub/templates/hub/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,30 +205,7 @@ spec:
key: hub.db.password
{{- end }}
{{- end }}
{{- if .Values.hub.extraEnv }}
{{- $extraEnvType := typeOf .Values.hub.extraEnv }}
{{- /* 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 {}" }}
{{- 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 }}
{{- include "jupyterhub.extraEnv" .Values.hub.extraEnv | nindent 12 }}
ports:
- name: http
containerPort: 8081
Expand Down
5 changes: 2 additions & 3 deletions tools/templates/lint-and-validate-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ hub:
extraConfigMap:
mock.entry: mock-config-map-entry
extraEnv:
dummyMergeKey1:
IGNORED_KEY_NAME:
name: MOCK_ENV_VAR_NAME1
value: MOCK_ENV_VAR_VALUE1
dummyMergeKey2:
name: MOCK_ENV_VAR_NAME2
MOCK_ENV_VAR_NAME2:
value: MOCK_ENV_VAR_VALUE2
extraContainers: []
extraVolumes: []
Expand Down

0 comments on commit a316b05

Please sign in to comment.