aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/common/kubernetes_utils.py
blob: e4c23283016280f597649fd9726c92914ca79c2e (plain)
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
##############################################################################
# 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

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:
        LOG.exception('config file not found')
        raise

    return client.CoreV1Api()


def create_replication_controller(template,
                                  namespace='default',
                                  wait=False,
                                  **kwargs):    # pragma: no cover

    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

    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,
               **kwargs):    # pragma: no cover

    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:
        LOG.exception('Delete pod failed')
        raise


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
    return read_pod(name).status.phase


def create_config_map(name,
                      data,
                      namespace='default',
                      wait=False,
                      **kwargs):   # pragma: no cover
    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
    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 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