diff options
-rw-r--r-- | yardstick/orchestrator/kubernetes.py | 18 | ||||
-rw-r--r-- | yardstick/tests/unit/orchestrator/test_kubernetes.py | 29 |
2 files changed, 45 insertions, 2 deletions
diff --git a/yardstick/orchestrator/kubernetes.py b/yardstick/orchestrator/kubernetes.py index 6fd3fa640..3c7559c7d 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -89,6 +89,7 @@ class ReplicationControllerObject(object): SSHKEY_DEFAULT = 'yardstick_key' RESTART_POLICY = ('Always', 'OnFailure', 'Never') + TOLERATIONS_KEYS = ('key', 'value', 'effect', 'operator') def __init__(self, name, **kwargs): super(ReplicationControllerObject, self).__init__() @@ -99,6 +100,7 @@ class ReplicationControllerObject(object): self._volumes = parameters.pop('volumes', []) self._security_context = parameters.pop('securityContext', None) self._networks = parameters.pop('networks', []) + self._tolerations = parameters.pop('tolerations', []) self._restart_policy = parameters.pop('restartPolicy', 'Always') if self._restart_policy not in self.RESTART_POLICY: raise exceptions.KubernetesWrongRestartPolicy( @@ -129,7 +131,8 @@ class ReplicationControllerObject(object): "containers": [], "volumes": [], "nodeSelector": {}, - "restartPolicy": self._restart_policy + "restartPolicy": self._restart_policy, + "tolerations": [] } } } @@ -141,6 +144,7 @@ class ReplicationControllerObject(object): self._add_volumes() self._add_security_context() self._add_networks() + self._add_tolerations() def get_template(self): return self.template @@ -211,6 +215,18 @@ class ReplicationControllerObject(object): 'spec.template.metadata.annotations', annotations) + def _add_tolerations(self): + tolerations = [] + for tol in self._tolerations: + tolerations.append({k: tol[k] for k in tol + if k in self.TOLERATIONS_KEYS}) + + tolerations = ([{'operator': 'Exists'}] if not tolerations + else tolerations) + utils.set_dict_value(self.template, + 'spec.template.spec.tolerations', + tolerations) + class ServiceNodePortObject(object): diff --git a/yardstick/tests/unit/orchestrator/test_kubernetes.py b/yardstick/tests/unit/orchestrator/test_kubernetes.py index afadf748a..cc1b37569 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -67,7 +67,10 @@ service ssh restart;while true ; do sleep 10000; done" "nodeSelector": { "kubernetes.io/hostname": "node-01" }, - "restartPolicy": "Always" + "restartPolicy": "Always", + "tolerations": [ + {"operator": "Exists"} + ] } } } @@ -231,6 +234,30 @@ class ReplicationControllerObjectTestCase(base.BaseUnitTestCase): '{"name": "network3"}]') self.assertEqual(expected, networks) + def test__add_tolerations(self): + _kwargs = {'tolerations': [{'key': 'key1', + 'value': 'value2', + 'effect': 'effect3', + 'operator': 'operator4', + 'wrong_key': 'error_key'}] + } + k8s_obj = kubernetes.ReplicationControllerObject('pod_name', **_kwargs) + k8s_obj._add_tolerations() + _tol = k8s_obj.template['spec']['template']['spec']['tolerations'] + self.assertEqual(1, len(_tol)) + self.assertEqual({'key': 'key1', + 'value': 'value2', + 'effect': 'effect3', + 'operator': 'operator4'}, + _tol[0]) + + def test__add_tolerations_default(self): + k8s_obj = kubernetes.ReplicationControllerObject('pod_name') + k8s_obj._add_tolerations() + _tol = k8s_obj.template['spec']['template']['spec']['tolerations'] + self.assertEqual(1, len(_tol)) + self.assertEqual({'operator': 'Exists'}, _tol[0]) + class ContainerObjectTestCase(base.BaseUnitTestCase): |