3

I'm using the Zero to Jupyterhub Helm package in order to deploy Jupyterhub into our kubernetes cluster. The individual notebook images need some extra environment variables (database connection information, mainly), and I'd like them to draw the values from an existing secret in the k8s namespace. How do I do this?

The naive approach of using the following config doesn't work:

singleuser:
  extraEnv:
    SECURE_ENVIRONMENT_VARIABLE: 
      valueFrom:
        secretKeyRef:
          name: secret
          value: key

It results in SECURE_ENVIRONMENT_VARIABLE being set to map[valueFrom:map[secretKeyRef:map[name:secret value:key]]].

I've also tried using singleuser.extraConfig to set c.KubeSpawner.extra_container_config as per the KubeSpawner config docs, but if you use that to set env it apparently overwrites the existing environment variables, which breaks the system:

extraConfig: |
    c.KubeSpawner.extra_container_config = {
      "env": [
        {
          "name": "SECURE_ENVIRONMENT_VARIABLE",
          "value": "test" # even a hardcoded value results in the container failing 
        }
      ]
    }

For the record, I'd be fine with creating the deployment .yaml via helm upgrade --debug --dry-run and editing that manually if necessary, I just can't figure out how to get this information onto the dynamically spawned pods.

Astrid
  • 1,808
  • 12
  • 24

1 Answers1

2

Here https://github.com/jupyterhub/kubespawner/issues/306#issuecomment-474934945 I've provided the solution for setting the environment variables with non-string values.

Basic idea is to use c.KubeSpawner.modify_pod_hook to add the variable to the pod specification.


hub:
  extraConfig:
    ipaddress: |
      from kubernetes import client

      def modify_pod_hook(spawner, pod):
          pod.spec.containers[0].env.append(client.V1EnvVar("MY_POD_IP", None, client.V1EnvVarSource(None, client.V1ObjectFieldSelector(None, "status.podIP"))))
          return pod
      c.KubeSpawner.modify_pod_hook = modify_pod_hook
abinet
  • 2,552
  • 1
  • 15
  • 24