Skip to content

Commit 1ae9b14

Browse files
committed
ref(scheduler): unify how information is applied to the application container object in a Pod template
This also allowed me to move healthchecks (default and otherwise) under the new _set_container and have deis run make use of it
1 parent 267bab7 commit 1ae9b14

1 file changed

Lines changed: 42 additions & 49 deletions

File tree

rootfs/scheduler/__init__.py

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,18 @@ def run(self, namespace, name, image, entrypoint, command, **kwargs):
565565
else:
566566
args = [command[1:-1]]
567567

568-
containers = template['spec']['containers'][0]
569-
containers['command'] = [entrypoint]
570-
containers['args'] = args
568+
spec = template['spec']
571569

572-
self._set_environment(containers, namespace, **kwargs)
570+
# apply tags as needed to restrict pod to particular node(s)
571+
spec["nodeSelector"] = kwargs.get('tags', {})
572+
573+
container = spec['containers'][0]
574+
container['command'] = [entrypoint]
575+
container['args'] = args
576+
577+
# set information to the application container
578+
kwargs['image'] = l['image']
579+
self._set_container(namespace, container, **kwargs)
573580

574581
url = self._api("/namespaces/{}/pods", namespace)
575582
response = self.session.post(url, json=template)
@@ -613,7 +620,8 @@ def run(self, namespace, name, image, entrypoint, command, **kwargs):
613620

614621
return 0, data
615622

616-
def _set_environment(self, data, namespace, **kwargs):
623+
def _set_container(self, namespace, data, **kwargs): # noqa
624+
"""Set app container information (env, healthcheck, etc) on a Pod"""
617625
app_type = kwargs.get('app_type')
618626
mem = kwargs.get('memory', {}).get(app_type)
619627
cpu = kwargs.get('cpu', {}).get(app_type)
@@ -638,7 +646,7 @@ def _set_environment(self, data, namespace, **kwargs):
638646
'version': kwargs.get('version'),
639647
'type': 'env'
640648
}
641-
self._create_secret(namespace, secret_name, secrets_env, labels)
649+
self._create_secret(namespace, secret_name, secrets_env, labels=labels)
642650
else:
643651
self._update_secret(namespace, secret_name, secrets_env)
644652

@@ -674,6 +682,12 @@ def _set_environment(self, data, namespace, **kwargs):
674682
if cpu:
675683
data["resources"]["limits"]["cpu"] = cpu
676684

685+
# add in healthchecks
686+
if kwargs.get('healthcheck', None):
687+
self._healthcheck(namespace, data, kwargs.get('routable'), **kwargs['healthcheck'])
688+
else:
689+
self._default_readiness_probe(data, kwargs.get('build_type'), kwargs.get('image'))
690+
677691
def resolve_state(self, pod):
678692
# See "Pod Phase" at http://kubernetes.io/v1.1/docs/user-guide/pod-states.html
679693
if pod is None:
@@ -870,9 +884,9 @@ def _wait_until_pods_terminate(self, namespace, labels, current, desired):
870884

871885
time.sleep(1)
872886

873-
logger.debug("{} pods in namespace {} are terminated".format(delta, namespace)) # noqa
887+
logger.debug("{} pods in namespace {} are terminated".format(delta, namespace))
874888

875-
def _get_pod_ready_status(self, namespace, controller, labels, desired):
889+
def _wait_until_pods_are_ready(self, namespace, controller, labels, desired): # noqa
876890
# If desired is 0 then there is no ready state to check on
877891
if desired == 0:
878892
return
@@ -991,7 +1005,7 @@ def _scale_rc(self, namespace, name, desired):
9911005
logger.debug("RC {} has a new resource version {}".format(name, js_template["metadata"]["resourceVersion"])) # noqa
9921006

9931007
# Double check enough pods are in the required state to service the application
994-
self._get_pod_ready_status(namespace, rc, labels, desired)
1008+
self._wait_until_pods_are_ready(namespace, rc, labels, desired)
9951009

9961010
# if it was a scale down operation, wait until terminating pods are done
9971011
if int(desired) < int(current):
@@ -1036,21 +1050,18 @@ def _create_rc(self, namespace, name, image, command, **kwargs): # noqa
10361050

10371051
template = json.loads(string.Template(TEMPLATE).substitute(l))
10381052

1039-
# apply tags as needed
1040-
tags = kwargs.get('tags', {})
1041-
template["spec"]["template"]["spec"]["nodeSelector"] = tags
1053+
spec = template["spec"]["template"]["spec"]
1054+
1055+
# apply tags as needed to restrict pod to particular node(s)
1056+
spec["nodeSelector"] = kwargs.get('tags', {})
10421057

