Skip to content

Commit

Permalink
Merge pull request #12 from consideRatio/tickets/DM-33560
Browse files Browse the repository at this point in the history
Refactor away the `create` function
  • Loading branch information
athornton authored Feb 19, 2022
2 parents 90231cb + 3a82fc0 commit dc18487
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
27 changes: 13 additions & 14 deletions kubespawner/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,19 @@ class is Proxy, as declared in proxy.py:
'component': self.component_label,
'hub.jupyter.org/proxy-route': 'true',
}
(
self.ingress_reflector,
self.service_reflector,
self.endpoint_reflector,
) = asyncio.gather(
IngressReflector.create(
parent=self, namespace=self.namespace, labels=labels
),
ServiceReflector.create(
parent=self, namespace=self.namespace, labels=labels
),
EndpointsReflector.create(
parent=self, namespace=self.namespace, labels=labels
),
self.ingress_reflector = IngressReflector(
parent=self, namespace=self.namespace, labels=labels
)
self.service_reflector = ServiceReflector(
parent=self, namespace=self.namespace, labels=labels
)
self.endpoint_reflector = EndpointsReflector(
self, namespace=self.namespace, labels=labels
)
await asyncio.gather(
self.ingress_reflector.start(),
self.service_reflector.start(),
self.endpoint_reflector.start(),
)

def _await_async_init(method):
Expand Down
15 changes: 0 additions & 15 deletions kubespawner/reflector.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,6 @@ def __init__(self, *args, **kwargs):

self.watch_task = None

@classmethod
async def create(cls, *args, **kwargs):
"""
This is a workaround: `__init__` cannot be async, but we want
to call async methods to instantiate a reflector in a usable state.
This method creates the reflector, and then loads Kubernetes config,
acquires an API client, and starts the watch task.
Use it to create a new Reflector object.
"""
inst = cls(*args, **kwargs)
await inst.start()
return inst

async def _list_and_update(self):
"""
Update current list of resources by doing a full fetch.
Expand Down
4 changes: 2 additions & 2 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2244,12 +2244,13 @@ def on_reflector_failure():
previous_reflector = self.__class__.reflectors.get(key)

if replace or not previous_reflector:
self.__class__.reflectors[key] = await ReflectorClass.create(
self.__class__.reflectors[key] = ReflectorClass(
parent=self,
namespace=self.namespace,
on_failure=on_reflector_failure,
**kwargs,
)
await self.__class__.reflectors[key].start()

if replace and previous_reflector:
# we replaced the reflector, stop the old one
Expand Down Expand Up @@ -2316,7 +2317,6 @@ async def _make_create_pod_request(self, pod, request_timeout):
self.log.info(
f"Attempting to create pod {pod.metadata.name}, with timeout {request_timeout}"
)
# Use asyncio.wait_for, _request_timeout seems unreliable?
await asyncio.wait_for(
self.api.create_namespaced_pod(
self.namespace,
Expand Down

0 comments on commit dc18487

Please sign in to comment.