Skip to content

Commit 332f2df

Browse files
committed
ref(scheduler): ditch error() in favour of using KubeHTTPException
1 parent 8bb9772 commit 332f2df

2 files changed

Lines changed: 82 additions & 45 deletions

File tree

rootfs/api/tests/test_release.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,13 @@ def test_release_rollback_failure(self, mock_requests):
320320
with mock.patch('api.models.Release._delete_release_in_scheduler') as mock_kube:
321321
# instead of full request mocking, fake it out in a simple way
322322
class Response(object):
323-
pass
323+
def json(self):
324+
return '{}'
324325

325326
response = Response()
326327
response.status_code = 404
327-
kube_exception = KubeHTTPException(response=response)
328+
response.reason = "Not Found"
329+
kube_exception = KubeHTTPException(response, 'big boom')
328330
mock_kube.side_effect = kube_exception
329331

330332
url = "/v2/apps/test/releases/rollback/"

rootfs/scheduler/__init__.py

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,17 @@ def __init__(self, *args, **kwargs):
301301

302302

303303
class KubeHTTPException(KubeException):
304-
def __init__(self, *args, **kwargs):
305-
self.response = kwargs.pop('response', object)
306-
KubeException.__init__(self, *args, **kwargs)
307-
308-
309-
def error(response, errmsg, *args):
310-
errmsg = errmsg.format(*args)
311-
errmsg = "failed to {}: {} {}\n{}".format(
312-
errmsg,
313-
response.status_code,
314-
response.reason,
315-
response.json()
316-
)
317-
318-
raise KubeHTTPException(errmsg, response=response)
304+
def __init__(self, response, errmsg, *args, **kwargs):
305+
self.response = response
306+
307+
msg = errmsg.format(*args)
308+
msg = "failed to {}: {} {}\n{}".format(
309+
msg,
310+
response.status_code,
311+
response.reason,
312+
response.json()
313+
)
314+
KubeException.__init__(self, msg, *args, **kwargs)
319315

320316

321317
def unhealthy(status_code):
@@ -582,7 +578,7 @@ def run(self, namespace, name, image, entrypoint, command, **kwargs):
582578
url = self._api("/namespaces/{}/pods", namespace)
583579
response = self.session.post(url, json=template)
584580
if unhealthy(response.status_code):
585-
error(response, 'create Pod in Namespace "{}"', namespace)
581+
raise KubeHTTPException(response, 'create Pod in Namespace "{}"', namespace)
586582

