Improve "get_server" function in Kubernetes context 71/59671/3
authorJohn O Loughlin <john.oloughlin@intel.com>
Wed, 11 Jul 2018 10:22:39 +0000 (11:22 +0100)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Thu, 12 Jul 2018 15:57:13 +0000 (16:57 +0100)
When a kubernetes context is returning the node info it is
assumed that the first port defined is the ssh port which may
not be the case. This patch will address this issue by looking
for a defined port 22.

JIRA: YARDSTICK-1301

Change-Id: I65dee1bcf62f21ebcaefeaa2666bb0ad53f3876c
Signed-off-by: John O Loughlin <john.oloughlin@intel.com>
yardstick/benchmark/contexts/kubernetes.py
yardstick/common/constants.py
yardstick/common/exceptions.py
yardstick/tests/unit/benchmark/contexts/test_kubernetes.py

index 4ba9eee..9feb006 100644 (file)
@@ -16,6 +16,8 @@ import paramiko
 
 from yardstick.benchmark.contexts.base import Context
 from yardstick.orchestrator import kubernetes
+from yardstick.common import constants
+from yardstick.common import exceptions
 from yardstick.common import kubernetes_utils as k8s_utils
 from yardstick.common import utils
 
@@ -156,19 +158,26 @@ class KubernetesContext(Context):
 
     def _get_server(self, name):
         service_name = '{}-service'.format(name)
-        service = k8s_utils.get_service_by_name(service_name).ports[0]
-
-        host = {
-            'name': service.name,
+        service = k8s_utils.get_service_by_name(service_name)
+        if not service:
+            raise exceptions.KubernetesServiceObjectNotDefined()
+
+        for sn_port in (sn_port for sn_port in service.ports
+                        if sn_port.port == constants.SSH_PORT):
+            node_port = sn_port.node_port
+            break
+        else:
+            raise exceptions.KubernetesSSHPortNotDefined()
+
+        return {
+            'name': name,
             'ip': self._get_node_ip(),
             'private_ip': k8s_utils.get_pod_by_name(name).status.pod_ip,
-            'ssh_port': service.node_port,
+            'ssh_port': node_port,
             'user': 'root',
-            'key_filename': self.key_path,
+            'key_filename': self.key_path
         }
 
-        return host
-
     def _get_node_ip(self):
         return k8s_utils.get_node_list().items[0].status.addresses[0].address
 
index 2f14d4b..4ed40f8 100644 (file)
@@ -6,7 +6,6 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
-from __future__ import absolute_import
 
 import errno
 import os
@@ -14,11 +13,9 @@ from functools import reduce
 
 import pkg_resources
 
-# this module must only import other modules that do
-# not require loggers to be created, so this cannot
-# include yardstick.common.utils
 from yardstick.common.yaml_loader import yaml_load
 
+
 dirname = os.path.dirname
 abspath = os.path.abspath
 join = os.path.join
@@ -175,3 +172,6 @@ OS_CLOUD_DEFAULT_CONFIG = {'verify': False}
 # Kubernetes
 SCOPE_NAMESPACED = 'Namespaced'
 SCOPE_CLUSTER = 'Cluster'
+
+# VNF definition
+SSH_PORT = 22
index 641c4e1..c25acba 100644 (file)
@@ -223,6 +223,14 @@ class KubernetesTemplateInvalidVolumeType(YardstickException):
     message = 'No valid "volume" types present in %(volume)s'
 
 
+class KubernetesSSHPortNotDefined(YardstickException):
+    message = 'Port 22 needs to be defined'
+
+
+class KubernetesServiceObjectNotDefined(YardstickException):
+    message = 'ServiceObject is not defined'
+
+
 class KubernetesCRDObjectDefinitionError(YardstickException):
     message = ('Kubernetes Custom Resource Definition Object error, missing '
                'parameters: %(missing_parameters)s')
index 3957aab..512468f 100644 (file)
@@ -12,6 +12,7 @@ import unittest
 
 from yardstick.benchmark.contexts import base
 from yardstick.benchmark.contexts import kubernetes
+from yardstick.common import constants
 from yardstick.orchestrator import kubernetes as orchestrator_kubernetes
 
 
@@ -117,8 +118,8 @@ class KubernetesTestCase(unittest.TestCase):
                         mock_get_pod_by_name):
         class Service(object):
             def __init__(self):
-                self.name = 'yardstick'
                 self.node_port = 30000
+                self.port = constants.SSH_PORT
 
         class Services(object):
             def __init__(self):
@@ -135,8 +136,9 @@ class KubernetesTestCase(unittest.TestCase):
         mock_get_service_by_name.return_value = Services()
         mock_get_pod_by_name.return_value = Pod()
         mock_get_node_ip.return_value = '172.16.10.131'
-
-        self.assertIsNotNone(self.k8s_context._get_server('server'))
+        server = self.k8s_context._get_server('server_name')
+        self.assertEqual('server_name', server['name'])
+        self.assertEqual(30000, server['ssh_port'])
 
     @mock.patch.object(kubernetes.KubernetesContext, '_create_rc')
     def test_create_rcs(self, mock_create_rc):