1414import time
1515from itertools import groupby
1616from urllib .parse import urljoin
17- from collections import defaultdict
1817
1918from django .conf import settings
2019from django .db import models
@@ -270,7 +269,7 @@ def create(self, *args, **kwargs): # noqa
270269 )
271270
272271 # create required minimum resources in k8s for the application
273- namespace = quota_name = service = self .id
272+ namespace = limits_name = quota_name = service = self .id
274273 try :
275274 self .log ('creating Namespace {} and services' .format (namespace ), level = logging .DEBUG )
276275 # Create essential resources
@@ -290,7 +289,14 @@ def create(self, *args, **kwargs): # noqa
290289 self ._scheduler .quota .get (namespace , quota_name )
291290 except KubeException :
292291 self ._scheduler .quota .create (namespace , quota_name , spec = quota_spec )
293-
292+ if settings .KUBERNETES_NAMESPACE_DEFAULT_LIMIT_RANGES_SPEC != '' :
293+ limits_spec = json .loads (settings .KUBERNETES_NAMESPACE_DEFAULT_LIMIT_RANGES_SPEC )
294+ self .log ('creating LimitRanges {} for namespace {}' .format (limits_name , namespace ),
295+ level = logging .DEBUG )
296+ try :
297+ self ._scheduler .limits .get (namespace , limits_name )
298+ except KubeException :
299+ self ._scheduler .limits .create (namespace , limits_name , spec = limits_spec )
294300 try :
295301 self ._scheduler .svc .get (namespace , service )
296302 except KubeException :
@@ -1096,7 +1102,7 @@ def _get_cpu_allocation(size):
10961102 )
10971103 )
10981104 return "{num}{unit}" .format (
1099- num = math .ceil (int (num ) / cpu_allocation_ratio ),
1105+ num = math .floor (int (num ) / cpu_allocation_ratio ),
11001106 unit = unit
11011107 )
11021108
@@ -1109,24 +1115,10 @@ def _get_ram_allocation(size):
11091115 )
11101116 )
11111117 return "{num}{unit}" .format (
1112- num = math .ceil (int (num ) / ram_allocation_ratio ),
1118+ num = math .floor (int (num ) / ram_allocation_ratio ),
11131119 unit = unit
11141120 )
11151121
1116- def _get_default_resources (self ):
1117- resources = defaultdict (dict )
1118- resources .update (
1119- json .loads (settings .KUBERNETES_POD_DEFAULT_RESOURCES ))
1120- if "cpu" in resources ["limits" ]:
1121- if "cpu" not in resources ["requests" ]:
1122- resources ["requests" ]["cpu" ] = self ._get_cpu_allocation (
1123- resources ["limits" ]["cpu" ])
1124- if "memory" in resources ["limits" ]:
1125- if "memory" not in resources ["requests" ]:
1126- resources ["requests" ]["memory" ] = self ._get_ram_allocation (
1127- resources ["limits" ]["memory" ])
1128- return resources
1129-
11301122 def _gather_app_settings (self , release , app_settings , process_type , replicas , volumes = None ):
11311123 """
11321124 Gathers all required information needed in one easy place for passing into
@@ -1187,7 +1179,7 @@ def _gather_app_settings(self, release, app_settings, process_type, replicas, vo
11871179 'replicas' : replicas ,
11881180 'version' : 'v{}' .format (release .version ),
11891181 'app_type' : process_type ,
1190- 'resources' : self . _get_default_resources ( ),
1182+ 'resources' : json . loads ( settings . KUBERNETES_POD_DEFAULT_RESOURCES ),
11911183 'build_type' : release .build .type ,
11921184 'healthcheck' : healthcheck ,
11931185 'lifecycle_post_start' : config .lifecycle_post_start ,
0 commit comments