Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
fix(validation): success threshold can only be equal to one when sett…
Browse files Browse the repository at this point in the history
…ing liveness probe. Error properly

Kubernetes has this restriction, catch it before it ever makes it that far.

Introducing jmespath now to later use in other places as well

Fixes #873
  • Loading branch information
helgi committed Jul 28, 2016
1 parent b3c2024 commit a4d58d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions rootfs/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import json
import jmespath
import re
import jsonschema
import idna
Expand Down Expand Up @@ -337,6 +338,15 @@ def validate_healthcheck(self, data):
raise serializers.ValidationError(
"could not validate {}: {}".format(value, e.message))

# http://kubernetes.io/docs/api-reference/v1/definitions/#_v1_probe
# liveness only supports successThreshold=1, no other value
# This is not in the schema since readiness supports other values
threshold = jmespath.search('livenessProbe.successThreshold', data)
if threshold is not None and threshold != 1:
raise serializers.ValidationError(
'livenessProbe successThreshold can only be 1'
)

return data


Expand Down
14 changes: 13 additions & 1 deletion rootfs/api/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,9 @@ def test_config_healthchecks(self, mock_requests):
self.assertIn('readinessProbe', resp.data['healthcheck'])
self.assertEqual(resp.data['healthcheck'], readiness_probe['healthcheck'])

liveness_probe = {'healthcheck': {'livenessProbe': {'httpGet': {'port': 5000}}}}
liveness_probe = {'healthcheck': {'livenessProbe':
{'httpGet': {'port': 5000},
'successThreshold': 1}}}
resp = self.client.post(
'/v2/apps/{app_id}/config'.format(**locals()),
liveness_probe)
Expand Down Expand Up @@ -879,6 +881,16 @@ def test_config_healthchecks_validations(self, mock_requests):
)
self.assertEqual(resp.status_code, 400, response.data)

# set liveness success threshold to a non-1 value
# Don't set one of the mandatory value
resp = self.client.post(
'/v2/apps/{app_id}/config'.format(**locals()),
{'healthcheck': {'livenessProbe':
{'httpGet': {'path': '/', 'port': 5000},
'successThreshold': 5}}}
)
self.assertEqual(resp.status_code, 400, response.data)

def test_config_healthchecks_legacy(self, mock_requests):
"""
Test that when a user uses `deis config:set HEALTHCHECK_URL=/`, the config
Expand Down
1 change: 1 addition & 0 deletions rootfs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ django-guardian==1.4.4
djangorestframework==3.4.0
docker-py==1.9.0
gunicorn==19.6.0
jmespath==0.9.0
jsonfield==1.0.3
morph==0.1.2
ndg-httpsclient==0.4.2
Expand Down

0 comments on commit a4d58d8

Please sign in to comment.