587583
labels = {
588584
'app': namespace,
@@ -820,23 +816,23 @@ def _get_namespace_events(self, namespace, **kwargs):
820816
url = self._api("/namespaces/{}/events", namespace)
821817
response = self.session.get(url, params=self._selectors(**kwargs))
822818
if unhealthy(response.status_code):
823-
error(response, "get Events in Namespace {}", namespace)
819+
raise KubeHTTPException(response, "get Events in Namespace {}", namespace)
824820

825821
return response
826822

827823
def _get_namespace(self, namespace):
828824
url = self._api("/namespaces/{}/", namespace)
829825
response = self.session.get(url)
830826
if unhealthy(response.status_code):
831-
error(response, 'get Namespace "{}"', namespace)
827+
raise KubeHTTPException(response, 'get Namespace "{}"', namespace)
832828

833829
return response
834830

835831
def _get_namespaces(self, **kwargs):
836832
url = self._api("/namespaces")
837833
response = self.session.get(url, params=self._selectors(**kwargs))
838834
if unhealthy(response.status_code):
839-
error(response, 'get Namespaces')
835+
raise KubeHTTPException(response, 'get Namespaces')
840836

841837
return response
842838

@@ -852,7 +848,7 @@ def _create_namespace(self, namespace):
852848

853849
response = self.session.post(url, json=data)
854850
if not response.status_code == 201:
855-
error(response, "create Namespace {}".format(namespace))
851+
raise KubeHTTPException(response, "create Namespace {}".format(namespace))
856852

857853
return response
858854

@@ -862,7 +858,7 @@ def _delete_namespace(self, namespace):
862858
if response.status_code == 404:
863859
logger.warn('delete Namespace "{}": not found'.format(namespace))
864860
elif response.status_code != 200:
865-
error(response, 'delete Namespace "{}"', namespace)
861+
raise KubeHTTPException(response, 'delete Namespace "{}"', namespace)
866862

867863
return response
868864

@@ -888,15 +884,21 @@ def _get_rc(self, namespace, name):
888884
url = self._api("/namespaces/{}/replicationcontrollers/{}", namespace, name)
889885
response = self.session.get(url)
890886
if unhealthy(response.status_code):
891-
error(response, 'get ReplicationController "{}" in Namespace "{}"', name, namespace)
887+
raise KubeHTTPException(
888+
response,
889+
'get ReplicationController "{}" in Namespace "{}"', name, namespace
890+
)
892891

893892
return response
894893

895894
def _get_rcs(self, namespace, **kwargs):
896895
url = self._api("/namespaces/{}/replicationcontrollers", namespace)
897896
response = self.session.get(url, params=self._selectors(**kwargs))
898897
if unhealthy(response.status_code):
899-
error(response, 'get ReplicationControllers in Namespace "{}"', namespace)
898+
raise KubeHTTPException(
899+
response,
900+
'get ReplicationControllers in Namespace "{}"', namespace
901+
)
900902

901903
return response
902904

@@ -1111,7 +1113,10 @@ def _create_rc(self, namespace, name, image, command, **kwargs): # noqa
11111113
url = self._api("/namespaces/{}/replicationcontrollers", namespace)
11121114
resp = self.session.post(url, json=template)
11131115
if unhealthy(resp.status_code):
1114-
error(resp, 'create ReplicationController "{}" in Namespace "{}"', name, namespace)
1116+
raise KubeHTTPException(
1117+
resp,
1118+
'create ReplicationController "{}" in Namespace "{}"', name, namespace
1119+
)
11151120
logger.debug('template used: {}'.format(json.dumps(template, indent=4)))
11161121

11171122
self._wait_for_rc_ready(namespace, name)
@@ -1141,16 +1146,18 @@ def _update_rc(self, namespace, name, data):
11411146
url = self._api("/namespaces/{}/replicationcontrollers/{}", namespace, name)
11421147
response = self.session.put(url, json=data)
11431148
if unhealthy(response.status_code):
1144-
error(response, 'scale ReplicationController "{}"', name)
1149+
raise KubeHTTPException(response, 'scale ReplicationController "{}"', name)
11451150

11461151
return response
11471152

11481153
def _delete_rc(self, namespace, name):
11491154
url = self._api("/namespaces/{}/replicationcontrollers/{}", namespace, name)
11501155
response = self.session.delete(url)
11511156
if unhealthy(response.status_code):
1152-
error(response, 'delete ReplicationController "{}" in Namespace "{}"',
1153-
name, namespace)
1157+
raise KubeHTTPException(
1158+
response,
1159+
'delete ReplicationController "{}" in Namespace "{}"', name, namespace
1160+
)
11541161

11551162
return response
11561163

@@ -1283,7 +1290,10 @@ def _get_secret(self, namespace, name):
12831290
url = self._api("/namespaces/{}/secrets/{}", namespace, name)
12841291
response = self.session.get(url)
12851292
if unhealthy(response.status_code):
1286-
error(response, 'get Secret "{}" in Namespace "{}"', name, namespace)
1293+
raise KubeHTTPException(
1294+
response,
1295+
'get Secret "{}" in Namespace "{}"', name, namespace
1296+
)
12871297

12881298
# decode the base64 data
12891299
secrets = response.json()
@@ -1299,7 +1309,7 @@ def _get_secrets(self, namespace, **kwargs):
12991309
url = self._api('/namespaces/{}/secrets', namespace)
13001310
response = self.session.get(url, params=self._selectors(**kwargs))
13011311
if unhealthy(response.status_code):
1302-
error(response, 'get Secrets in Namespace "{}"', namespace)
1312+
raise KubeHTTPException(response, 'get Secrets in Namespace "{}"', namespace)
13031313

13041314
return response
13051315

@@ -1326,7 +1336,10 @@ def _create_secret(self, namespace, name, data, secret_type='Opaque', labels={})
13261336
url = self._api("/namespaces/{}/secrets", namespace)
13271337
response = self.session.post(url, json=template)
13281338
if unhealthy(response.status_code):
1329-
error(response, 'failed to create Secret "{}" in Namespace "{}"', name, namespace)
1339+
raise KubeHTTPException(
1340+
response,
1341+
'failed to create Secret "{}" in Namespace "{}"', name, namespace
1342+
)
13301343

13311344
return response
13321345

@@ -1342,15 +1355,22 @@ def _update_secret(self, namespace, name, data):
13421355
url = self._api("/namespaces/{}/secrets/{}", namespace, name)
13431356
response = self.session.put(url, json=template)
13441357
if unhealthy(response.status_code):
1345-
error(response, 'failed to update Secret "{}" in Namespace "{}"', name, namespace)
1358+
raise KubeHTTPException(
1359+
response,
1360+
'failed to update Secret "{}" in Namespace "{}"',
1361+
name, namespace
1362+
)
13461363

13471364
return response
13481365

13491366
def _delete_secret(self, namespace, name):
13501367
url = self._api("/namespaces/{}/secrets/{}", namespace, name)
13511368
response = self.session.delete(url)
13521369
if unhealthy(response.status_code):
1353-
error(response, 'delete Secret "{}" in Namespace "{}"', name, namespace)
1370+
raise KubeHTTPException(
1371+
response,
1372+
'delete Secret "{}" in Namespace "{}"', name, namespace
1373+
)
13541374

13551375
return response
13561376

@@ -1360,15 +1380,18 @@ def _get_service(self, namespace, name):
13601380
url = self._api("/namespaces/{}/services/{}", namespace, name)
13611381
response = self.session.get(url)
13621382
if unhealthy(response.status_code):
1363-
error(response, 'get Service "{}" in Namespace "{}"', name, namespace)
1383+
raise KubeHTTPException(
1384+
response,
1385+
'get Service "{}" in Namespace "{}"', name, namespace
1386+
)
13641387

13651388
return response
13661389

13671390
def _get_services(self, namespace, **kwargs):
13681391
url = self._api('/namespaces/{}/services', namespace)
13691392
response = self.session.get(url, params=self._selectors(**kwargs))
13701393
if unhealthy(response.status_code):
1371-
error(response, 'get Services in Namespace "{}"', namespace)
1394+
raise KubeHTTPException(response, 'get Services in Namespace "{}"', namespace)
13721395

13731396
return response
13741397

@@ -1384,23 +1407,32 @@ def _create_service(self, namespace, name, data={}, **kwargs):
13841407
url = self._api("/namespaces/{}/services", namespace)
13851408
response = self.session.post(url, json=data)
13861409
if unhealthy(response.status_code):
1387-
error(response, 'create Service "{}" in Namespace "{}"', namespace, namespace)
1410+
raise KubeHTTPException(
1411+
response,
1412+
'create Service "{}" in Namespace "{}"', namespace, namespace
1413+
)
13881414

13891415
return response
13901416

13911417
def _update_service(self, namespace, name, data):
13921418
url = self._api("/namespaces/{}/services/{}", namespace, name)
13931419
response = self.session.put(url, json=data)
13941420
if unhealthy(response.status_code):
1395-
error(response, 'update Service "{}" in Namespace "{}"', namespace, name)
1421+
raise KubeHTTPException(
1422+
response,
1423+
'update Service "{}" in Namespace "{}"', namespace, name
1424+
)
13961425

13971426
return response
13981427

13991428
def _delete_service(self, namespace, name):
14001429
url = self._api("/namespaces/{}/services/{}", namespace, name)
14011430
response = self.session.delete(url)
14021431
if unhealthy(response.status_code):
1403-
error(response, 'delete Service "{}" in Namespace "{}"', name, namespace)
1432+
raise KubeHTTPException(
1433+
response,
1434+
'delete Service "{}" in Namespace "{}"', name, namespace
1435+
)
14041436

14051437
return response
14061438

@@ -1410,23 +1442,23 @@ def _get_pod(self, namespace, name):
14101442
url = self._api("/namespaces/{}/pods/{}", namespace, name)
14111443
response = self.session.get(url)
14121444
if unhealthy(response.status_code):
1413-
error(response, 'get Pod "{}" in Namespace "{}"', name, namespace)
1445+
raise KubeHTTPException(response, 'get Pod "{}" in Namespace "{}"', name, namespace)
14141446

14151447
return response
14161448

14171449
def _get_pods(self, namespace, **kwargs):
14181450
url = self._api('/namespaces/{}/pods', namespace)
14191451
response = self.session.get(url, params=self._selectors(**kwargs))
14201452
if unhealthy(response.status_code):
1421-
error(response, 'get Pods in Namespace "{}"', namespace)
1453+
raise KubeHTTPException(response, 'get Pods in Namespace "{}"', namespace)
14221454

14231455
return response
14241456

14251457
def _delete_pod(self, namespace, name):
14261458
url = self._api("/namespaces/{}/pods/{}", namespace, name)
14271459
resp = self.session.delete(url)
14281460
if unhealthy(resp.status_code):
1429-
error(resp, 'delete Pod "{}" in Namespace "{}"', name, namespace)
1461+
raise KubeHTTPException(resp, 'delete Pod "{}" in Namespace "{}"', name, namespace)
14301462

14311463
# Verify the pod has been deleted
14321464
# Only wait as long as the grace period is - k8s will eventually GC
@@ -1446,7 +1478,10 @@ def _pod_log(self, namespace, name):
14461478
url = self._api("/namespaces/{}/pods/{}/log", namespace, name)
14471479
response = self.session.get(url)
14481480
if unhealthy(response.status_code):
1449-
error(response, 'get logs for Pod "{}" in Namespace "{}"', name, namespace)
1481+
raise KubeHTTPException(
1482+
response,
1483+
'get logs for Pod "{}" in Namespace "{}"', name, namespace
1484+
)
14501485

14511486
return response
14521487

@@ -1620,15 +1655,15 @@ def _get_nodes(self, **kwargs):
16201655
url = self._api('/nodes')
16211656
response = self.session.get(url, params=self._selectors(**kwargs))
16221657
if unhealthy(response.status_code):
1623-
error(response, 'get Nodes')
1658+
raise KubeHTTPException(response, 'get Nodes')
16241659

16251660
return response
16261661

16271662
def _get_node(self, name, **kwargs):
16281663
url = self._api('/nodes/{}'.format(name))
16291664
response = self.session.get(url)
16301665
if unhealthy(response.status_code):
1631-
error(response, 'get Node {} in Nodes'.format(name))
1666+
raise KubeHTTPException(response, 'get Node {} in Nodes', name)
16321667

16331668
return response
16341669

0 commit comments

Comments
 (0)