|
4 | 4 |
|
5 | 5 | import contextlib |
6 | 6 | import dataclasses |
| 7 | +import datetime |
7 | 8 | import functools |
8 | 9 | import logging |
9 | 10 | import math |
@@ -1243,6 +1244,17 @@ def clusters(request, tenant): |
1243 | 1244 | context={"session": session, "cluster_manager": cluster_manager}, |
1244 | 1245 | ) |
1245 | 1246 | input_serializer.is_valid(raise_exception=True) |
| 1247 | + |
| 1248 | + if not _check_max_platform_duration(input_serializer.validated_data): |
| 1249 | + return response.Response( |
| 1250 | + { |
| 1251 | + "detail": "Platform exceeds max duration of " |
| 1252 | + + str(cloud_settings.SCHEDULING.MAX_PLATFORM_DURATION_HOURS) |
| 1253 | + + " hours." |
| 1254 | + }, |
| 1255 | + status=status.HTTP_409_CONFLICT, |
| 1256 | + ) |
| 1257 | + |
1246 | 1258 | # Check that the cluster fits within quota |
1247 | 1259 | calculator = scheduling.CaaSClusterCalculator(session) |
1248 | 1260 | resources = calculator.calculate( |
@@ -1483,6 +1495,21 @@ def kubernetes_cluster_template_details(request, tenant, template): |
1483 | 1495 | return response.Response(serializer.data) |
1484 | 1496 |
|
1485 | 1497 |
|
| 1498 | +def _check_max_platform_duration(platform_data): |
| 1499 | + if ( |
| 1500 | + cloud_settings.SCHEDULING.MAX_PLATFORM_DURATION_HOURS is None |
| 1501 | + or not cloud_settings.SCHEDULING.ENABLED |
| 1502 | + ): |
| 1503 | + return True |
| 1504 | + end_time = platform_data["schedule"].end_time |
| 1505 | + now = datetime.datetime.now(tz=datetime.timezone.utc) |
| 1506 | + duration = (end_time - now).total_seconds() / 3600 |
| 1507 | + if duration < cloud_settings.SCHEDULING.MAX_PLATFORM_DURATION_HOURS: |
| 1508 | + return True |
| 1509 | + else: |
| 1510 | + return False |
| 1511 | + |
| 1512 | + |
1486 | 1513 | def kubernetes_cluster_check_quotas(session, cluster, template, **data): |
1487 | 1514 | """ |
1488 | 1515 | Check the quotas for a Kubernetes cluster. |
@@ -1642,6 +1669,15 @@ def kubernetes_clusters(request, tenant): |
1642 | 1669 | context={"session": session, "capi_session": capi_session}, |
1643 | 1670 | ) |
1644 | 1671 | input_serializer.is_valid(raise_exception=True) |
| 1672 | + if not _check_max_platform_duration(input_serializer.validated_data): |
| 1673 | + return response.Response( |
| 1674 | + { |
| 1675 | + "detail": "Platform exceeds max duration of " |
| 1676 | + + str(cloud_settings.SCHEDULING.MAX_PLATFORM_DURATION_HOURS) |
| 1677 | + + " hours." |
| 1678 | + }, |
| 1679 | + status=status.HTTP_409_CONFLICT, |
| 1680 | + ) |
1645 | 1681 | # Check that the cluster fits within quota |
1646 | 1682 | resources, fits, _ = kubernetes_cluster_check_quotas( |
1647 | 1683 | session, None, **input_serializer.validated_data |
|
0 commit comments