Spawner Unnecessarily Encoding Capital Letters Leading to PVC Creation Errors and JHub Crash

I had a JupyterHub crash on me today. When scrutinizing the logs (e.g., kubctl logs hub-xxx -n jhub), I stumbled upon the error below. Note, that this problem temporarily crashed the entire JupyterHub(!). See how MattPanavva is getting encoded into -4datt-50anavva. The -4d and -50 are coming from URL ASCII character encodings. In turn, the PVC claim errors out because it does not like the characters being provided to create the PVC.

[E 2823-01-26 19:18:56.387 JupyterHub log: 189] 500 GET /hub/spawn-pending/MattPanavva (MattPanavva@192.206.29.254) 8.94ms
[I 2823-81-26 19:18:57.558 JupyterHub provider:574] Creating oauth client jupvterhub-user-MattPanavva
[I 2023-01-26 19:18:57.573 JupyterHub spawner: 2344] Attempting to create pvc claim--4datt-50anavva, with timeout 3
[I 2823-01-26 19:18:57.574 JupyterHub log: 189] 302 GET /hub/spawn/MattPanavva -> /hub/spawn-pending/MattPanavva (MattPanavva@192.206.29.254) 38.73ms
[E 2023-81-26 19:18:57.576 JupyterHub user: 718] Unhandled error starting MattPanavva's server: (422)
Reason: Unprocessable Entity
HTTP response headers: HTTPHeaderDict({'Audit-Id':'11db7f18-d806-476e-9c05-3ba6fe563beb',
'Cache-Control': 'no-cache, private',
'Content-Type':
application/json'
X-Kubernetes-Pf-Flowschema-Uid'
"3f8fc112-fdfe-4680-9186-16f1a12207
af'
, "X-Kubernetes-Pf-Prioritylevel-Uid':'972c5ee8-5624-4658-6403-6a13663a7485'
'Date': 'Thu, 26 Jan 2823 19:18:57 GMT', 'Content-Length': '954'3)
HTTP response body: {"kind": "Status"
, "apiVersion": "vi", "metadata": {3, "status" "Failure", "message": "PersistentVolumeClaim ("claim--4datt-50anavva)" is invalid: metadata.labels: Invalid value: ("-4datt-50anavva)":
a valid label
must be
an empty string or consist of alphanumeric characters,
or
and must start and end with an alphanumeric character (e.g. 'MyValue'
or 'mv_value'
or '12345', regex used for validation is '((LA-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-
28-9])?')", "reason": "Invalid", "details": {"name": "claim--4datt-50anavva",
, "kind": "PersistentVolumeClaim", "causes":[{" reason" "FieldValueInvalid", "message": "Invalid value: ("-4datt-50anavva)": a valid label must be an empty string or consist
of alphanumeric characters,
and must start and end with an alphanumeric character (e.g.
"MyValue":
'my _value',
'12345', regex used for validation is '(([A-Za-z8-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')", "field": "metada
ta.labels"}]}, "code": 422}

There’s an open bug report for this Spawning fails for user names starting with non-alphanumeric chars · Issue #498 · jupyterhub/kubespawner · GitHub

If your usernames are case-insensitive a quick workaround is to subclass the authenticator to convert them to lowercase by overriding normalize_username: