From 9c19d2dc2ca9057d5d27b3e1b77e40fee2fcc6d4 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 15 May 2018 16:03:30 +0100 Subject: [PATCH] Specify the networks to be used per pod If CRD "Network" is defined and network items are created, each pod (server) can have access to one or several networks. This is defined in the metadata section, as "annotations.networks" [1]. Example of Kubernetes pod definition with networks: apiVersion: v1 kind: Pod metadata: name: test-pod annotations: networks: '[{"name": "flannel"}]' Example of Yardstick server definition with networks: context: type: Kubernetes servers: host: containers: - name: ... networks: - flannel # These names must be defined in # context.networks ... networks: - name: flannel plugin: flannel Kubernetes annotations [2]. [1]https://github.com/intel/multus-cni/tree/b9446232cdf4f1b6f2bea583291973cc97e963f4#configuring-multus-to-use-kubeconfig-and-a-default-network [2]https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/ JIRA: YARDSTICK-1178 Change-Id: I6e7b4bacf10810833ec733c14d44e5db613675e3 Signed-off-by: Rodolfo Alonso Hernandez --- yardstick/orchestrator/kubernetes.py | 19 ++++++++++++++++++- yardstick/tests/unit/orchestrator/test_kubernetes.py | 10 ++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/yardstick/orchestrator/kubernetes.py b/yardstick/orchestrator/kubernetes.py index 44a333e4b..231a03b13 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -9,10 +9,12 @@ import copy +from oslo_serialization import jsonutils + from yardstick.common import constants from yardstick.common import exceptions -from yardstick.common import utils from yardstick.common import kubernetes_utils as k8s_utils +from yardstick.common import utils class ContainerObject(object): @@ -71,6 +73,7 @@ class KubernetesObject(object): self.ssh_key = parameters.pop('ssh_key', self.SSHKEY_DEFAULT) self._volumes = parameters.pop('volumes', []) self._security_context = parameters.pop('securityContext', None) + self._networks = parameters.pop('networks', []) containers = parameters.pop('containers', None) if containers: @@ -107,6 +110,7 @@ class KubernetesObject(object): self._add_node_selector() self._add_volumes() self._add_security_context() + self._add_networks() def get_template(self): return self.template @@ -164,6 +168,19 @@ class KubernetesObject(object): 'spec.template.spec.securityContext', self._security_context) + def _add_networks(self): + networks = [] + for net in self._networks: + networks.append({'name': net}) + + if not networks: + return + + annotations = {'networks': jsonutils.dumps(networks)} + utils.set_dict_value(self.template, + 'spec.template.metadata.annotations', + annotations) + class ServiceObject(object): diff --git a/yardstick/tests/unit/orchestrator/test_kubernetes.py b/yardstick/tests/unit/orchestrator/test_kubernetes.py index e45545d6a..fe9e2fd2b 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -208,6 +208,16 @@ class KubernetesObjectTestCase(base.BaseUnitTestCase): self.assertEqual({'key%s' % i: 'value%s' % i}, container['securityContext']) + def test__add_networks(self): + k8s_obj = kubernetes.KubernetesObject( + 'name', networks=['network1', 'network2', 'network3']) + k8s_obj._add_networks() + networks = k8s_obj.\ + template['spec']['template']['metadata']['annotations']['networks'] + expected = ('[{"name": "network1"}, {"name": "network2"}, ' + '{"name": "network3"}]') + self.assertEqual(expected, networks) + class ContainerObjectTestCase(base.BaseUnitTestCase): -- 2.16.6