|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import tempfile |
16 |
| - |
17 |
| -from kubernetes import client as k8s_config |
18 |
| -from kubernetes.client import api_client |
19 |
| -from kubernetes.client.apis import core_v1_api |
20 |
| -from kubernetes.client import configuration as k8s_configuration |
21 |
| -from kubernetes.client import rest |
22 |
| -from oslo_log import log as logging |
| 15 | +import requests |
23 | 16 |
|
24 | 17 | from magnum.conductor.handlers.common.cert_manager import create_client_files
|
25 | 18 |
|
26 |
| -LOG = logging.getLogger(__name__) |
27 |
| - |
28 | 19 |
|
29 |
| -class ApiClient(api_client.ApiClient): |
30 |
| - |
31 |
| - def __init__(self, configuration=None, header_name=None, |
32 |
| - header_value=None, cookie=None): |
33 |
| - if configuration is None: |
34 |
| - configuration = k8s_configuration.Configuration() |
35 |
| - self.configuration = configuration |
| 20 | +class KubernetesAPI: |
| 21 | + """ |
| 22 | + Simple Kubernetes API client using requests. |
36 | 23 |
|
37 |
| - self.rest_client = rest.RESTClientObject(configuration) |
38 |
| - self.default_headers = {} |
39 |
| - if header_name is not None: |
40 |
| - self.default_headers[header_name] = header_value |
41 |
| - self.cookie = cookie |
| 24 | + This API wrapper allows for a set of very simple operations to be |
| 25 | + performed on a Kubernetes cluster using the `requests` library. The |
| 26 | + reason behind it is that the native `kubernetes` library does not |
| 27 | + seem to be quite thread-safe at the moment. |
42 | 28 |
|
43 |
| - def __del__(self): |
44 |
| - pass |
45 |
| - |
46 |
| - def call_api(self, resource_path, method, |
47 |
| - path_params=None, query_params=None, header_params=None, |
48 |
| - body=None, post_params=None, files=None, |
49 |
| - response_type=None, auth_settings=None, |
50 |
| - _return_http_data_only=None, collection_formats=None, |
51 |
| - _preload_content=True, _request_timeout=None, **kwargs): |
52 |
| - """Makes http request (synchronous) and return the deserialized data |
53 |
| -
|
54 |
| - :param resource_path: Path to method endpoint. |
55 |
| - :param method: Method to call. |
56 |
| - :param path_params: Path parameters in the url. |
57 |
| - :param query_params: Query parameters in the url. |
58 |
| - :param header_params: Header parameters to be |
59 |
| - placed in the request header. |
60 |
| - :param body: Request body. |
61 |
| - :param post_params dict: Request post form parameters, |
62 |
| - for `application/x-www-form-urlencoded`, `multipart/form-data`. |
63 |
| - :param auth_settings list: Auth Settings names for the request. |
64 |
| - :param response: Response data type. |
65 |
| - :param files dict: key -> filename, value -> filepath, |
66 |
| - for `multipart/form-data`. |
67 |
| - :param _return_http_data_only: response data without head status code |
68 |
| - and headers |
69 |
| - :param collection_formats: dict of collection formats for path, query, |
70 |
| - header, and post parameters. |
71 |
| - :param _preload_content: if False, the urllib3.HTTPResponse object will |
72 |
| - be returned without reading/decoding response |
73 |
| - data. Default is True. |
74 |
| - :param _request_timeout: timeout setting for this request. If one |
75 |
| - number provided, it will be total request |
76 |
| - timeout. It can also be a pair (tuple) of |
77 |
| - (connection, read) timeouts. |
78 |
| -
|
79 |
| - :return: The method will return the response directly |
| 29 | + Also, our interactions with the Kubernetes API are happening inside |
| 30 | + Greenthreads so we don't need to use connection pooling on top of it, |
| 31 | + in addition to pools not being something that you can disable with |
| 32 | + the native Kubernetes API. |
| 33 | + """ |
80 | 34 |
|
| 35 | + def __init__(self, context, cluster): |
| 36 | + self.context = context |
| 37 | + self.cluster = cluster |
| 38 | + |
| 39 | + # Load certificates for cluster |
| 40 | + (self.ca_file, self.key_file, self.cert_file) = create_client_files( |
| 41 | + self.cluster, self.context |
| 42 | + ) |
| 43 | + |
| 44 | + def _request(self, method, url, json=True): |
| 45 | + response = requests.request( |
| 46 | + method, |
| 47 | + url, |
| 48 | + verify=self.ca_file.name, |
| 49 | + cert=(self.cert_file.name, self.key_file.name) |
| 50 | + ) |
| 51 | + response.raise_for_status() |
| 52 | + if json: |
| 53 | + return response.json() |
| 54 | + else: |
| 55 | + return response.text |
| 56 | + |
| 57 | + def get_healthz(self): |
81 | 58 | """
|
82 |
| - return self.__call_api(resource_path, method, |
83 |
| - path_params, query_params, header_params, |
84 |
| - body, post_params, files, |
85 |
| - response_type, auth_settings, |
86 |
| - _return_http_data_only, collection_formats, |
87 |
| - _preload_content, _request_timeout) |
88 |
| - |
89 |
| - |
90 |
| -class K8sAPI(core_v1_api.CoreV1Api): |
91 |
| - |
92 |
| - def _create_temp_file_with_content(self, content): |
93 |
| - """Creates temp file and write content to the file. |
94 |
| -
|
95 |
| - :param content: file content |
96 |
| - :returns: temp file |
| 59 | + Get the health of the cluster from API |
97 | 60 | """
|
98 |
| - try: |
99 |
| - tmp = tempfile.NamedTemporaryFile(delete=True) |
100 |
| - tmp.write(content) |
101 |
| - tmp.flush() |
102 |
| - except Exception as err: |
103 |
| - LOG.error("Error while creating temp file: %s", err) |
104 |
| - raise |
105 |
| - return tmp |
106 |
| - |
107 |
| - def __init__(self, context, cluster): |
108 |
| - self.ca_file = None |
109 |
| - self.cert_file = None |
110 |
| - self.key_file = None |
| 61 | + return self._request( |
| 62 | + 'GET', |
| 63 | + f"{self.cluster.api_address}/healthz", |
| 64 | + json=False |
| 65 | + ) |
111 | 66 |
|
112 |
| - if cluster.magnum_cert_ref: |
113 |
| - (self.ca_file, self.key_file, |
114 |
| - self.cert_file) = create_client_files(cluster, context) |
| 67 | + def list_node(self): |
| 68 | + """ |
| 69 | + List all nodes in the cluster. |
115 | 70 |
|
116 |
| - config = k8s_config.Configuration() |
117 |
| - config.host = cluster.api_address |
118 |
| - config.ssl_ca_cert = self.ca_file.name |
119 |
| - config.cert_file = self.cert_file.name |
120 |
| - config.key_file = self.key_file.name |
| 71 | + :return: List of nodes. |
| 72 | + """ |
| 73 | + return self._request( |
| 74 | + 'GET', |
| 75 | + f"{self.cluster.api_address}/api/v1/nodes" |
| 76 | + ) |
121 | 77 |
|
122 |
| - # build a connection with Kubernetes master |
123 |
| - client = ApiClient(configuration=config) |
| 78 | + def list_namespaced_pod(self, namespace): |
| 79 | + """ |
| 80 | + List all pods in the given namespace. |
124 | 81 |
|
125 |
| - super(K8sAPI, self).__init__(client) |
| 82 | + :param namespace: Namespace to list pods from. |
| 83 | + :return: List of pods. |
| 84 | + """ |
| 85 | + return self._request( |
| 86 | + 'GET', |
| 87 | + f"{self.cluster.api_address}/api/v1/namespaces/{namespace}/pods" |
| 88 | + ) |
126 | 89 |
|
127 | 90 | def __del__(self):
|
128 |
| - if self.ca_file: |
| 91 | + """ |
| 92 | + Close all of the file descriptions for the certificates, since they |
| 93 | + are left open by `create_client_files`. |
| 94 | +
|
| 95 | + TODO(mnaser): Use a context manager and avoid having these here. |
| 96 | + """ |
| 97 | + if hasattr(self, 'ca_file'): |
129 | 98 | self.ca_file.close()
|
130 |
| - if self.cert_file: |
| 99 | + if hasattr(self, 'cert_file'): |
131 | 100 | self.cert_file.close()
|
132 |
| - if self.key_file: |
| 101 | + if hasattr(self, 'key_file'): |
133 | 102 | self.key_file.close()
|
134 |
| - |
135 |
| - |
136 |
| -def create_k8s_api(context, cluster): |
137 |
| - """Create a kubernetes API client |
138 |
| -
|
139 |
| - Creates connection with Kubernetes master and creates ApivApi instance |
140 |
| - to call Kubernetes APIs. |
141 |
| -
|
142 |
| - :param context: The security context |
143 |
| - :param cluster: Cluster object |
144 |
| - """ |
145 |
| - return K8sAPI(context, cluster) |
0 commit comments