1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import logging
from kubernetes import client
from kubernetes import config
from kubernetes.client.rest import ApiException
from yardstick.common import constants as consts
from yardstick.common import exceptions
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
def get_core_api(): # pragma: no cover
try:
config.load_kube_config(config_file=consts.K8S_CONF_FILE)
except IOError:
raise exceptions.KubernetesConfigFileNotFound()
return client.CoreV1Api()
def get_extensions_v1beta_api():
try:
config.load_kube_config(config_file=consts.K8S_CONF_FILE)
except IOError:
raise exceptions.KubernetesConfigFileNotFound()
return client.ApiextensionsV1beta1Api()
def get_custom_objects_api():
try:
config.load_kube_config(config_file=consts.K8S_CONF_FILE)
except IOError:
raise exceptions.KubernetesConfigFileNotFound()
return client.CustomObjectsApi()
def get_node_list(**kwargs): # pragma: no cover
core_v1_api = get_core_api()
try:
return core_v1_api.list_node(**kwargs)
except ApiException:
LOG.exception('Get node list failed')
raise
def create_service(template,
namespace='default',
wait=False,
**kwargs): # pragma: no cover
# pylint: disable=unused-argument
core_v1_api = get_core_api()
metadata = client.V1ObjectMeta(**template.get('metadata', {}))
ports = [client.V1ServicePort(**port) for port in
template.get('spec', {}).get('ports', [])]
template['spec']['ports'] = ports
spec = client.V1ServiceSpec(**template.get('spec', {}))
service = client.V1Service(metadata=metadata, spec=spec)
try:
core_v1_api.create_namespaced_service('default', service)
except ApiException:
LOG.exception('Create Service failed')
raise
def delete_service(name, namespace='default', skip_codes=None, **kwargs):
skip_codes = [] if not skip_codes else skip_codes
core_v1_api = get_core_api()
try:
body = client.V1DeleteOptions()
core_v1_api.delete_namespaced_service(name, namespace, body, **kwargs)
except ApiException as e:
if e.status in skip_codes:
LOG.info(e.reason)
else:
raise exceptions.KubernetesApiException(
action='delete', resource='Service')
def get_service_list(namespace='default', **kwargs):
core_v1_api = get_core_api()
try:
return core_v1_api.list_namespaced_service(namespace, **kwargs)
except ApiException:
LOG.exception('Get Service list failed')
raise
def get_service_by_name(name): # pragma: no cover
service_list = get_service_list()
return next((s.spec for s in service_list.items if s.metadata.name == name), None)
def create_replication_controller(template,
namespace='default',
wait=False,
**kwargs): # pragma: no cover
# pylint: disable=unused-argument
core_v1_api = get_core_api()
try:
core_v1_api.create_namespaced_replication_controller(namespace,
template,
**kwargs)
except ApiException:
LOG.exception('Create replication controller failed')
raise
def delete_replication_controller(name,
namespace='default',
wait=False,
**kwargs): # pragma: no cover
# pylint: disable=unused-argument
core_v1_api = get_core_api()
body = kwargs.get('body', client.V1DeleteOptions())
kwargs.pop('body', None)
try:
core_v1_api.delete_namespaced_replication_controller(name,
namespace,
body,
**kwargs)
except ApiException:
LOG.exception('Delete replication controller failed')
raise
def delete_pod(name,
namespace='default',
wait=False,
skip_codes=None,
**kwargs): # pragma: no cover
# pylint: disable=unused-argument
skip_codes = [] if not skip_codes else skip_codes
core_v1_api = get_core_api()
body = kwargs.get('body', client.V1DeleteOptions())
kwargs.pop('body', None)
try:
core_v1_api.delete_namespaced_pod(name,
namespace,
body,
**kwargs)
except ApiException as e:
if e.status in skip_codes:
LOG.info(e.reason)
else:
raise exceptions.KubernetesApiException(
action='delete', resource='Pod')
def read_pod(name,
namespace='default',
**kwargs): # pragma: no cover
core_v1_api = get_core_api()
try:
resp = core_v1_api.read_namespaced_pod(name, namespace, **kwargs)
except ApiException:
LOG.exception('Read pod failed')
raise
else:
return resp
def read_pod_status(name, namespace='default', **kwargs): # pragma: no cover
# pylint: disable=unused-argument
return read_pod(name).status.phase
def create_config_map(name,
data,
namespace='default',
wait=False,
**kwargs): # pragma: no cover
# pylint: disable=unused-argument
core_v1_api = get_core_api()
metadata = client.V1ObjectMeta(name=name)
body = client.V1ConfigMap(data=data, metadata=metadata)
try:
core_v1_api.create_namespaced_config_map(namespace, body, **kwargs)
except ApiException:
LOG.exception('Create config map failed')
raise
def delete_config_map(name,
namespace='default',
wait=False,
**kwargs): # pragma: no cover
# pylint: disable=unused-argument
core_v1_api = get_core_api()
body = kwargs.get('body', client.V1DeleteOptions())
kwargs.pop('body', None)
try:
core_v1_api.delete_namespaced_config_map(name,
namespace,
body,
**kwargs)
except ApiException:
LOG.exception('Delete config map failed')
raise
def create_custom_resource_definition(body):
api = get_extensions_v1beta_api()
body_obj = client.V1beta1CustomResourceDefinition(
spec=body['spec'], metadata=body['metadata'])
try:
api.create_custom_resource_definition(body_obj)
except ValueError:
# NOTE(ralonsoh): bug in kubernetes-client/python 6.0.0
# https://github.com/kubernetes-client/python/issues/491
pass
except ApiException:
raise exceptions.KubernetesApiException(
action='create', resource='CustomResourceDefinition')
def delete_custom_resource_definition(name, skip_codes=None):
skip_codes = [] if not skip_codes else skip_codes
api = get_extensions_v1beta_api()
body_obj = client.V1DeleteOptions()
try:
api.delete_custom_resource_definition(name, body_obj)
except ApiException as e:
if e.status in skip_codes:
LOG.info(e.reason)
else:
raise exceptions.KubernetesApiException(
action='delete', resource='CustomResourceDefinition')
def get_custom_resource_definition(kind):
api = get_extensions_v1beta_api()
try:
crd_list = api.list_custom_resource_definition()
for crd_obj in (crd_obj for crd_obj in crd_list.items
if crd_obj.spec.names.kind == kind):
return crd_obj
return None
except ApiException:
raise exceptions.KubernetesApiException(
action='delete', resource='CustomResourceDefinition')
def create_network(scope, group, version, plural, body, namespace='default'):
api = get_custom_objects_api()
try:
if scope == consts.SCOPE_CLUSTER:
api.create_cluster_custom_object(group, version, plural, body)
else:
api.create_namespaced_custom_object(
group, version, namespace, plural, body)
except ApiException:
raise exceptions.KubernetesApiException(
action='create', resource='Custom Object: Network')
def delete_network(scope, group, version, plural, name, namespace='default', skip_codes=None):
skip_codes = [] if not skip_codes else skip_codes
api = get_custom_objects_api()
try:
if scope == consts.SCOPE_CLUSTER:
api.delete_cluster_custom_object(group, version, plural, name, {})
else:
api.delete_namespaced_custom_object(
group, version, namespace, plural, name, {})
except ApiException as e:
if e.status in skip_codes:
LOG.info(e.reason)
else:
raise exceptions.KubernetesApiException(
action='delete', resource='Custom Object: Network')
def get_pod_list(namespace='default'): # pragma: no cover
core_v1_api = get_core_api()
try:
return core_v1_api.list_namespaced_pod(namespace=namespace)
except ApiException:
LOG.exception('Get pod list failed')
raise
def get_pod_by_name(name): # pragma: no cover
pod_list = get_pod_list()
return next((n for n in pod_list.items if n.metadata.name.startswith(name)), None)
def get_volume_types():
"""Return the "volume" types supported by the current API"""
return [vtype for vtype in client.V1Volume.attribute_map.values()
if vtype != 'name']
|