summaryrefslogtreecommitdiffstats
path: root/k8s/k8sclient/k8sclient.py
diff options
context:
space:
mode:
authorJack Lucas <jflucas@research.att.com>2018-07-31 21:10:25 +0000
committerJack Lucas <jflucas@research.att.com>2018-08-01 21:14:45 +0000
commitb13a6e53197bb01a857442680faa78938f1b614e (patch)
tree328e7964fd2e6a597ba6e127fc51eff16b6e09d6 /k8s/k8sclient/k8sclient.py
parenta2aefd40115798e4093bc2d9e606ad5a34d3e017 (diff)
Fix k8splugin to accept intervals as strings
Also update type file to point to new plugin version Add unit tests for parsing interval Change RE to be non-Python specific Use uppercase for RE constant and unit conversion factors Issue-ID: DCAEGEN2-649 Change-Id: I1de728b3efd0725d4a3da996d95ec61e68f56ab4 Signed-off-by: Jack Lucas <jflucas@research.att.com>
Diffstat (limited to 'k8s/k8sclient/k8sclient.py')
-rw-r--r--k8s/k8sclient/k8sclient.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/k8s/k8sclient/k8sclient.py b/k8s/k8sclient/k8sclient.py
index e388fb5..1c30534 100644
--- a/k8s/k8sclient/k8sclient.py
+++ b/k8s/k8sclient/k8sclient.py
@@ -18,6 +18,7 @@
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
import os
+import re
import uuid
from msb import msb
from kubernetes import config, client, stream
@@ -26,6 +27,11 @@ from kubernetes import config, client, stream
PROBE_DEFAULT_PERIOD = 15
PROBE_DEFAULT_TIMEOUT = 1
+# Regular expression for interval/timeout specification
+INTERVAL_SPEC = re.compile("^([0-9]+)(s|m|h)?$")
+# Conversion factors to seconds
+FACTORS = {None: 1, "s": 1, "m": 60, "h": 3600}
+
def _create_deployment_name(component_name):
return "dep-{0}".format(component_name)
@@ -58,12 +64,32 @@ def _configure_api():
environ=localenv
).load_and_set()
+def _parse_interval(t):
+ """
+ Parse an interval specification
+ t can be
+ - a simple integer quantity, interpreted as seconds
+ - a string representation of a decimal integer, interpreted as seconds
+ - a string consisting of a represention of an decimal integer followed by a unit,
+ with "s" representing seconds, "m" representing minutes,
+ and "h" representing hours
+ Used for compatibility with the Docker plugin, where time intervals
+ for health checks were specified as strings with a number and a unit.
+ See 'intervalspec' above for the regular expression that's accepted.
+ """
+ m = INTERVAL_SPEC.match(str(t))
+ if m:
+ time = int(m.group(1)) * FACTORS[m.group(2)]
+ else:
+ raise ValueError("Bad interval specification: {0}".format(t))
+ return time
+
def _create_probe(hc, port):
''' Create a Kubernetes probe based on info in the health check dictionary hc '''
probe_type = hc['type']
probe = None
- period = hc.get('interval', PROBE_DEFAULT_PERIOD)
- timeout = hc.get('timeout', PROBE_DEFAULT_TIMEOUT)
+ period = _parse_interval(hc.get('interval', PROBE_DEFAULT_PERIOD))
+ timeout = _parse_interval(hc.get('timeout', PROBE_DEFAULT_TIMEOUT))
if probe_type in ['http', 'https']:
probe = client.V1Probe(
failure_threshold = 1,