6b00c03745fb7cf5510921af1eef5c41da5c3e0e
[yardstick.git] / yardstick / benchmark / contexts / kubernetes.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
3 #
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 ##############################################################################
9
10 from __future__ import absolute_import
11 import logging
12 import time
13 import pkg_resources
14
15 import paramiko
16
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
21
22 LOG = logging.getLogger(__name__)
23 BITS_LENGTH = 2048
24
25
26 class KubernetesContext(Context):
27     """Class that handle nodes info"""
28
29     __context_type__ = "Kubernetes"
30
31     def __init__(self):
32         self.ssh_key = ''
33         self.key_path = ''
34         self.public_key_path = ''
35         self.template = None
36
37         super(KubernetesContext, self).__init__()
38
39     def init(self, attrs):
40         super(KubernetesContext, self).init(attrs)
41
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)
46
47     def deploy(self):
48         LOG.info('Creating ssh key')
49         self._set_ssh_key()
50
51         LOG.info('Launch containers')
52         self._create_rcs()
53         self._create_services()
54         time.sleep(1)
55         self.template.get_rc_pods()
56
57         self._wait_until_running()
58
59     def undeploy(self):
60         self._delete_ssh_key()
61         self._delete_rcs()
62         self._delete_pods()
63         self._delete_services()
64
65         super(KubernetesContext, self).undeploy()
66
67     def _wait_until_running(self):
68         while not all(self._check_pod_status(p) for p in self.template.pods):
69             time.sleep(1)
70
71     def _check_pod_status(self, pod):
72         status = k8s_utils.read_pod_status(pod)
73         LOG.debug('%s:%s', pod, status)
74         if status == 'Failed':
75             LOG.error('Pod %s status is failed', pod)
76             raise RuntimeError
77         if status != 'Running':
78             return False
79         return True
80
81     def _create_services(self):
82         for obj in self.template.service_objs:
83             obj.create()
84
85     def _delete_services(self):
86         for obj in self.template.service_objs:
87             obj.delete()
88
89     def _create_rcs(self):
90         for obj in self.template.k8s_objs:
91             self._create_rc(obj.get_template())
92
93     def _create_rc(self, template):
94         k8s_utils.create_replication_controller(template)
95
96     def _delete_rcs(self):
97         for rc in self.template.rcs:
98             self._delete_rc(rc)
99
100     def _delete_rc(self, rc):
101         k8s_utils.delete_replication_controller(rc)
102
103     def _delete_pods(self):
104         for pod in self.template.pods:
105             self._delete_pod(pod)
106
107     def _delete_pod(self, pod):
108         k8s_utils.delete_pod(pod)
109
110     def _get_key_path(self):
111         task_id = self.name.split('-')[-1]
112         k = 'files/yardstick_key-{}'.format(task_id)
113         return pkg_resources.resource_filename('yardstick.resources', k)
114
115     def _set_ssh_key(self):
116         rsa_key = paramiko.RSAKey.generate(bits=BITS_LENGTH)
117
118         LOG.info('Writing private key')
119         rsa_key.write_private_key_file(self.key_path)
120
121         LOG.info('Writing public key')
122         key = '{} {}\n'.format(rsa_key.get_name(), rsa_key.get_base64())
123         with open(self.public_key_path, 'w') as f:
124             f.write(key)
125
126         LOG.info('Create configmap for ssh key')
127         k8s_utils.create_config_map(self.ssh_key, {'authorized_keys': key})
128
129     def _delete_ssh_key(self):
130         k8s_utils.delete_config_map(self.ssh_key)
131         utils.remove_file(self.key_path)
132         utils.remove_file(self.public_key_path)
133
134     def _get_server(self, name):
135         service_name = '{}-service'.format(name)
136         service = k8s_utils.get_service_by_name(service_name).ports[0]
137
138         host = {
139             'name': service.name,
140             'ip': self._get_node_ip(),
141             'private_ip': k8s_utils.get_pod_by_name(name).status.pod_ip,
142             'ssh_port': service.node_port,
143             'user': 'root',
144             'key_filename': self.key_path,
145         }
146
147         return host
148
149     def _get_node_ip(self):
150         return k8s_utils.get_node_list().items[0].status.addresses[0].address
151
152     def _get_network(self, attr_name):
153         return None