Merge "Add new Kubernetes resource kind: "CustomResourceDefinition""
[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         super(KubernetesContext, self).__init__(host_name_separator='-')
37
38     def init(self, attrs):
39         super(KubernetesContext, self).init(attrs)
40
41         self.template = kubernetes.KubernetesTemplate(self.name, attrs)
42         self.ssh_key = '{}-key'.format(self.name)
43         self.key_path = self._get_key_path()
44         self.public_key_path = '{}.pub'.format(self.key_path)
45
46     def deploy(self):
47         LOG.info('Creating ssh key')
48         self._set_ssh_key()
49
50         self._create_crd()
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         self._delete_crd()
65
66         super(KubernetesContext, self).undeploy()
67
68     def _wait_until_running(self):
69         while not all(self._check_pod_status(p) for p in self.template.pods):
70             time.sleep(1)
71
72     def _check_pod_status(self, pod):
73         status = k8s_utils.read_pod_status(pod)
74         LOG.debug('%s:%s', pod, status)
75         if status == 'Failed':
76             LOG.error('Pod %s status is failed', pod)
77             raise RuntimeError
78         if status != 'Running':
79             return False
80         return True
81
82     def _create_services(self):
83         for obj in self.template.service_objs:
84             obj.create()
85
86     def _delete_services(self):
87         for obj in self.template.service_objs:
88             obj.delete()
89
90     def _create_rcs(self):
91         for obj in self.template.k8s_objs:
92             self._create_rc(obj.get_template())
93
94     def _create_rc(self, template):
95         k8s_utils.create_replication_controller(template)
96
97     def _delete_rcs(self):
98         for rc in self.template.rcs:
99             self._delete_rc(rc)
100
101     def _delete_rc(self, rc):
102         k8s_utils.delete_replication_controller(rc)
103
104     def _delete_pods(self):
105         for pod in self.template.pods:
106             self._delete_pod(pod)
107
108     def _delete_pod(self, pod):
109         k8s_utils.delete_pod(pod)
110
111     def _create_crd(self):
112         LOG.info('Create Custom Resource Definition elements')
113         for crd in self.template.crd:
114             crd.create()
115
116     def _delete_crd(self):
117         LOG.info('Delete Custom Resource Definition elements')
118         for crd in self.template.crd:
119             crd.delete()
120
121     def _get_key_path(self):
122         task_id = self.name.split('-')[-1]
123         k = 'files/yardstick_key-{}'.format(task_id)
124         return pkg_resources.resource_filename('yardstick.resources', k)
125
126     def _set_ssh_key(self):
127         rsa_key = paramiko.RSAKey.generate(bits=BITS_LENGTH)
128
129         LOG.info('Writing private key')
130         rsa_key.write_private_key_file(self.key_path)
131
132         LOG.info('Writing public key')
133         key = '{} {}\n'.format(rsa_key.get_name(), rsa_key.get_base64())
134         with open(self.public_key_path, 'w') as f:
135             f.write(key)
136
137         LOG.info('Create configmap for ssh key')
138         k8s_utils.create_config_map(self.ssh_key, {'authorized_keys': key})
139
140     def _delete_ssh_key(self):
141         k8s_utils.delete_config_map(self.ssh_key)
142         utils.remove_file(self.key_path)
143         utils.remove_file(self.public_key_path)
144
145     def _get_server(self, name):
146         service_name = '{}-service'.format(name)
147         service = k8s_utils.get_service_by_name(service_name).ports[0]
148
149         host = {
150             'name': service.name,
151             'ip': self._get_node_ip(),
152             'private_ip': k8s_utils.get_pod_by_name(name).status.pod_ip,
153             'ssh_port': service.node_port,
154             'user': 'root',
155             'key_filename': self.key_path,
156         }
157
158         return host
159
160     def _get_node_ip(self):
161         return k8s_utils.get_node_list().items[0].status.addresses[0].address
162
163     def _get_network(self, attr_name):
164         return None
165
166     def _get_physical_nodes(self):
167         return None
168
169     def _get_physical_node_for_server(self, server_name):
170         return None