Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 1 | # Copyright 2017 Mirantis, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 14 | |
| 15 | from kubernetes import client |
| 16 | |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 17 | from devops.helpers import helpers |
| 18 | |
| 19 | from tcp_tests.managers.k8s.base import K8sBaseResource |
| 20 | from tcp_tests.managers.k8s.base import K8sBaseManager |
| 21 | |
| 22 | |
| 23 | class K8sPod(K8sBaseResource): |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 24 | resource_type = 'pod' |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 25 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 26 | def _read(self, **kwargs): |
| 27 | return self._manager.api.read_namespaced_pod( |
| 28 | self.name, self.namespace, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 29 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 30 | def _create(self, body, **kwargs): |
| 31 | return self._manager.api.create_namespaced_pod( |
| 32 | self.namespace, body, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 33 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 34 | def _patch(self, body, **kwargs): |
| 35 | return self._manager.api.patch_namespaced_pod( |
| 36 | self.name, self.namespace, body, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 37 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 38 | def _replace(self, body, **kwargs): |
| 39 | return self._manager.api.replace_namespaced_pod( |
| 40 | self.name, self.namespace, body, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 41 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 42 | def _delete(self, **kwargs): |
| 43 | self._manager.api.delete_namespaced_pod( |
| 44 | self.name, self.namespace, client.V1DeleteOptions(), **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 45 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 46 | def wait_phase(self, phases, timeout=60, interval=3): |
| 47 | if isinstance(phases, str): |
| 48 | phases = [phases] |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 49 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 50 | helpers.wait(lambda: self.read().status.phase in phases, |
| 51 | timeout=timeout, interval=interval, |
| 52 | timeout_msg='Timeout waiting, pod {0} phase is not in ' |
| 53 | '"{1}"'.format(self.name, phases)) |
| 54 | return self |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 55 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 56 | def wait_running(self, timeout=240, interval=3): |
| 57 | return self.wait_phase('Running', timeout=timeout, interval=interval) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 58 | |
| 59 | |
| 60 | class K8sPodManager(K8sBaseManager): |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 61 | resource_class = K8sPod |
| 62 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 63 | @property |
| 64 | def api(self): |
| 65 | return self._cluster.api_core |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 66 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 67 | def _list(self, namespace, **kwargs): |
| 68 | return self.api.list_namespaced_pod(namespace, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 69 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame^] | 70 | def _list_all(self, **kwargs): |
| 71 | return self.api.list_pod_for_all_namespaces(**kwargs) |