Skip to content

Commit 0702285

Browse files
committed
Merge pull request #591 from helgi/health
feat(config): setting one healthcheck value now results in all defaults for health to be saved
2 parents b0ec07e + bf9746c commit 0702285

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

rootfs/api/models/config.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,33 @@ def __str__(self):
2727
return "{}-{}".format(self.app.id, str(self.uuid)[:7])
2828

2929
def healthcheck(self):
30-
# Update healthcheck - Scheduler determines the app type
30+
"""
31+
Get all healthchecks options together for use in scheduler
32+
"""
33+
# return empty dict if no healthcheck is found
3134
if 'HEALTHCHECK_URL' not in self.values.keys():
3235
return {}
36+
3337
path = self.values.get('HEALTHCHECK_URL', '/')
3438
timeout = int(self.values.get('HEALTHCHECK_TIMEOUT', 50))
3539
delay = int(self.values.get('HEALTHCHECK_INITIAL_DELAY', 50))
36-
port = int(self.values.get('HEALTHCHECK_PORT', 8080))
40+
port = int(self.values.get('HEALTHCHECK_PORT', 5000))
3741

3842
return {'path': path, 'timeout': timeout, 'delay': delay, 'port': port}
3943

44+
def set_healthchecks(self):
45+
"""Defines default values for HTTP healthchecks"""
46+
if not {k: v for k, v in self.values.items() if k.startswith('HEALTHCHECK_')}:
47+
return
48+
49+
# fetch set health values and any defaults
50+
# this approach allows new health items to be added without issues
51+
health = self.healthcheck()
52+
self.values['HEALTHCHECK_URL'] = health['path']
53+
self.values['HEALTHCHECK_TIMEOUT'] = health['timeout']
54+
self.values['HEALTHCHECK_INITIAL_DELAY'] = health['delay']
55+
self.values['HEALTHCHECK_PORT'] = health['port']
56+
4057
def save(self, **kwargs):
4158
"""merge the old config with the new"""
4259
try:
@@ -61,6 +78,9 @@ def save(self, **kwargs):
6178
except Config.DoesNotExist:
6279
pass
6380

81+
# set any missing HEALTHCHECK_* elements
82+
self.set_healthchecks()
83+
6484
# verify the tags exist on any nodes as labels
6585
if self.tags:
6686
# Get all nodes with label selectors

rootfs/scheduler/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def _create_rc(self, namespace, name, image, command, **kwargs): # noqa
942942

943943
# add in healtchecks
944944
if kwargs.get('healthcheck'):
945-
template = self._healthcheck(template, **kwargs['healthcheck'])
945+
template = self._healthcheck(template, kwargs['routable'], **kwargs['healthcheck'])
946946

947947
url = self._api("/namespaces/{}/replicationcontrollers", namespace)
948948
resp = self.session.post(url, json=template)
@@ -986,10 +986,8 @@ def _delete_rc(self, namespace, name):
986986

987987
return response
988988

989-
def _healthcheck(self, controller, path='/', port=8080, delay=30, timeout=1):
990-
# FIXME this logic ideally should live higher up
991-
app_type = controller['spec']['selector']['type']
992-
if app_type not in ['web', 'cmd']:
989+
def _healthcheck(self, controller, routable=False, path='/', port=5000, delay=30, timeout=5): # noqa
990+
if not routable:
993991
return controller
994992

995993
namespace = controller['spec']['selector']['app']
@@ -1029,6 +1027,7 @@ def _healthcheck(self, controller, path='/', port=8080, delay=30, timeout=1):
10291027
}
10301028

10311029
# Update only the application container with the health check
1030+
app_type = controller['spec']['selector']['type']
10321031
container_name = '{}-{}'.format(namespace, app_type)
10331032
containers = controller['spec']['template']['spec']['containers']
10341033
for container in containers:

0 commit comments

Comments
 (0)