在Jupyterhub中将k8s secret的环境变量添加到笔记本中

问题描述 投票:3回答:1

我正在使用Zero to Jupyterhub Helm包来将Jupyterhub部署到我们的kubernetes集群中。单个笔记本图像需要一些额外的环境变量(主要是数据库连接信息),我希望它们从k8s命名空间中的现有秘密中绘制值。我该怎么做呢?

使用以下配置的天真方法不起作用:

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

它导致SECURE_ENVIRONMENT_VARIABLE被设置为map[valueFrom:map[secretKeyRef:map[name:secret value:key]]]

我也尝试使用singleuser.extraConfig根据c.KubeSpawner.extra_container_config设置KubeSpawner config docs,但如果你用它来设置env它显然会覆盖现有的环境变量,这会破坏系统:

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

为了记录,我可以通过helm upgrade --debug --dry-run创建部署.yaml并在必要时手动编辑,我无法弄清楚如何将这些信息传递到动态生成的pod上。

jupyterhub
1个回答
1
投票

在这里https://github.com/jupyterhub/kubespawner/issues/306#issuecomment-474934945我提供了使用非字符串值设置环境变量的解决方案。

基本思想是使用c.KubeSpawner.modify_pod_hook将变量添加到pod规范中。


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
© www.soinside.com 2019 - 2024. All rights reserved.