Skip to content

Commit

Permalink
Merge pull request #13 from lsst-sqre/tickets/DM-27441
Browse files Browse the repository at this point in the history
[DM-27441] Load images list from URL
  • Loading branch information
cbanek authored Dec 4, 2020
2 parents 06654b0 + 5d6bfc2 commit f57721d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
20 changes: 4 additions & 16 deletions dev-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ jupyterhub:
storage:
type: none

auth:
state:
cryptoKey: "080c6d0eb8d9f84ef5f0860bea71dba6ab2e1be712c7bc8c2eaf92321c9fd968"

proxy:
secretToken: "3854245f5b48412ee9b7a2c985da6acdc146f80cc4f10eb175f0efa24530399b"
service:
type: ClusterIP

ingress:
hosts: ["minikube.lsst.codes"]
annotations:
Expand All @@ -34,15 +25,10 @@ jupyterhub:

config:
options_form:
images_url: "http://cachemachine-dev.cachemachine-dev.svc.cluster.local/cachemachine/jupyter/available"
images:
- name: Recommended
image: registry.hub.docker.com/jupyterhub/k8s-singleuser-sample:0.9.0-n358.h11c2aeb8
- name: Weekly 44
image: registry.hub.docker.com/lsstsqre/sciplat-lab:w_2020_44
- name: Weekly 43
image: registry.hub.docker.com/lsstsqre/sciplat-lab:w_2020_43
- name: Weekly 42
image: registry.hub.docker.com/lsstsqre/sciplat-lab:w_2020_42
image_url: registry.hub.docker.com/lsstsqre/sciplat-lab:w_2020_42
sizes:
- name: Tiny
cpu: .5
Expand All @@ -57,3 +43,5 @@ config:
cpu: 4
ram: 12288M
user_resources: {}

vault_secret_path: "secret/k8s_operator/minikube.lsst.codes/nublado2"
6 changes: 3 additions & 3 deletions src/nublado2/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pre_spawn(self, spawner: Spawner) -> None:
# This will help set up the created lab pod.
nc = NubladoConfig()
(cpu, ram) = nc.lookup_size(size_name)
spawner.image = nc.lookup_image_url(image_name)
spawner.image = image_name
spawner.debug = options.get("debug_enabled", False)
spawner.mem_limit = ram
spawner.cpu_limit = cpu
Expand All @@ -41,7 +41,7 @@ def post_stop(self, spawner: Spawner) -> None:
self.log.debug(f"Post stop-hook called for {user}")
self.resourcemgr.delete_user_resources(spawner.namespace)

def show_options(self, spawner: Spawner) -> str:
async def show_options(self, spawner: Spawner) -> str:
user = spawner.user.name
self.log.debug(f"Show options hook called for {user}")
return self.optionsform.show_options_form(spawner)
return await self.optionsform.show_options_form(spawner)
9 changes: 0 additions & 9 deletions src/nublado2/nublado_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,3 @@ def lookup_size(self, name: str) -> Tuple[float, str]:
return (float(s["cpu"]), s["ram"])

raise ValueError(f"Size {name} not found")

def lookup_image_url(self, name: str) -> str:
images = self.get()["options_form"]["images"]

for i in images:
if i["name"] == name:
return i["image"]

raise ValueError(f"Image url for {name} not found")
30 changes: 27 additions & 3 deletions src/nublado2/options.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Dict, List, Optional

from aiohttp import ClientSession
from jinja2 import Template
from jupyterhub.spawner import Spawner
from traitlets.config import LoggingConfigurable
Expand Down Expand Up @@ -25,7 +28,7 @@
<td width="50%">
{% for i in images %}
<input type="radio" name="image"
id="{{ i.name }}" value="{{ i.name }}"
id="{{ i.name }}" value="{{ i.image_url }}"
{% if loop.first %} checked {% endif %}
>
{{ i.name }}<br />
Expand All @@ -51,10 +54,31 @@
"""
)

# Don't have this be a member of NubladoOptions, we should
# share this connection pool. Also the LoggingConfigurable
# will try to pickle it to json, and it can't pickle a session.
session = ClientSession()


class NubladoOptions(LoggingConfigurable):
def show_options_form(self, spawner: Spawner) -> str:
async def show_options_form(self, spawner: Spawner) -> str:
options_config = NubladoConfig().get()["options_form"]
images = options_config["images"]
sizes = options_config["sizes"]

images_url = options_config.get("images_url")
images = await self._get_images_from_url(images_url)
images.extend(options_config["images"])

return options_template.render(images=images, sizes=sizes)

async def _get_images_from_url(
self, url: Optional[str]
) -> List[Dict[str, str]]:
if not url:
return []

r = await session.get(url)
if r.status != 200:
raise Exception(f"Error {r.status} from {url}")

return await r.json()

0 comments on commit f57721d

Please sign in to comment.