Skip to content

Commit 5d23d12

Browse files
author
Keerthan Mala
committed
feat(dockerapp): add default readiness check for the docker file app
1 parent 9213747 commit 5d23d12

1 file changed

Lines changed: 52 additions & 16 deletions

File tree

rootfs/scheduler/__init__.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ def _update_application_service(self, namespace, name, app_type, image, routable
427427
# Find if target port exists already, update / create as required
428428
if routable:
429429
port = self._get_port(image)
430+
if port is None:
431+
logger.debug("Failed to find port for Docker image {}, defaulting to 5000".format(image)) # noqa
432+
port = 5000
430433
for pos, item in enumerate(service['spec']['ports']):
431434
if item['port'] == 80 and port != item['targetPort']:
432435
# port 80 is the only one we care about right now
@@ -670,8 +673,7 @@ def _get_port(self, image):
670673
image_info = docker_cli.inspect_image(image)
671674
port = int(list(image_info['Config']['ExposedPorts'].keys())[0].split("/")[0])
672675
except Exception:
673-
logger.debug("Failed to find port for Docker image {}, defaulting to 5000".format(image)) # noqa
674-
port = 5000
676+
port = None
675677

676678
return port
677679

@@ -938,8 +940,8 @@ def _create_rc(self, namespace, name, image, command, **kwargs): # noqa
938940
# add in healthchecks
939941
if kwargs.get('healthcheck'):
940942
template = self._healthcheck(template, kwargs['routable'], **kwargs['healthcheck'])
941-
elif kwargs.get('build_type') == "buildpack":
942-
template = self._buildpack_readiness_probe(template)
943+
else:
944+
template = self._default_readiness_probe(template, image, kwargs.get('build_type'))
943945

944946
url = self._api("/namespaces/{}/replicationcontrollers", namespace)
945947
resp = self.session.post(url, json=template)
@@ -1047,6 +1049,24 @@ def _healthcheck(self, controller, routable=False, path='/', port=5000, delay=30
10471049

10481050
return controller
10491051

1052+
def _default_readiness_probe(self, controller, image, build_type):
1053+
if build_type == "buildpack":
1054+
readinessprobe = self._default_buildpack_readiness_probe()
1055+
else:
1056+
readinessprobe = self._default_dockerapp_readiness_probe(image)
1057+
if readinessprobe is None:
1058+
return controller
1059+
# Update only the application container with the health check
1060+
app_type = controller['spec']['selector']['type']
1061+
namespace = controller['spec']['selector']['app']
1062+
container_name = '{}-{}'.format(namespace, app_type)
1063+
containers = controller['spec']['template']['spec']['containers']
1064+
for container in containers:
1065+
if container['name'] == container_name:
1066+
container.update(readinessprobe)
1067+
1068+
return controller
1069+
10501070
'''
10511071
Applies exec readiness probe to the slugrunner container.
10521072
http://kubernetes.io/docs/user-guide/pod-states/#container-probes
@@ -1061,8 +1081,8 @@ def _healthcheck(self, controller, routable=False, path='/', port=5000, delay=30
10611081
This should be added only for the build pack apps when a custom liveness probe is not set to
10621082
make sure that the pod is ready only when the slug is downloaded and started running.
10631083
'''
1064-
def _buildpack_readiness_probe(self, controller, delay=30, timeout=5, period_seconds=5,
1065-
success_threshold=1, failure_threshold=1):
1084+
def _default_buildpack_readiness_probe(self, delay=30, timeout=5, period_seconds=5,
1085+
success_threshold=1, failure_threshold=1):
10661086
readinessprobe = {
10671087
'readinessProbe': {
10681088
# an exec probe
@@ -1082,17 +1102,33 @@ def _buildpack_readiness_probe(self, controller, delay=30, timeout=5, period_sec
10821102
'failureThreshold': failure_threshold,
10831103
},
10841104
}
1105+
return readinessprobe
10851106

1086-
# Update only the application container with the health check
1087-
app_type = controller['spec']['selector']['type']
1088-
namespace = controller['spec']['selector']['app']
1089-
container_name = '{}-{}'.format(namespace, app_type)
1090-
containers = controller['spec']['template']['spec']['containers']
1091-
for container in containers:
1092-
if container['name'] == container_name:
1093-
container.update(readinessprobe)
1094-
1095-
return controller
1107+
'''
1108+
Applies tcp socket readiness probe to the docker app container only if some port is exposed
1109+
by the docker image.
1110+
'''
1111+
def _default_dockerapp_readiness_probe(self, image, delay=5, timeout=5, period_seconds=5,
1112+
success_threshold=1, failure_threshold=1):
1113+
port = self._get_port(image)
1114+
if port is None:
1115+
return None
1116+
readinessprobe = {
1117+
'readinessProbe': {
1118+
# an exec probe
1119+
'tcpSocket': {
1120+
"port": port
1121+
},
1122+
# length of time to wait for a pod to initialize
1123+
# after pod startup, before applying health checking
1124+
'initialDelaySeconds': delay,
1125+
'timeoutSeconds': timeout,
1126+
'periodSeconds': period_seconds,
1127+
'successThreshold': success_threshold,
1128+
'failureThreshold': failure_threshold,
1129+
},
1130+
}
1131+
return readinessprobe
10961132

10971133
# SECRETS #
10981134
# http://kubernetes.io/v1.1/docs/api-reference/v1/definitions.html#_v1_secret

0 commit comments

Comments
 (0)