Skip to content

Commit 2885a1e

Browse files
committed
fix(limits): always set default
1 parent 7391edf commit 2885a1e

2 files changed

Lines changed: 59 additions & 11 deletions

File tree

rootfs/api/models/app.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -738,19 +738,27 @@ def _scale_pods(self, scale_types, release, app_settings):
738738
self.log(err, logging.ERROR)
739739
raise ServiceUnavailable(err) from e
740740

741-
def _set_default_config(self, config=None, procfile_types=None):
742-
procfile_types = self.procfile_types if procfile_types is None else procfile_types
741+
def _set_default_limit(self, config, procfile_type):
742+
if procfile_type not in config.limits:
743+
plan = LimitPlan.get_default()
744+
config.limits[procfile_type] = plan.id
745+
config.save(update_fields=['limits'])
746+
return config
747+
748+
def _set_default_config(self):
743749
plan = LimitPlan.get_default()
744750
limits = {PROCFILE_TYPE_WEB: plan.id, PROCFILE_TYPE_RUN: plan.id}
745751
try:
746-
config = self.config_set.latest() if config is None else config
747-
for procfile_type in procfile_types:
748-
limits[procfile_type] = config.limits.get(procfile_type, plan.id)
749-
if limits != config.limits:
750-
config.limits = limits
751-
config.save(update_fields=['limits'])
752+
config = self.config_set.latest()
753+
limits[PROCFILE_TYPE_WEB] = config.limits.get(PROCFILE_TYPE_WEB, plan.id)
754+
limits[PROCFILE_TYPE_RUN] = config.limits.get(PROCFILE_TYPE_RUN, plan.id)
752755
except Config.DoesNotExist:
753756
config = Config.objects.create(owner=self.owner, app=self, limits=limits)
757+
for procfile_type in self.procfile_types:
758+
limits[procfile_type] = config.limits.get(procfile_type, plan.id)
759+
if limits != config.limits:
760+
config.limits = limits
761+
config.save(update_fields=['limits'])
754762
return config
755763

756764
def _create_default_ingress(self, target_port):
@@ -993,9 +1001,8 @@ def _gather_app_settings(self, release, app_settings, procfile_type, replicas, v
9931001
"""
9941002

9951003
envs = self._build_env_vars(release)
996-
config = release.config
9971004
# Obtain a limit plan that must exist, if raise error here, it must be a bug
998-
self._set_default_config(config, procfile_types=[procfile_type])
1005+
config = self._set_default_limit(release.config, procfile_type)
9991006
limit_plan = LimitPlan.objects.get(id=config.limits.get(procfile_type))
10001007

10011008
# see if the app config has deploy batch preference, otherwise use global

rootfs/api/tests/test_config.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ def test_unset_limits_error(self, mock_requests):
473473
# dockerfile + procfile worflow
474474
app = App.objects.get(id=app_id)
475475
user = User.objects.get(username='autotest')
476-
build = Build.objects.create(owner=user, app=app, image="qwerty")
477476
build = Build.objects.create(
478477
owner=user,
479478
app=app,
@@ -545,3 +544,45 @@ def test_measure_config(self, *args, **kwargs):
545544
out = self.call_command()
546545
self.assertIn(out, "done\n")
547546
self.assertEqual(response.status_code, 201)
547+
548+
def test_set_config_limits_run(self, *args, **kwargs):
549+
# create
550+
app_id = self.create_app()
551+
# dockerfile + procfile worflow
552+
app = App.objects.get(id=app_id)
553+
user = User.objects.get(username='autotest')
554+
build = Build.objects.create(
555+
owner=user,
556+
app=app,
557+
image="qwerty",
558+
procfile={
559+
'web': 'node server.js',
560+
'worker': 'node worker.js'
561+
},
562+
dockerfile='foo',
563+
sha='somereallylongsha'
564+
)
565+
# create an initial release
566+
release = Release.objects.create(
567+
version=3,
568+
owner=user,
569+
app=app,
570+
config=app.config_set.latest(),
571+
build=build
572+
)
573+
# deploy
574+
app.pipeline(release)
575+
body = {
576+
'values': json.dumps({'PORT': 5000}),
577+
'limits': {
578+
PROCFILE_TYPE_RUN: 'std1.large.c2m4',
579+
PROCFILE_TYPE_WEB: 'std1.large.c2m4',
580+
},
581+
}
582+
url = f"/v2/apps/{app_id}/config"
583+
response = self.client.post(url, body)
584+
url = "/v2/apps/{app_id}/config".format(**locals())
585+
response = self.client.get(url)
586+
self.assertEqual(response.status_code, 200, response.data)
587+
expect = {'run': 'std1.large.c2m4', 'web': 'std1.large.c2m4', 'worker': 'std1.large.c1m1'}
588+
self.assertEqual(expect, response.json()["limits"], response.data)

0 commit comments

Comments
 (0)