Skip to content

Commit

Permalink
pass server options to spawner and process them
Browse files Browse the repository at this point in the history
  • Loading branch information
rokroskar committed Oct 16, 2018
1 parent f3be861 commit 096363c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
2 changes: 1 addition & 1 deletion helm-chart/renku-notebooks/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ apiVersion: v1
appVersion: '1.0'
description: A Helm chart for the Renku Notebooks service
name: renku-notebooks
version: 0.2.1
version: 0.0-unclean-03ed7fa
31 changes: 19 additions & 12 deletions helm-chart/renku-notebooks/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ replicaCount: 1

image:
repository: renku/renku-notebooks
tag: '0.2.1'
tag: '0.0-unclean-03ed7fa'
pullPolicy: IfNotPresent

## Optionally specify an array of imagePullSecrets.
Expand Down Expand Up @@ -83,17 +83,24 @@ affinity: {}

serverOptions:
resources:
cpu:
cpu_limit:
displayName: Number of CPUs
type: integer
default: 1
range: [1, 4]
memory:
type: float
default: 1.0
range: [0.1, 4.0]
mem_limit:
displayName: Memory
type: enum
default: 1
options: [1Gi, 2Gi, 4Gi, 8Gi]
defaultPath: /lab
default: 1G
options: [2G, 4G, 8G]
gpu:
displayName: Number of GPUs
type: integer
default: 0
range: [0, 4]
defaultUrl:
default: /lab
options: [/lab]

## Configuration for the jupyterhub service
jupyterhub:
Expand All @@ -102,7 +109,7 @@ jupyterhub:
hub:
image:
name: renku/jupyterhub-k8s
tag: '0.2.1'
tag: '0.0-unclean-03ed7fa'
allowNamedServers: true
services:
notebooks:
Expand Down Expand Up @@ -160,14 +167,14 @@ jupyterhub:
singleuser:
image:
name: renku/singleuser
tag: '0.2.1'
tag: '0.0-unclean-03ed7fa'
storage:
type: none
defaultUrl: /lab
singleuser-r:
image:
name: renku/singleuser-r
tag: '0.2.1'
tag: '0.0-unclean-03ed7fa'
# FIXME: bug in prepuller makes helm hang in certain cases. Fixed in 0.7
# so this should be removed eventually.
# https://github.com/jupyterhub/zero-to-jupyterhub-k8s/issues/477
Expand Down
26 changes: 18 additions & 8 deletions jupyterhub/spawners.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def start(self, *args, **kwargs):
commit_sha = options.get('commit_sha')
commit_sha_7 = commit_sha[:7]
self.image = options.get('image')
self.default_url = options.get('default_url')

url = os.getenv('GITLAB_URL', 'http://gitlab.renku.build')

Expand All @@ -112,8 +111,8 @@ def start(self, *args, **kwargs):
# gather project permissions for the logged in user
permissions = gl_project.attributes['permissions']
access_level = max([
x[1].get('access_level', 0) for x in permissions.items()
if x[1]
x[1].get('access_level', 0)
for x in permissions.items() if x[1]
])
self.log.debug(
'access level for user {username} in '
Expand All @@ -133,8 +132,6 @@ def start(self, *args, **kwargs):
raise web.HTTPError(401, 'Not authorized to view project.')
return



self.cmd = 'jupyterhub-singleuser'
try:
result = yield super().start(*args, **kwargs)
Expand All @@ -151,6 +148,7 @@ def start(self, *args, **kwargs):

return result


try:
import docker
from dockerspawner import DockerSpawner
Expand Down Expand Up @@ -198,8 +196,8 @@ def start(self):
# make sure we have the alpine/git image
images = yield self.docker('images')
if not any([
'alpine/git:latest' in i['RepoTags'] for i in images
if i['RepoTags']
'alpine/git:latest' in i['RepoTags']
for i in images if i['RepoTags']
]):
alpine_git = yield self.docker(
'pull', 'alpine/git', tag='latest'
Expand Down Expand Up @@ -290,7 +288,8 @@ def get_pod_manifest(self):

# 1. Define a new empty volume.
self.volumes = [
volume for volume in self.volumes if volume['name'] != git_volume_name
volume
for volume in self.volumes if volume['name'] != git_volume_name
]
volume = {
'name': git_volume_name,
Expand Down Expand Up @@ -345,6 +344,17 @@ def get_pod_manifest(self):
]
self.volume_mounts.append(volume_mount)

## Process the requested server options
server_options = options.get('server_options', {})
self.default_url = server_options.get('default_url')
self.cpu_limit = float(server_options['resources'].get('cpu_limit'))
self.mem_limit = server_options['resources'].get('mem_limit')
gpu = server_options.get('gpu', {})
if gpu:
self.extra_resource_guarantees = {
"nvidia.com/gpu": string(gpu)
}

## Finalize the pod configuration

# Set the notebook container image
Expand Down
43 changes: 36 additions & 7 deletions src/notebooks_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import escapism
import gitlab
import requests
from flask import (Flask, Response, abort, jsonify, make_response, redirect,
render_template, request, send_file, send_from_directory)
from flask import (
Flask, Response, abort, jsonify, make_response, redirect, render_template,
request, send_file, send_from_directory
)
from jupyterhub.services.auth import HubOAuth

SERVICE_PREFIX = os.environ.get('JUPYTERHUB_SERVICE_PREFIX', '/')
Expand Down Expand Up @@ -378,15 +380,40 @@ def launch_notebook(user, namespace, project, commit_sha, notebook=None):
mimetype='application/json'
)

# 1. launch using spawner that checks the access
## 1. launch using spawner that checks the access
headers = {auth.auth_header_name: 'token {0}'.format(auth.api_token)}

# if there is an image passed with the request, use it
# set the notebook image
image = get_notebook_image(user, namespace, project, commit_sha)

default_url = request.args.get(
'default_url', os.environ.get('JUPYTERHUB_SINGLEUSER_DEFAULT_URL')
## process the server options
# server options from system configuration
server_options_file = os.getenv(
'NOTEBOOKS_SERVER_OPTIONS_PATH',
'/etc/renku-notebooks/server_options.json'
)

with open(server_options_file) as f:
server_options_defaults = json.load(f)
default_resources = server_options_defaults.get(
'resources', {}
)
# process the requested options and set others to defaults from config

server_options = (request.get_json() or {}).get('serverOptions', {})
server_options.setdefault(
'default_url',
server_options_defaults.get('defaultUrl', {}).get(
'default', os.getenv('JUPYTERHUB_SINGLEUSER_DEFAULT_URL')
)
)
server_options.setdefault('resources', {})

for key in default_resources.keys():
server_options['resources'].setdefault(
key,
default_resources.get(key)['default']
)

payload = {
'branch': request.args.get('branch', 'master'),
Expand All @@ -395,8 +422,10 @@ def launch_notebook(user, namespace, project, commit_sha, notebook=None):
'notebook': notebook,
'project': project,
'image': image,
'default_url': default_url,
'server_options': server_options,
}
app.logger.debug(payload)

if os.environ.get('GITLAB_REGISTRY_SECRET'):
payload['image_pull_secrets'] = payload.get('image_pull_secrets', [])
payload['image_pull_secrets'].append(
Expand Down

0 comments on commit 096363c

Please sign in to comment.