Skip to content

Commit

Permalink
Merge pull request #130 from minrk/reflector-init-wait
Browse files Browse the repository at this point in the history
Allow waiting for reflectors to get initial data
  • Loading branch information
yuvipanda committed Feb 28, 2018
2 parents f7ebb59 + 5dcae44 commit c07996b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions kubespawner/reflector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# specifically use concurrent.futures for threadsafety
# asyncio Futures cannot be used across threads
from concurrent.futures import Future

import time
import threading

Expand All @@ -6,6 +10,7 @@
from kubernetes import client, config, watch
from tornado.ioloop import IOLoop


class NamespacedResourceReflector(LoggingConfigurable):
"""
Base class for keeping a local up-to-date copy of a set of kubernetes resources.
Expand Down Expand Up @@ -84,6 +89,8 @@ def __init__(self, *args, **kwargs):
# FIXME: Protect against malicious labels?
self.label_selector = ','.join(['{}={}'.format(k, v) for k, v in self.labels.items()])

self.first_load_future = Future()

self.start()

def _list_and_update(self):
Expand Down Expand Up @@ -130,6 +137,9 @@ def _watch_and_update(self):
w = watch.Watch()
try:
resource_version = self._list_and_update()
if not self.first_load_future.done():
# signal that we've loaded our initial data
self.first_load_future.set_result(None)
for ev in w.stream(
getattr(self.api, self.list_method_name),
self.namespace,
Expand Down
3 changes: 3 additions & 0 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ def poll(self):
Returns None if it is, and 1 if it isn't. These are the return values
JupyterHub expects.
"""
# have to wait for first load of data before we have a valid answer
if not self.pod_reflector.first_load_future.done():
yield self.pod_reflector.first_load_future
data = self.pod_reflector.pods.get(self.pod_name, None)
if data is not None:
return None
Expand Down

0 comments on commit c07996b

Please sign in to comment.