Skip to content

Commit

Permalink
Merge pull request #111 from yuvipanda/more-parameters
Browse files Browse the repository at this point in the history
Allow easily augmenting the pod config before launching
  • Loading branch information
minrk committed Dec 15, 2017
2 parents aa03c5c + 759957b commit 13bd555
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
26 changes: 25 additions & 1 deletion kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import escapism

from kubespawner.traitlets import Callable
from kubespawner.utils import request_maker, k8s_url
from kubespawner.utils import request_maker, k8s_url, Callable
from kubespawner.objects import make_pod, make_pvc
from kubespawner.reflector import PodReflector

Expand Down Expand Up @@ -438,6 +438,28 @@ def _hub_connect_port_default(self):
"""
)

modify_pod_hook = Callable(
None,
allow_none=True,
config=True,
help="""
Callable to augment the Pod object before launching.
Expects a callable that takes two parameters:
1. The spawner object that is doing the spawning
2. The Pod object that is to be launched
You should modify the Pod object and return it.
This can be a coroutine if necessary. When set to none, no augmenting is done.
This is very useful if you want to modify the pod being launched dynamically.
Note that the spawner object can change between versions of KubeSpawner and JupyterHub,
so be careful relying on this!
"""
)

volumes = List(
[],
config=True,
Expand Down Expand Up @@ -909,6 +931,8 @@ def start(self):
# FIXME: Have better / cleaner retry logic!
retry_times = 4
pod = yield self.get_pod_manifest()
if self.modify_pod_hook:
pod = yield gen.maybe_future(self.modify_pod_hook(self, pod))
for i in range(retry_times):
try:
yield self.asynchronize(
Expand Down
17 changes: 17 additions & 0 deletions kubespawner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import yaml

from tornado.httpclient import HTTPRequest
from traitlets import TraitType


def request_maker():
Expand Down Expand Up @@ -130,3 +131,19 @@ def k8s_url(namespace, kind, name=None):
if name is not None:
url_parts.append(name)
return '/' + '/'.join(url_parts)


class Callable(TraitType):
"""A trait which is callable.
Notes
-----
Classes are callable, as are instances
with a __call__() method."""

info_text = 'a callable'

def validate(self, obj, value):
if callable(value):
return value
else:
self.error(obj, value)

0 comments on commit 13bd555

Please sign in to comment.