Add "env" parameter in Kubernetes context 47/57347/5
authorJohn O Loughlin <john.oloughlin@intel.com>
Fri, 11 May 2018 17:09:54 +0000 (18:09 +0100)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Fri, 6 Jul 2018 09:02:21 +0000 (09:02 +0000)
This new parameter, "env", will allow the user to
automatically to add environment variables in a pod definition

Example of definition in a context:
  context:
    type: Kubernetes
      servers:
        host:
            image: ...
            commands: ...
            env:
                - <variable name>: <variable value>
                - <variable name>: <variable value>

The volume type and the definition must be one of the supported
ones in Kubernetes [1].

[1] https://kubernetes.io/docs/concepts/storage/volumes/#types-of-volumes

JIRA: YARDSTICK-1161

Change-Id: Icf372a98c090063f10c9217755519ab92ba9b0ba
Signed-off-by: John O Loughlin <john.oloughlin@intel.com>
yardstick/orchestrator/kubernetes.py
yardstick/tests/unit/orchestrator/test_kubernetes.py

index a595de8..f690ab3 100644 (file)
@@ -31,6 +31,7 @@ class ContainerObject(object):
         self._args = kwargs.get('args', [])
         self._volume_mounts = kwargs.get('volumeMounts', [])
         self._security_context = kwargs.get('securityContext')
+        self._env = kwargs.get('env', [])
 
     def _create_volume_mounts(self):
         """Return all "volumeMounts" items per container"""
@@ -58,6 +59,11 @@ class ContainerObject(object):
                      'volumeMounts': self._create_volume_mounts()}
         if self._security_context:
             container['securityContext'] = self._security_context
+        if self._env:
+            container['env'] = []
+            for env in self._env:
+                container['env'].append({'name': env['name'],
+                                         'value': env['value']})
         return container
 
 
index 70d17e2..5eba1a0 100644 (file)
@@ -285,6 +285,23 @@ class ContainerObjectTestCase(base.BaseUnitTestCase):
                     'securityContext': {'key': 'value'}}
         self.assertEqual(expected, container_obj.get_container_item())
 
+    def test_get_container_item_with_env(self):
+        volume_mount = {'name': 'fake_name',
+                        'mountPath': 'fake_path'}
+        args = ['arg1', 'arg2']
+        container_obj = kubernetes.ContainerObject(
+            'cname', ssh_key='fake_sshkey', volumeMount=[volume_mount],
+            args=args, env=[{'name': 'fake_var_name',
+                             'value': 'fake_var_value'}])
+        expected = {'args': args,
+                    'command': [kubernetes.ContainerObject.COMMAND_DEFAULT],
+                    'image': kubernetes.ContainerObject.IMAGE_DEFAULT,
+                    'name': 'cname-container',
+                    'volumeMounts': container_obj._create_volume_mounts(),
+                    'env': [{'name': 'fake_var_name',
+                             'value': 'fake_var_value'}]}
+        self.assertEqual(expected, container_obj.get_container_item())
+
 
 class CustomResourceDefinitionObjectTestCase(base.BaseUnitTestCase):