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