Skip to content

Commit 6c38240

Browse files
committed
chore(pod): add pod default resources support
1 parent 83d3451 commit 6c38240

6 files changed

Lines changed: 37 additions & 25 deletions

File tree

rootfs/api/models/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def create(self, *args, **kwargs): # noqa
286286
try:
287287
self._scheduler.quota.get(namespace, quota_name)
288288
except KubeException:
289-
self._scheduler.quota.create(namespace, quota_name, data=quota_spec)
289+
self._scheduler.quota.create(namespace, quota_name, spec=quota_spec)
290290

291291
try:
292292
self._scheduler.svc.get(namespace, service)
@@ -1100,6 +1100,9 @@ def _gather_app_settings(self, release, app_settings, process_type, replicas):
11001100
pod_termination_grace_period_seconds = int(config.values.get(
11011101
'KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS', settings.KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS)) # noqa
11021102

1103+
# get pod default resources
1104+
pod_default_resources = json.loads(settings.KUBERNETES_POD_DEFAULT_RESOURCES)
1105+
11031106
# set the image pull policy that is associated with the application container
11041107
image_pull_policy = config.values.get('IMAGE_PULL_POLICY', settings.IMAGE_PULL_POLICY)
11051108

@@ -1123,6 +1126,7 @@ def _gather_app_settings(self, release, app_settings, process_type, replicas):
11231126
'replicas': replicas,
11241127
'version': 'v{}'.format(release.version),
11251128
'app_type': process_type,
1129+
'resources': pod_default_resources,
11261130
'build_type': release.build.type,
11271131
'healthcheck': healthcheck,
11281132
'lifecycle_post_start': config.lifecycle_post_start,

rootfs/api/settings/production.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,34 @@
333333
# How long k8s waits for a pod to finish work after a SIGTERM before sending SIGKILL
334334
KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS = int(os.environ.get('KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS', 30)) # noqa
335335

336+
# Default pod spec for application
337+
KUBERNETES_POD_DEFAULT_RESOURCES = os.environ.get(
338+
'KUBERNETES_POD_DEFAULT_RESOURCES',
339+
json.dumps({
340+
"requests": {
341+
"cpu": "256m",
342+
"memory": "256Mi"
343+
},
344+
"limits": {
345+
"cpu": "512m",
346+
"memory": "512Mi"
347+
},
348+
})
349+
)
350+
336351
# Default quota spec for application namespace
337352
KUBERNETES_NAMESPACE_DEFAULT_QUOTA_SPEC = os.environ.get(
338353
'KUBERNETES_NAMESPACE_DEFAULT_QUOTA_SPEC',
339-
json.dumps({"spec": {
354+
json.dumps({
340355
"hard": {
341-
"cpu": "4",
342-
"memory": "4Gi",
343-
"pods": "2",
356+
"cpu": "8",
357+
"pods": "16",
358+
"memory": "32Gi",
344359
"ephemeral-storage": "1Gi",
345360
"requests.storage": "10Gi",
346361
"persistentvolumeclaims": 8,
347362
}
348-
}})
363+
})
349364
)
350365

351366
# registry settings

rootfs/scheduler/resources/pod.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ def manifest(self, namespace, name, image, **kwargs):
119119
},
120120
'spec': {}
121121
}
122-
123122
# pod manifest spec
124123
spec = manifest['spec']
125124

@@ -231,8 +230,8 @@ def _set_resources(self, container, kwargs):
231230
app_type = kwargs.get("app_type")
232231
mem = kwargs.get("memory", {}).get(app_type)
233232
cpu = kwargs.get("cpu", {}).get(app_type)
233+
resources = kwargs.get("resources", defaultdict(dict))
234234
if mem or cpu:
235-
resources = defaultdict(dict)
236235
if mem:
237236
if "/" in mem:
238237
parts = mem.split("/")
@@ -248,8 +247,7 @@ def _set_resources(self, container, kwargs):
248247
resources["limits"]["cpu"] = parts[1].lower()
249248
else:
250249
resources["limits"]["cpu"] = cpu.lower()
251-
if resources:
252-
container["resources"] = dict(resources)
250+
container["resources"] = dict(resources)
253251

254252
@staticmethod
255253
def _get_termination_grace_period(kwargs):

rootfs/scheduler/resources/quota.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from scheduler.exceptions import KubeHTTPException
22
from scheduler.resources import Resource
3-
from scheduler.utils import dict_merge
43

54

65
class Quota(Resource):
@@ -35,11 +34,9 @@ def create(self, namespace_name, name, **kwargs):
3534
'heritage': 'drycc'
3635
},
3736
},
38-
'spec': {}
37+
'spec': kwargs.get('spec', {})
3938
}
40-
41-
data = dict_merge(manifest, kwargs.get('data', {}))
42-
response = self.http_post(url, json=data)
39+
response = self.http_post(url, json=manifest)
4340
if not response.status_code == 201:
4441
raise KubeHTTPException(response,
4542
"create quota {} for namespace {}".format(

rootfs/scheduler/tests/test_pod_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ def _assert_resources(self, caze, manifest):
5252
if expected:
5353
self.assertEqual(resources_parent["resources"], expected, caze)
5454
else:
55-
self.assertTrue("resources" not in resources_parent, caze)
55+
self.assertTrue(resources_parent["resources"] == {}, caze)

rootfs/scheduler/tests/test_quota.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ class QuotaTest(TestCase):
1111

1212
def test_create_quota(self):
1313
namespace_name = self.create_namespace()
14-
quota = {
15-
'spec': {
16-
'hard': {
17-
'cpu': '3',
18-
'pods': '10',
19-
'secrets': '5'
20-
}
14+
spec = {
15+
'hard': {
16+
'cpu': '3',
17+
'pods': '10',
18+
'secrets': '5'
2119
}
2220
}
23-
self.scheduler.quota.create(namespace_name, 'test1', data=quota)
21+
self.scheduler.quota.create(namespace_name, 'test1', spec=spec)
2422

2523
response = self.scheduler.quota.get(namespace_name, 'test1')
2624
data = response.json()
27-
self.assertEqual(data.get('spec', {}), quota['spec'])
25+
self.assertEqual(data.get('spec', {}), spec)
2826
self.assertEqual(data['metadata']['namespace'], namespace_name)
2927

3028
def test_create_with_nonexistent_namespace(self):

0 commit comments

Comments
 (0)