Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monkeypatch kubernetes to avoid ThreadPool problems #169

Merged
merged 2 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions kubespawner/clients.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
"""Shared clients for kubernetes

avoids creating multiple kubernetes client objects,
each of which spawns an unused max-size thread pool
"""

from unittest.mock import Mock
import weakref

import kubernetes.client
from kubernetes.client import api_client

# FIXME: remove when instantiating a kubernetes client
# doesn't create N-CPUs threads unconditionally.
# monkeypatch threadpool in kubernetes api_client
# to avoid instantiating ThreadPools.
# This is known to work for kubernetes-4.0
# and may need updating with later kubernetes clients
_dummy_pool = Mock()
api_client.ThreadPool = lambda *args, **kwargs: _dummy_pool

_client_cache = {}

Expand Down
6 changes: 3 additions & 3 deletions kubespawner/reflector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

from traitlets.config import LoggingConfigurable
from traitlets import Any, Dict, Unicode
from kubernetes import client, config, watch
from kubernetes import config, watch
from tornado.ioloop import IOLoop

from .clients import shared_client

class NamespacedResourceReflector(LoggingConfigurable):
"""
Expand Down Expand Up @@ -92,7 +92,7 @@ def __init__(self, *args, **kwargs):
config.load_incluster_config()
except config.ConfigException:
config.load_kube_config()
self.api = getattr(client, self.api_group_name)()
self.api = shared_client(self.api_group_name)

# FIXME: Protect against malicious labels?
self.label_selector = ','.join(['{}={}'.format(k, v) for k, v in self.labels.items()])
Expand Down