Advanced z2jh: Deeply customizing the spawner

Sometimes, you need to fix something in your z2jh that you can’t do with any of the configuration options or even by setting traitlets directly.

As an example, I was running into a bug that made kubespawner just not work for users with usernames starting with -. Now, this needs to be fixed properly in kubespawner, but the student encountering this issue had a deadline! How to fix this?

From the error message, I know that the issue was us setting a label with the username on the pod, and that not being escaped properly. Looking through the code, I also discovered the exact line causing the issue. Since I don’t use the labels for anything useful, I just needed to get rid of it.

You can specify python you want to run as part of config via hub.extraConfig, and this can be used to actually make a new spawner class! So you inherit from KubeSpawner, override what you want, and tada!

In our case, the config ended up something like this:

hub:
  extraConfig: 
    01-no-labels: |
      from kubespawner import KubeSpawner
    
      class CustomSpawner(KubeSpawner):
          def _build_common_labels(self, extra_labels):
            labels = super()._build_common_labels(extra_labels)
            # Until https://github.com/jupyterhub/kubespawner/issues/498
            # is fixed
            del labels['hub.jupyter.org/username']
            return labels

This removes the username label, and solves my problem! I link it to the kubespawner issue, so I can remove this customization when the issue is fixed.

Hope this helps folks!

5 Likes

If you wanna look at a somewhat bananas customized spawner that has organically grown over time at berkeley, I present to you datahub/values.yaml at 21e4a45c9f694578ec297c2947a0537f3bdcaa5b · berkeley-dsep-infra/datahub · GitHub. Do not recommend :slight_smile:

1 Like

Hi,

Thanks for the hints !

Is there any recommended way to test/develop a custom KubeSpawner beside updating the JupyterHub installation for each iteration of the spawner code ?

Not sure about “recommended”, but a couple of options spring to mind:

1 Like

The first solution works well on macOS with Docker Desktop.

What I am currently doing is writing and testing my code using the KubeSpawner repository so I have everything there available and at hand.

I think it should also work well with Kind but that remains to be tested.

I also added k9s to the mix so I can monitor that the cleanup after the test finished running to avoid spamming the cluster.

Thanks for the suggestions !

1 Like