Spawn notebook on different namespace on Kubernetes

hi, I’m deploying jupyterhub on Kubernetes cluster with the helm chart,
I’m using custom pre_spawn_hook to determine the namespace-
in my conf.py file:
async def custom_pre_spawn_hook(spawner):
if :
spawner.namespace = namespce_1
else:
spawner.namespace = namespce_2

c.kubespawner.pre_spawn_hook = custom_pre_spawn_hook

with this configuration the pod is spawn successfully on the other namespace but the hub don’t get the pod and in the ui I’m stack on state of server quested with no events.

when I’m changing the value of c.kubespawner.namespce to the wanted namespace its working.

what is the difference between the spawner.namespace inside the custom_pre_spawn_hook and the c.kubespawner.namespce. and how can i change it dynamically?

I think you’ll want to set KubeSpawner.enable_user_namespaces = True, at which point the namespace is governed by KubeSpawner.user_namespace_template:

hub:
  config:
    KubeSpawner:
      enable_user_namespaces: true

The reason changing namespace in hooks doens’t work is that kubespawner uses reflectors to watch for resources changes, which are tied to namespaces. Changing the namespace after the reflectors start means resources won’t be found.