Skip to content

Commit

Permalink
Merge pull request #169 from minrk/mock-kube-pool
Browse files Browse the repository at this point in the history
monkeypatch kubernetes to avoid ThreadPool problems
  • Loading branch information
minrk committed May 7, 2018
2 parents cc658f4 + f076ec7 commit 54730e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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 @@ -84,7 +84,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

0 comments on commit 54730e7

Please sign in to comment.