|
| 1 | +from scheduler.exceptions import KubeHTTPException |
| 2 | +from scheduler.resources import Resource |
| 3 | + |
| 4 | + |
| 5 | +class Ingress(Resource): |
| 6 | + short_name = 'ingress' |
| 7 | + |
| 8 | + def get(self, name=None, **kwargs): |
| 9 | + """ |
| 10 | + Fetch a single Ingress or a list of Ingresses |
| 11 | + """ |
| 12 | + if name is not None: |
| 13 | + url = "/apis/extensions/v1beta1/namespaces/%s/ingresses/%s" % (name, name) |
| 14 | + message = 'get Ingress ' + name |
| 15 | + else: |
| 16 | + url = "/apis/extensions/v1beta1/namespaces/%s/ingresses" % name |
| 17 | + message = 'get Ingresses' |
| 18 | + |
| 19 | + response = self.http_get(url, params=self.query_params(**kwargs)) |
| 20 | + if self.unhealthy(response.status_code): |
| 21 | + raise KubeHTTPException(response, message) |
| 22 | + |
| 23 | + return response |
| 24 | + |
| 25 | + def create(self, ingress, namespace, hostname): |
| 26 | + url = "/apis/extensions/v1beta1/namespaces/%s/ingresses" % namespace |
| 27 | + |
| 28 | + if hostname == "": |
| 29 | + raise KubeHTTPException("empty hostname value") |
| 30 | + |
| 31 | + data = { |
| 32 | + "kind": "Ingress", |
| 33 | + "apiVersion": "extensions/v1beta1", |
| 34 | + "metadata": { |
| 35 | + "name": ingress |
| 36 | + }, |
| 37 | + "spec": { |
| 38 | + "rules": [ |
| 39 | + {"host": ingress + "." + hostname, |
| 40 | + "http": { |
| 41 | + "paths": [ |
| 42 | + {"path": "/", |
| 43 | + "backend": { |
| 44 | + "serviceName": ingress, |
| 45 | + "servicePort": 80 |
| 46 | + }} |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + } |
| 53 | + response = self.http_post(url, json=data) |
| 54 | + |
| 55 | + if not response.status_code == 201: |
| 56 | + raise KubeHTTPException(response, "create Ingress {}".format(namespace)) |
| 57 | + |
| 58 | + return response |
| 59 | + |
| 60 | + def delete(self, namespace, ingress): |
| 61 | + url = self.api("/namespaces/{}/ingresses/{}", namespace, ingress) |
| 62 | + response = self.http_delete(url) |
| 63 | + if self.unhealthy(response.status_code): |
| 64 | + raise KubeHTTPException(response, 'delete Ingress "{}"', namespace) |
| 65 | + |
| 66 | + return response |
0 commit comments