KubeSpawner pre_spawn_hook not called?

Hello,
I am trying to use the pre_spawn_hook of the KubeSpawner.
My ultimate goal is to add volumes and auth info.

Following different examples I’ve seen in this forum, I actually can’t see the hook being executed at all anywhere.
So I went very basic and have this code, for which I would expect some logs in the hub pod logs.

singleuser:
  profileList:
    - display_name: "test"
      kubespawner_override:
        extraConfig:
          00-volumes: |
            def preSpawnHook(spawner):          
                print("preSpawnHook for mounting volume\n")
            print("hook - 00-volume")
            c.KubeSpawner.pre_spawn_hook = preSpawnHook

Those 2 print statements never show up in the hub pod logs.

The override appears to be read properly by the hub, as stated in the hub logs when the single user pod is starting.

[D 2023-02-14 23:47:05.232 JupyterHub spawner:2989] .. overriding KubeSpawner value extraConfig={'00-volumes': 'def preSpawnHook(spawner):          \n    print("preSpawnHook for mounting volume\\n")\nprint("hook - 00-volume")\nc.KubeSpawner.pre_spawn_hook = preSpawnHook\n'}

I have 2 questions:

  • anything else I need to do to “activate” the hook?
  • is there a better way to debug this than the print statement and pod logs?

I am using the version 2.0.0 of the helm chart of jupyterhub

extraConfig is a Helm Chart feature implemented in the Hub section, it lets you add arbitrary Python code, and KubeSpawner is part of the hub.
Try something like:

hub:
  extraConfig:
    00-volumes: |
      def preSpawnHook(spawner):

Oh my,
I must have been dreaming it was part of the user profile overrides :slight_smile:

Thanks for opening my eyes !

Were you able to figure this out? And what if you only want this applied to a single profile in the profile list instead of applied to everything by the hub?

You can access the selected profile from user_options on the spawner object so that can filter the logic based on profile. The name of profile would be what you define in slug in the list. Something like

hub:
  extraConfig:
    00-volumes: |
        def preSpawnHook(spawner):
              spawner.log.info(f"List of profiles: {spawner.profile_list}")
              if spawner.user_options.get('profile') == 'target_profile':      
                  spawner.log.info(f"preSpawnHook for profile {spawner.user_options.get('profile')}")
        c.KubeSpawner.pre_spawn_hook = preSpawnHook

1 Like