-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgateway.py
More file actions
182 lines (147 loc) · 6.49 KB
/
gateway.py
File metadata and controls
182 lines (147 loc) · 6.49 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from scheduler.resources import Resource
from scheduler.exceptions import KubeHTTPException
class Gateway(Resource):
api_prefix = 'apis'
api_version = 'gateway.networking.k8s.io/v1'
def manifest(self, namespace, name, **kwargs):
data = {
"apiVersion": self.api_version,
"kind": "Gateway",
"metadata": {
"name": name,
"namespace": namespace,
"annotations": kwargs.get("annotations", {}),
},
"spec": {
"gatewayClassName": kwargs.get("gateway_class", "default"),
"listeners": kwargs.get("listeners")
}
}
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 Gateway or a list of Gateways
"""
if name is not None:
url = self.api("/namespaces/{}/gateways/{}", namespace, name)
message = 'get Gateway ' + name
else:
url = self.api("/namespaces/{}/gateways", namespace)
message = 'get Gateways'
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/{}/gateways", 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 gateway {}".format(namespace))
return response
def patch(self, namespace, name, ignore_exception=False, **kwargs):
url = self.api("/namespaces/{}/gateways/{}", 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, "put gateway {}".format(namespace))
return response
def delete(self, namespace, name, ignore_exception=True):
url = self.api("/namespaces/{}/gateways/{}", namespace, name)
response = self.http_delete(url)
if not ignore_exception and self.unhealthy(response.status_code):
raise KubeHTTPException(response, 'delete gateway "{}"', namespace)
return response
class Route(Resource):
api_prefix = 'apis'
api_version = 'gateway.networking.k8s.io/v1'
def manifest(self, namespace, name, **kwargs):
data = {
"apiVersion": self.api_version,
"kind": self.kind,
"metadata": {
"name": name,
"namespace": namespace
},
"spec": {
"parentRefs": kwargs["parent_refs"],
"rules": kwargs["rules"]
}
}
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 Route or a list of Routes
"""
if name is not None:
url = self.api("/namespaces/{}/{}s/{}", namespace, self.kind.lower(), name)
message = 'get %s %s' % (self.kind, name)
else:
url = self.api("/namespaces/{}/{}s", namespace, self.kind.lower())
message = 'get %s' % self.kind
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/{}/{}s", namespace, self.kind.lower())
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 {} {}".format(self.kind.lower(), namespace))
return response
def patch(self, namespace, name, ignore_exception=False, **kwargs):
url = self.api("/namespaces/{}/{}s/{}", namespace, self.kind.lower(), 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, "put {} {}".format(self.kind.lower(), namespace))
return response
def delete(self, namespace, name, ignore_exception=True):
url = self.api("/namespaces/{}/{}s/{}", namespace, self.kind.lower(), name)
response = self.http_delete(url)
if not ignore_exception and self.unhealthy(response.status_code):
raise KubeHTTPException(response, 'delete {} "{}"', self.kind.lower(), namespace)
return response
class TCPRoute(Route):
kind = "TCPRoute"
api_version = 'gateway.networking.k8s.io/v1alpha2'
class UDPRoute(Route):
kind = "UDPRoute"
api_version = 'gateway.networking.k8s.io/v1alpha2'
class HTTPRoute(Route):
kind = "HTTPRoute"
api_version = 'gateway.networking.k8s.io/v1'
def manifest(self, namespace, name, **kwargs):
data = super().manifest(namespace, name, **kwargs)
if "hostnames" in kwargs:
data["spec"]["hostnames"] = kwargs["hostnames"]
return data
class GRPCRoute(Route):
kind = "GRPCRoute"
api_version = 'gateway.networking.k8s.io/v1alpha2'
def manifest(self, namespace, name, **kwargs):
data = super().manifest(namespace, name, **kwargs)
if "hostnames" in kwargs:
data["spec"]["hostnames"] = kwargs["hostnames"]
return data
class TLSRoute(Route):
kind = "GRPCRoute"
api_version = 'gateway.networking.k8s.io/v1alpha2'
def manifest(self, namespace, name, **kwargs):
data = super().manifest(namespace, name, **kwargs)
if "hostnames" in kwargs:
data["spec"]["hostnames"] = kwargs["hostnames"]
return data