|
| 1 | +from api import utils |
| 2 | +from scheduler.resources import Resource |
| 3 | +from scheduler.exceptions import KubeHTTPException |
| 4 | + |
| 5 | + |
| 6 | +class NetworkPolicy(Resource): |
| 7 | + api_prefix = 'apis' |
| 8 | + api_version = 'networking.k8s.io/v1' |
| 9 | + |
| 10 | + def manifest(self, namespace, name, **kwargs): |
| 11 | + data = { |
| 12 | + "apiVersion": self.api_version, |
| 13 | + "kind": "NetworkPolicy", |
| 14 | + "metadata": { |
| 15 | + "name": name, |
| 16 | + "namespace": namespace, |
| 17 | + "labels": { |
| 18 | + "heritage": "drycc" |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + data = utils.dict_merge(data, kwargs) |
| 23 | + if "version" in kwargs: |
| 24 | + data["metadata"]["resourceVersion"] = kwargs.get("version") |
| 25 | + return data |
| 26 | + |
| 27 | + def get(self, namespace, name=None, ignore_exception=False, **kwargs): |
| 28 | + """ |
| 29 | + Fetch a single NetworkPolicy or a list |
| 30 | + """ |
| 31 | + if name is not None: |
| 32 | + url = self.api("/namespaces/{}/networkpolicies/{}", namespace, name) |
| 33 | + message = 'get NetworkPolicy "{}" in Namespace "{}"'.format(name, namespace) |
| 34 | + else: |
| 35 | + url = self.api("/namespaces/{}/networkpolicies", namespace) |
| 36 | + message = 'get NetworkPolicies in Namespace "{}"'.format(namespace) |
| 37 | + |
| 38 | + response = self.http_get(url, params=self.query_params(**kwargs)) |
| 39 | + if not ignore_exception and self.unhealthy(response.status_code): |
| 40 | + raise KubeHTTPException(response, message) |
| 41 | + |
| 42 | + return response |
| 43 | + |
| 44 | + def create(self, namespace, name, ignore_exception=False, **kwargs): |
| 45 | + url = self.api("/namespaces/{}/networkpolicies", namespace) |
| 46 | + data = self.manifest(namespace, name, **kwargs) |
| 47 | + response = self.http_post(url, json=data) |
| 48 | + |
| 49 | + if not ignore_exception and self.unhealthy(response.status_code): |
| 50 | + raise KubeHTTPException( |
| 51 | + response, 'create NetworkPolicy "{}" in Namespace "{}"', name, namespace) |
| 52 | + |
| 53 | + return response |
| 54 | + |
| 55 | + def patch(self, namespace, name, ignore_exception=False, **kwargs): |
| 56 | + url = self.api("/namespaces/{}/networkpolicies/{}", namespace, name) |
| 57 | + data = self.manifest(namespace, name, **kwargs) |
| 58 | + response = self.http_patch( |
| 59 | + url, |
| 60 | + json=data, |
| 61 | + headers={"Content-Type": "application/merge-patch+json"} |
| 62 | + ) |
| 63 | + if not ignore_exception and self.unhealthy(response.status_code): |
| 64 | + raise KubeHTTPException( |
| 65 | + response, 'patch NetworkPolicy "{}" in Namespace "{}"', name, namespace) |
| 66 | + return response |
| 67 | + |
| 68 | + def delete(self, namespace, name, ignore_exception=False): |
| 69 | + url = self.api("/namespaces/{}/networkpolicies/{}", namespace, name) |
| 70 | + response = self.http_delete(url) |
| 71 | + if not ignore_exception and self.unhealthy(response.status_code): |
| 72 | + raise KubeHTTPException( |
| 73 | + response, 'delete NetworkPolicy "{}" in Namespace "{}"', name, namespace) |
| 74 | + return response |
0 commit comments