Specify the networks to be used per pod 35/57435/4
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Tue, 15 May 2018 15:03:30 +0000 (16:03 +0100)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Thu, 14 Jun 2018 07:15:47 +0000 (07:15 +0000)
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 <rodolfo.alonso.hernandez@intel.com>
yardstick/orchestrator/kubernetes.py
yardstick/tests/unit/orchestrator/test_kubernetes.py

index 44a333e..231a03b 100644 (file)
@@ -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):
 
index e45545d..fe9e2fd 100644 (file)
@@ -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):