10431058
# Deal with container information
1044-
container = template["spec"]["template"]["spec"]["containers"][0]
1059+
container = spec["containers"][0]
10451060
container['args'] = args
10461061

1047-
self._set_environment(container, namespace, **kwargs)
1048-
1049-
# add in healthchecks
1050-
if kwargs.get('healthcheck'):
1051-
template = self._healthcheck(template, kwargs['routable'], **kwargs['healthcheck'])
1052-
else:
1053-
template = self._default_readiness_probe(template, image, kwargs.get('build_type'))
1062+
# set information to the application container
1063+
kwargs['image'] = l['image']
1064+
self._set_container(namespace, container, **kwargs)
10541065

10551066
url = self._api("/namespaces/{}/replicationcontrollers", namespace)
10561067
resp = self.session.post(url, json=template)
@@ -1098,8 +1109,9 @@ def _delete_rc(self, namespace, name):
10981109

10991110
return response
11001111

1101-
def _healthcheck(self, controller, routable=False, path='/', port=5000, delay=30, timeout=5,
1102-
period_seconds=1, success_threshold=1, failure_threshold=3): # noqa
1112+
def _healthcheck(self, namespace, container, routable=False, path='/', port=5000,
1113+
delay=30, timeout=5, period_seconds=1, success_threshold=1,
1114+
failure_threshold=3): # noqa
11031115
"""
11041116
Apply HTTP GET healthcehck to the application container
11051117
@@ -1108,18 +1120,16 @@ def _healthcheck(self, controller, routable=False, path='/', port=5000, delay=30
11081120
http://kubernetes.io/docs/user-guide/liveness/
11091121
"""
11101122
if not routable:
1111-
return controller
1123+
return
11121124

1113-
namespace = controller['spec']['selector']['app']
1114-
# Inspect if a PORT env is already defined, make sure that's the port used
11151125
try:
11161126
service = self._get_service(namespace, namespace).json()
11171127
port = service['spec']['ports'][0]['targetPort']
11181128
except:
11191129
pass
11201130

11211131
# Only support HTTP checks for now
1122-
# http://kubernetes.io/v1.1/docs/user-guide/pod-states.html#container-probes
1132+
# http://kubernetes.io/docs/user-guide/pod-states/#container-probes
11231133
healthcheck = {
11241134
# defines the health checking
11251135
'livenessProbe': {
@@ -1153,32 +1163,14 @@ def _healthcheck(self, controller, routable=False, path='/', port=5000, delay=30
11531163
}
11541164

11551165
# Update only the application container with the health check
1156-
app_type = controller['spec']['selector']['type']
1157-
container_name = '{}-{}'.format(namespace, app_type)
1158-
containers = controller['spec']['template']['spec']['containers']
1159-
for container in containers:
1160-
if container['name'] == container_name:
1161-
container.update(healthcheck)
1162-
1163-
return controller
1166+
container.update(healthcheck)
11641167

1165-
def _default_readiness_probe(self, controller, image, build_type):
1168+
def _default_readiness_probe(self, container, build_type, image):
1169+
# Update only the application container with the health check
11661170
if build_type == "buildpack":
1167-
readinessprobe = self._default_buildpack_readiness_probe()
1171+
container.update(self._default_buildpack_readiness_probe())
11681172
else:
1169-
readinessprobe = self._default_dockerapp_readiness_probe(image)
1170-
if readinessprobe is None:
1171-
return controller
1172-
# Update only the application container with the health check
1173-
app_type = controller['spec']['selector']['type']
1174-
namespace = controller['spec']['selector']['app']
1175-
container_name = '{}-{}'.format(namespace, app_type)
1176-
containers = controller['spec']['template']['spec']['containers']
1177-
for container in containers:
1178-
if container['name'] == container_name:
1179-
container.update(readinessprobe)
1180-
1181-
return controller
1173+
container.update(self._default_dockerapp_readiness_probe(image))
11821174

11831175
'''
11841176
Applies exec readiness probe to the slugrunner container.
@@ -1229,6 +1221,7 @@ def _default_dockerapp_readiness_probe(self, image, delay=5, timeout=5, period_s
12291221
return None
12301222
except Exception:
12311223
return None
1224+
12321225
readinessprobe = {
12331226
'readinessProbe': {
12341227
# an exec probe

0 commit comments

Comments
 (0)