Skip to content

Commit 0d61b07

Browse files
committed
chore(scheduler): optimize the kubernetes api
1 parent 72bbb0e commit 0d61b07

43 files changed

Lines changed: 3036 additions & 2727 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

rootfs/api/exceptions.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66
from rest_framework.views import set_rollback, exception_handler
77

88

9+
def error_response(code: str, message: str, details=None, status_code: int = 400) -> Response:
10+
"""
11+
Create a standardized error response.
12+
13+
Format:
14+
{
15+
"error": {
16+
"code": "ERROR_CODE",
17+
"message": "Human readable message",
18+
"details": {} // optional
19+
}
20+
}
21+
"""
22+
error_body = {
23+
"error": {
24+
"code": code,
25+
"message": message,
26+
}
27+
}
28+
if details is not None:
29+
error_body["error"]["details"] = details
30+
return Response(error_body, status=status_code)
31+
32+
933
class HealthcheckException(APIException):
1034
"""Exception class used for when the application's health check fails"""
1135
pass
@@ -36,24 +60,33 @@ def custom_exception_handler(exc, context):
3660
# give more context on the error since DRF masks it as Not Found
3761
if isinstance(exc, Http404):
3862
set_rollback()
39-
return Response(str(exc), status=status.HTTP_404_NOT_FOUND)
63+
return error_response('NOT_FOUND', str(exc), status_code=status.HTTP_404_NOT_FOUND)
4064
# Convert Django ValidationError to DRF 400 response
4165
if isinstance(exc, ValidationError):
4266
set_rollback()
4367
if hasattr(exc, 'message_dict'):
44-
return Response(exc.message_dict, status=status.HTTP_400_BAD_REQUEST)
68+
return error_response(
69+
'VALIDATION_ERROR', 'Validation failed',
70+
exc.message_dict, status.HTTP_400_BAD_REQUEST)
4571
elif hasattr(exc, 'messages'):
46-
return Response({'non_field_errors': exc.messages}, status=status.HTTP_400_BAD_REQUEST)
47-
return Response({'non_field_errors': [str(exc)]}, status=status.HTTP_400_BAD_REQUEST)
72+
return error_response(
73+
'VALIDATION_ERROR', 'Validation failed',
74+
{'non_field_errors': exc.messages},
75+
status.HTTP_400_BAD_REQUEST)
76+
return error_response(
77+
'VALIDATION_ERROR', str(exc),
78+
status_code=status.HTTP_400_BAD_REQUEST)
4879
# Call REST framework's default exception handler after specific 404 handling,
4980
# to get the standard error response.
5081
response = exception_handler(exc, context)
5182
# No response means DRF couldn't handle it, output a generic 500 in a JSON format
5283
if response is None:
5384
logging.exception('Uncaught Exception', exc_info=exc)
5485
set_rollback()
55-
return Response({'detail': 'Server Error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
86+
return error_response(
87+
'INTERNAL_ERROR', 'An internal error occurred',
88+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
5689
# log a few different types of exception instead of using APIException
5790
if isinstance(exc, (DryccException, ServiceUnavailable, HealthcheckException)):
58-
logging.exception(exc.__cause__, exc_info=exc)
91+
logging.exception(str(exc), exc_info=True)
5992
return response

rootfs/api/filer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def _create_job(self, username, password: str) -> str:
6868
k8s_volume.update({"persistentVolumeClaim": {"claimName": self.volume.name}})
6969
else:
7070
k8s_volume.update(self.volume.parameters)
71-
self.scheduler.job.create(self.app_id, job_name, settings.DRYCC_FILER_IMAGE, **{
71+
self.scheduler.job.create(self.app_id, job_name, **{
72+
"image": settings.DRYCC_FILER_IMAGE,
7273
"command": ["init-stack", "/usr/bin/pingguard"],
7374
"args": [
7475
f"--bind=:{self.ports[0]}", f"--interval={settings.DRYCC_FILER_DURATION}s",

0 commit comments

Comments
 (0)