Skip to content

Commit 91e6bdd

Browse files
authored
Merge pull request #1274 from betatim/no-preload-builds
[MRG] Stop preloading k8s API response when listing all DIND pods
2 parents 6c6a2a7 + 3aa7cd9 commit 91e6bdd

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

binderhub/build.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,16 @@ def get_affinity(self):
183183
repository prefer to schedule on the same node in order to reuse the
184184
docker layer cache of previous builds.
185185
"""
186-
dind_pods = self.api.list_namespaced_pod(
186+
resp = self.api.list_namespaced_pod(
187187
self.namespace,
188188
label_selector="component=dind,app=binder",
189189
_request_timeout=KUBE_REQUEST_TIMEOUT,
190+
_preload_content=False,
190191
)
192+
dind_pods = json.loads(resp.read())
191193

192194
if self.sticky_builds and dind_pods:
193-
node_names = [pod.spec.node_name for pod in dind_pods.items]
195+
node_names = [pod["spec"]["nodeName"] for pod in dind_pods["items"]]
194196
ranked_nodes = rendezvous_rank(node_names, self.repo_url)
195197
best_node_name = ranked_nodes[0]
196198

binderhub/tests/test_build.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,31 @@ async def test_build(app, needs_build, needs_launch, always_build, slug, pytestc
7171
assert r.url.startswith(final['url'])
7272

7373

74+
def _list_dind_pods_mock():
75+
"""Mock list of DIND pods"""
76+
mock_response = mock.MagicMock()
77+
mock_response.read.return_value = json.dumps(
78+
{
79+
"items": [
80+
{
81+
"spec": {"nodeName": name},
82+
}
83+
for name in ["node-a", "node-b"]
84+
]
85+
}
86+
)
87+
mock_k8s_api = mock.MagicMock()
88+
mock_k8s_api.list_namespaced_pod.return_value = mock_response
89+
return mock_k8s_api
90+
91+
7492
def test_default_affinity():
7593
# check that the default affinity is a pod anti-affinity
94+
95+
mock_k8s_api = _list_dind_pods_mock()
96+
7697
build = Build(
77-
mock.MagicMock(), api=mock.MagicMock(), name='test_build',
98+
mock.MagicMock(), api=mock_k8s_api, name='test_build',
7899
namespace='build_namespace', repo_url=mock.MagicMock(),
79100
ref=mock.MagicMock(), build_image=mock.MagicMock(),
80101
image_name=mock.MagicMock(), push_secret=mock.MagicMock(),
@@ -92,14 +113,7 @@ def test_default_affinity():
92113

93114
def test_sticky_builds_affinity():
94115
# Setup some mock objects for the response from the k8s API
95-
Pod = namedtuple("Pod", "spec")
96-
PodSpec = namedtuple("PodSpec", "node_name")
97-
PodList = namedtuple("PodList", "items")
98-
99-
mock_k8s_api = mock.MagicMock()
100-
mock_k8s_api.list_namespaced_pod.return_value = PodList(
101-
[Pod(PodSpec("node-a")), Pod(PodSpec("node-b"))],
102-
)
116+
mock_k8s_api = _list_dind_pods_mock()
103117

104118
build = Build(
105119
mock.MagicMock(), api=mock_k8s_api, name='test_build',
@@ -127,19 +141,21 @@ def test_git_credentials_passed_to_podspec_upon_submit():
127141
'client_id': 'my_username',
128142
'access_token': 'my_access_token',
129143
}
144+
145+
mock_k8s_api = _list_dind_pods_mock()
146+
130147
build = Build(
131-
mock.MagicMock(), api=mock.MagicMock(), name='test_build',
148+
mock.MagicMock(), api=mock_k8s_api, name='test_build',
132149
namespace='build_namespace', repo_url=mock.MagicMock(), ref=mock.MagicMock(),
133150
git_credentials=git_credentials, build_image=mock.MagicMock(),
134151
image_name=mock.MagicMock(), push_secret=mock.MagicMock(),
135152
memory_limit=mock.MagicMock(), docker_host='http://mydockerregistry.local',
136153
node_selector=mock.MagicMock())
137154

138-
with mock.patch.object(build, 'api') as api_patch, \
139-
mock.patch.object(build.stop_event, 'is_set', return_value=True):
155+
with mock.patch.object(build.stop_event, 'is_set', return_value=True):
140156
build.submit()
141157

142-
call_args_list = api_patch.create_namespaced_pod.call_args_list
158+
call_args_list = mock_k8s_api.create_namespaced_pod.call_args_list
143159
assert len(call_args_list) == 1
144160

145161
args = call_args_list[0][0]

0 commit comments

Comments
 (0)