Skip to content

Commit 64bc574

Browse files
committed
fix(scheduler): set env, limits, tags for run containers
1 parent 63f859d commit 64bc574

5 files changed

Lines changed: 49 additions & 36 deletions

File tree

rootfs/api/models/container.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ def run(self, command):
134134
raise EnvironmentError('No build associated with this release '
135135
'to run this command')
136136
image = self.release.image
137+
kwargs = {'memory': self.release.config.memory,
138+
'cpu': self.release.config.cpu,
139+
'tags': self.release.config.tags,
140+
'envs': self.release.config.values}
137141
entrypoint = '/bin/bash'
138142
# if this is a procfile-based app, switch the entrypoint to slugrunner's default
139143
# FIXME: remove slugrunner's hardcoded entrypoint
@@ -145,7 +149,7 @@ def run(self, command):
145149
else:
146150
command = "-c '{}'".format(command)
147151
try:
148-
rc, output = self._scheduler.run(self.job_id, image, entrypoint, command)
152+
rc, output = self._scheduler.run(self.job_id, image, entrypoint, command, **kwargs)
149153
return rc, output
150154
except Exception as e:
151155
err = '{} (run): {}'.format(self.job_id, e)

rootfs/scheduler/__init__.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def destroy(self, namespace):
446446
except KubeException:
447447
break
448448

449-
def run(self, name, image, entrypoint, command):
449+
def run(self, name, image, entrypoint, command, **kwargs):
450450
"""Run a one-off command."""
451451
logger.debug('run {}, img {}, entrypoint {}, cmd "{}"'.format(
452452
name, image, entrypoint, command))
@@ -470,8 +470,12 @@ def run(self, name, image, entrypoint, command):
470470
args = [command[1:-1]]
471471

472472
js_template = json.loads(template)
473-
js_template['spec']['containers'][0]['command'] = [entrypoint]
474-
js_template['spec']['containers'][0]['args'] = args
473+
containers = js_template['spec']['containers'][0]
474+
containers['command'] = [entrypoint]
475+
containers['args'] = args
476+
477+
self._set_environment(containers, **kwargs)
478+
475479
url = self._api("/namespaces/{}/pods", appname)
476480
resp = self.session.post(url, json=js_template)
477481
if unhealthy(resp.status_code):
@@ -508,6 +512,39 @@ def run(self, name, image, entrypoint, command):
508512
return err_code, data
509513
return 0, data
510514

515+
def _set_environment(self, json_data, **kwargs):
516+
app_type = kwargs.get('app_type')
517+
mem = kwargs.get('memory', {}).get(app_type)
518+
cpu = kwargs.get('cpu', {}).get(app_type)
519+
env = kwargs.get('envs', {})
520+
521+
if env:
522+
for key, value in env.items():
523+
json_data["env"].append({
524+
"name": key,
525+
"value": str(value)
526+
})
527+
528+
# Inject debugging if workflow is in debug mode
529+
if os.environ.get("DEBUG", False):
530+
json_data["env"].append({
531+
"name": "DEBUG",
532+
"value": "1"
533+
})
534+
535+
if mem or cpu:
536+
json_data["resources"] = {"limits": {}}
537+
538+
if mem:
539+
if mem[-2:-1].isalpha() and mem[-1].isalpha():
540+
mem = mem[:-1]
541+
542+
mem = mem + "i"
543+
json_data["resources"]["limits"]["memory"] = mem
544+
545+
if cpu:
546+
json_data["resources"]["limits"]["cpu"] = cpu
547+
511548
def state(self, name):
512549
"""Display the state of a container."""
513550
try:
@@ -896,36 +933,8 @@ def _create_rc(self, namespace, name, image, command, **kwargs): # noqa
896933
containers[0]['args'] = args
897934
loc = locals().copy()
898935
loc.update(re.match(MATCH, container_fullname).groupdict())
899-
mem = kwargs.get('memory', {}).get(app_type)
900-
cpu = kwargs.get('cpu', {}).get(app_type)
901-
env = kwargs.get('envs', {})
902936

903-
if env:
904-
for key, value in env.items():
905-
containers[0]["env"].append({
906-
"name": key,
907-
"value": str(value)
908-
})
909-
910-
# Inject debugging if workflow is in debug mode
911-
if os.environ.get("DEBUG", False):
912-
containers[0]["env"].append({
913-
"name": "DEBUG",
914-
"value": "1"
915-
})
916-
917-
if mem or cpu:
918-
containers[0]["resources"] = {"limits": {}}
919-
920-
if mem:
921-
if mem[-2:-1].isalpha() and mem[-1].isalpha():
922-
mem = mem[:-1]
923-
924-
mem = mem+"i"
925-
containers[0]["resources"]["limits"]["memory"] = mem
926-
927-
if cpu:
928-
containers[0]["resources"]["limits"]["cpu"] = cpu
937+
self._set_environment(containers[0], **kwargs)
929938

930939
# add in healtchecks
931940
if kwargs.get('healthcheck'):

rootfs/scheduler/abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def destroy(self, name):
1515
"""Destroy a container."""
1616
raise NotImplementedError
1717

18-
def run(self, name, image, entrypoint, command):
18+
def run(self, name, image, entrypoint, command, **kwargs):
1919
"""Run a one-off command."""
2020
raise NotImplementedError
2121

rootfs/scheduler/chaos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def destroy(self, name):
2626
else:
2727
super(ChaosSchedulerClient, self).destroy(name)
2828

29-
def run(self, name, image, entrypoint, command):
29+
def run(self, name, image, entrypoint, command, **kwargs):
3030
"""Run a one-off command."""
3131
if random.random() < CREATE_ERROR_RATE:
3232
raise RuntimeError('exit code 1')

rootfs/scheduler/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def destroy(self, name):
2222
"""Destroy a container."""
2323
jobs.setdefault(name, {})['state'] = JobState.destroyed
2424

25-
def run(self, name, image, entrypoint, command):
25+
def run(self, name, image, entrypoint, command, **kwargs):
2626
"""Run a one-off command."""
2727
# dump input into a json object for testing purposes
2828
return 0, json.dumps({

0 commit comments

Comments
 (0)