Skip to content

Commit 651c519

Browse files
committed
fix(views): use Exception instead of RuntimeError in view around rollbacks
1 parent 145ef31 commit 651c519

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

rootfs/api/models/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def restart(self, **kwargs): # noqa
213213
while True:
214214
# timed out
215215
if elapsed >= timeout:
216-
raise RuntimeError('timeout - 5 minutes have passed and pods are not up')
216+
raise KubeException('timeout - 5 minutes have passed and pods are not up')
217217

218218
# restarting a single pod behaves differently, fetch the *newest* pod
219219
# and hope it is the right one. Comes back sorted

rootfs/api/views.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from api import authentication, models, permissions, serializers, viewsets
2020
from api.models import AlreadyExists
21+
from scheduler import KubeException
2122

2223
import requests
2324
import logging
@@ -152,7 +153,7 @@ def create(self, request, *args, **kwargs):
152153
try:
153154
return super(BaseDeisViewSet, self).create(request, *args, **kwargs)
154155
# If the scheduler oopsie'd
155-
except RuntimeError as e:
156+
except KubeException as e:
156157
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
157158

158159

@@ -230,7 +231,7 @@ def scale(self, request, **kwargs):
230231
status=status.HTTP_400_BAD_REQUEST)
231232
except (EnvironmentError, ValidationError) as e:
232233
return Response({'detail': str(e)}, status=status.HTTP_400_BAD_REQUEST)
233-
except RuntimeError as e:
234+
except KubeException as e:
234235
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
235236
return Response(status=status.HTTP_204_NO_CONTENT)
236237

@@ -260,7 +261,7 @@ def run(self, request, **kwargs):
260261
rc, output = app.run(self.request.user, request.data['command'])
261262
except EnvironmentError as e:
262263
return Response({'detail': str(e)}, status=status.HTTP_400_BAD_REQUEST)
263-
except RuntimeError as e:
264+
except KubeException as e:
264265
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
265266
return Response({'rc': rc, 'output': str(output)})
266267

@@ -322,7 +323,7 @@ def list(self, *args, **kwargs):
322323
# fake out pagination for now
323324
pagination = {'results': data, 'count': len(data)}
324325
return Response(pagination, status=status.HTTP_200_OK)
325-
except Exception as e:
326+
except KubeException as e:
326327
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
327328

328329
def restart(self, *args, **kwargs):
@@ -335,7 +336,7 @@ def restart(self, *args, **kwargs):
335336
# pagination = {'results': data, 'count': len(data)}
336337
pagination = data
337338
return Response(pagination, status=status.HTTP_200_OK)
338-
except Exception as e:
339+
except KubeException as e:
339340
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
340341

341342

@@ -368,7 +369,7 @@ def attach(self, request, *args, **kwargs):
368369
raise
369370
except AlreadyExists as e:
370371
return Response({'detail': str(e)}, status=status.HTTP_409_CONFLICT)
371-
except Exception as e:
372+
except KubeException as e:
372373
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
373374

374375
return Response(status=status.HTTP_201_CREATED)
@@ -378,7 +379,7 @@ def detach(self, request, *args, **kwargs):
378379
self.get_object().detach(*args, **kwargs)
379380
except Http404:
380381
raise
381-
except Exception as e:
382+
except KubeException as e:
382383
return Response({'detail': str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
383384

384385
return Response(status=status.HTTP_204_NO_CONTENT)
@@ -417,7 +418,7 @@ def rollback(self, request, **kwargs):
417418
return Response(response, status=status.HTTP_201_CREATED)
418419
except EnvironmentError as e:
419420
return Response({'detail': str(e)}, status=status.HTTP_400_BAD_REQUEST)
420-
except RuntimeError:
421+
except Exception:
421422
if 'new_release' in locals():
422423
new_release.delete()
423424
raise

rootfs/scheduler/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def deploy(self, namespace, name, image, command, **kwargs):
394394
if old_rc:
395395
self._scale_rc(namespace, old_rc["metadata"]["name"], desired)
396396

397-
raise RuntimeError('{} (scheduler::deploy): {}'.format(name, e))
397+
raise KubeException('{} (scheduler::deploy): {}'.format(name, e))
398398

399399
# New release is live and kicking. Clean up old release
400400
if old_rc:
@@ -438,7 +438,7 @@ def _update_application_service(self, namespace, name, app_type, image, routable
438438
except Exception as e:
439439
# Fix service to old port and app type
440440
self._update_service(namespace, namespace, data=old_service)
441-
raise RuntimeError('{} (scheduler::deploy::service_update): {}'.format(name, e))
441+
raise KubeException('{} (scheduler::deploy::service_update): {}'.format(name, e))
442442

443443
def scale(self, namespace, name, image, command, **kwargs):
444444
logger.debug('scale {}, img {}, params {}, cmd "{}"'.format(name, image, kwargs, command))
@@ -451,15 +451,15 @@ def scale(self, namespace, name, image, command, **kwargs):
451451
self._create_rc(namespace, name, image, command, **kwargs)
452452
except KubeException as e:
453453
logger.debug("Creating RC failed because of: {}".format(str(e)))
454-
raise RuntimeError('{} (RC): {}'.format(name, e))
454+
raise KubeException('{} (RC): {}'.format(name, e))
455455

456456
try:
457457
self._scale_rc(namespace, name, replicas)
458458
except KubeException as e:
459459
logger.debug("Scaling failed because of: {}".format(str(e)))
460460
old = self._get_rc(namespace, name).json()
461461
self._scale_rc(namespace, name, old['spec']['replicas'])
462-
raise RuntimeError('{} (Scale): {}'.format(name, e))
462+
raise KubeException('{} (Scale): {}'.format(name, e))
463463

464464
def create(self, namespace, **kwargs):
465465
"""Create a basic structure for an application in k8s"""

0 commit comments

Comments
 (0)