Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get JUPYTERHUB_API_TOKEN from secret #693

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions kubespawner/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Dict, List, Optional
from urllib.parse import urlparse

from kubernetes.client import V1EnvVarSource, V1SecretKeySelector
from kubernetes_asyncio.client.models import (
V1Affinity,
V1Container,
Expand Down Expand Up @@ -69,6 +70,7 @@ def make_pod(
port,
image,
image_pull_policy,
user_secret_name,
image_pull_secrets=None,
node_selector=None,
uid=None,
Expand Down Expand Up @@ -468,6 +470,15 @@ def _get_env_var_deps(env):
if not "name" in env:
env["name"] = key
env = get_k8s_model(V1EnvVar, env)
elif key == "JUPYTERHUB_API_TOKEN":
env = V1EnvVar(
name="JUPYTERHUB_API_TOKEN",
value_from=V1EnvVarSource(
secret_key_ref=V1SecretKeySelector(
name=user_secret_name, key="JUPYTERHUB_API_TOKEN"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why secret ref name is configurable, but secret name itself is not?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ref has to be unique per user, so we need to pass it the user name.

)
),
)
else:
env = V1EnvVar(name=key, value=env)

Expand Down Expand Up @@ -922,6 +933,7 @@ def make_secret(
owner_references,
labels=None,
annotations=None,
jupyterhub_api_token="",
):
"""
Make a k8s secret specification using pre-existing ssl credentials for a given user.
Expand All @@ -941,6 +953,8 @@ def make_secret(
Labels to add to the secret.
annotations:
Annotations to add to the secret.
jupyterhub_api_token:
The JupyterHub API token for the user.
"""

secret = V1Secret()
Expand Down Expand Up @@ -972,6 +986,10 @@ def make_secret(
"notebooks-ca_trust.crt"
] + encoded.decode("utf-8")

secret.data["jupyterhub_api.token"] = (
base64.b64encode(jupyterhub_api_token).decode("utf-8"),
)

return secret


Expand Down
2 changes: 2 additions & 0 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,7 @@ async def get_pod_manifest(self):
port=self.port,
image=self.image,
image_pull_policy=self.image_pull_policy,
user_secret_name=self.secret_name,
image_pull_secrets=self.image_pull_secrets,
node_selector=self.node_selector,
uid=uid,
Expand Down Expand Up @@ -2106,6 +2107,7 @@ def get_secret_manifest(self, owner_reference):
owner_references=[owner_reference],
labels=labels,
annotations=annotations,
jupyterhub_api_token=self.api_token,
)

def get_service_manifest(self, owner_reference):
Expand Down