1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from __future__ import absolute_import
17 from yardstick.benchmark.contexts.base import Context
18 from yardstick.orchestrator import kubernetes
19 from yardstick.common import kubernetes_utils as k8s_utils
20 from yardstick.common import utils
22 LOG = logging.getLogger(__name__)
26 class KubernetesContext(Context):
27 """Class that handle nodes info"""
29 __context_type__ = "Kubernetes"
34 self.public_key_path = ''
37 super(KubernetesContext, self).__init__()
39 def init(self, attrs):
40 super(KubernetesContext, self).init(attrs)
42 self.template = kubernetes.KubernetesTemplate(self.name, attrs)
43 self.ssh_key = '{}-key'.format(self.name)
44 self.key_path = self._get_key_path()
45 self.public_key_path = '{}.pub'.format(self.key_path)
48 LOG.info('Creating ssh key')
52 self._create_networks()
53 LOG.info('Launch containers')
55 self._create_services()
57 self.template.get_rc_pods()
59 self._wait_until_running()
62 self._delete_ssh_key()
65 self._delete_services()
66 self._delete_networks()
69 super(KubernetesContext, self).undeploy()
71 def _wait_until_running(self):
72 while not all(self._check_pod_status(p) for p in self.template.pods):
75 def _check_pod_status(self, pod):
76 status = k8s_utils.read_pod_status(pod)
77 LOG.debug('%s:%s', pod, status)
78 if status == 'Failed':
79 LOG.error('Pod %s status is failed', pod)
81 if status != 'Running':
85 def _create_services(self):
86 for obj in self.template.service_objs:
89 def _delete_services(self):
90 for obj in self.template.service_objs:
93 def _create_rcs(self):
94 for obj in self.template.k8s_objs:
95 self._create_rc(obj.get_template())
97 def _create_rc(self, template):
98 k8s_utils.create_replication_controller(template)
100 def _delete_rcs(self):
101 for rc in self.template.rcs:
104 def _delete_rc(self, rc):
105 k8s_utils.delete_replication_controller(rc)
107 def _delete_pods(self):
108 for pod in self.template.pods:
109 self._delete_pod(pod)
111 def _delete_pod(self, pod):
112 k8s_utils.delete_pod(pod)
114 def _create_crd(self):
115 LOG.info('Create Custom Resource Definition elements')
116 for crd in self.template.crd:
119 def _delete_crd(self):
120 LOG.info('Delete Custom Resource Definition elements')
121 for crd in self.template.crd:
124 def _create_networks(self): # pragma: no cover
125 LOG.info('Create Network elements')
126 for net in self.template.network_objs:
129 def _delete_networks(self): # pragma: no cover
130 LOG.info('Create Network elements')
131 for net in self.template.network_objs:
134 def _get_key_path(self):
135 task_id = self.name.split('-')[-1]
136 k = 'files/yardstick_key-{}'.format(task_id)
137 return pkg_resources.resource_filename('yardstick.resources', k)
139 def _set_ssh_key(self):
140 rsa_key = paramiko.RSAKey.generate(bits=BITS_LENGTH)
142 LOG.info('Writing private key')
143 rsa_key.write_private_key_file(self.key_path)
145 LOG.info('Writing public key')
146 key = '{} {}\n'.format(rsa_key.get_name(), rsa_key.get_base64())
147 with open(self.public_key_path, 'w') as f:
150 LOG.info('Create configmap for ssh key')
151 k8s_utils.create_config_map(self.ssh_key, {'authorized_keys': key})
153 def _delete_ssh_key(self):
154 k8s_utils.delete_config_map(self.ssh_key)
155 utils.remove_file(self.key_path)
156 utils.remove_file(self.public_key_path)
158 def _get_server(self, name):
159 service_name = '{}-service'.format(name)
160 service = k8s_utils.get_service_by_name(service_name).ports[0]
163 'name': service.name,
164 'ip': self._get_node_ip(),
165 'private_ip': k8s_utils.get_pod_by_name(name).status.pod_ip,
166 'ssh_port': service.node_port,
168 'key_filename': self.key_path,
173 def _get_node_ip(self):
174 return k8s_utils.get_node_list().items[0].status.addresses[0].address
176 def _get_network(self, attr_name):