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 | |
| 14 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 15 | from kubernetes import client |
| 16 | |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 17 | from tcp_tests.managers.k8s.base import K8sBaseResource |
| 18 | from tcp_tests.managers.k8s.base import K8sBaseManager |
| 19 | |
| 20 | |
| 21 | class K8sService(K8sBaseResource): |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 22 | resource_type = 'service' |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 23 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 24 | def _read(self, **kwargs): |
| 25 | return self._manager.api.read_namespaced_service( |
| 26 | self.name, self.namespace, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 27 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 28 | def _create(self, body, **kwargs): |
| 29 | return self._manager.api.create_namespaced_service( |
| 30 | self.namespace, body, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 31 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 32 | def _patch(self, body, **kwargs): |
| 33 | return self._manager.api.patch_namespaced_service( |
| 34 | self.name, self.namespace, body, **kwargs) |
| 35 | |
| 36 | def _replace(self, body, **kwargs): |
| 37 | return self._manager.api.replace_namespaced_service( |
| 38 | self.name, self.namespace, body, **kwargs) |
| 39 | |
| 40 | def _delete(self, **kwargs): |
| 41 | self._manager.api.delete_namespaced_service( |
| 42 | self.name, self.namespace, client.V1DeleteOptions(), **kwargs) |
| 43 | |
| 44 | def get_ip(self, external=False): |
| 45 | if external: |
| 46 | return self.read().status.load_balancer.ingress[0].ip |
| 47 | else: |
| 48 | return self.read().spec.cluster_ip |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 49 | |
| 50 | |
| 51 | class K8sServiceManager(K8sBaseManager): |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 52 | resource_class = K8sService |
| 53 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 54 | @property |
| 55 | def api(self): |
| 56 | return self._cluster.api_core |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 57 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 58 | def _list(self, namespace, **kwargs): |
| 59 | return self.api.list_namespaced_service(namespace, **kwargs) |
Artem Panchenko | 0594cd7 | 2017-06-12 13:25:26 +0300 | [diff] [blame] | 60 | |
Vladimir Jigulin | 4ad52a8 | 2018-08-12 05:51:30 +0400 | [diff] [blame] | 61 | def _list_all(self, **kwargs): |
| 62 | return self.api.list_service_for_all_namespaces(**kwargs) |