-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathingress.py
More file actions
158 lines (126 loc) · 5.53 KB
/
ingress.py
File metadata and controls
158 lines (126 loc) · 5.53 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
from scheduler.exceptions import KubeHTTPException
from scheduler.resources import Resource
MANIFEAT_CLASSES = {}
class BaseManifest(object):
def manifest(self, api_version, ingress, ingress_class, namespace, **kwargs):
path = "/*" if ingress_class in ("gce", "alb") else "/"
hosts, tls = kwargs.pop("hosts", None), kwargs.pop("tls", None)
version = kwargs.pop("version", None)
data = {
"kind": "Ingress",
"apiVersion": api_version,
"metadata": {
"name": ingress,
"annotations": {
"kubernetes.io/tls-acme": "true",
}
},
"spec": {}
}
if hosts:
data["spec"]["rules"] = [{
"host": host,
"http": {
"paths": [
{
"path": path,
"backend": {
"serviceName": ingress,
"servicePort": 80
}
}
]
}
} for host in hosts]
if ingress_class:
data["metadata"]["annotations"].update({
"kubernetes.io/ingress.class": ingress_class
})
if tls:
data["spec"]["tls"] = tls
if version:
data["metadata"]["resourceVersion"] = version
return data
class NginxManifest(BaseManifest):
def manifest(self, api_version, ingress, ingress_class, namespace, **kwargs):
data = BaseManifest.manifest(
self, api_version, ingress, ingress_class, namespace, **kwargs)
if "allowlist" in kwargs:
allowlist = ", ".join(kwargs.pop("allowlist"))
data["metadata"]["annotations"].update({
"nginx.ingress.kubernetes.io/whitelist-source-range": allowlist
})
if "ssl_redirect" in kwargs:
ssl_redirect = kwargs.pop("ssl_redirect")
data["metadata"]["annotations"].update({
"nginx.ingress.kubernetes.io/ssl-redirect": ssl_redirect
})
return data
MANIFEAT_CLASSES["nginx"] = NginxManifest
class TraefikManifest(BaseManifest):
def manifest(self, api_version, ingress, ingress_class, namespace, **kwargs):
data = BaseManifest.manifest(
self, api_version, ingress, ingress_class, namespace, **kwargs)
if "allowlist" in kwargs:
allowlist = ", ".join(kwargs.pop("allowlist"))
data["metadata"]["annotations"].update({
"ingress.kubernetes.io/whitelist-x-forwarded-for": "true",
"traefik.ingress.kubernetes.io/whitelist-source-range": allowlist
})
if "ssl_redirect" in kwargs:
ssl_redirect = kwargs.pop("ssl_redirect")
data["metadata"]["annotations"].update({
"ingress.kubernetes.io/ssl-redirect": ssl_redirect
})
return data
MANIFEAT_CLASSES["traefik"] = TraefikManifest
class Ingress(Resource):
api_version = 'networking.k8s.io/v1beta1'
api_prefix = 'apis'
short_name = 'ingress'
@staticmethod
def manifest(api_version, ingress, ingress_class, namespace, **kwargs):
return MANIFEAT_CLASSES.get(ingress_class, BaseManifest)().manifest(
api_version, ingress, ingress_class, namespace, **kwargs
)
def get(self, namespace, ingress=None, **kwargs):
"""
Fetch a single Ingress or a list of Ingresses
"""
if ingress is not None:
url = self.api("/namespaces/{}/ingresses/{}", namespace, ingress)
message = 'get Ingress ' + ingress
else:
url = self.api("/namespaces/{}/ingresses", namespace)
message = 'get Ingresses'
response = self.http_get(url, params=self.query_params(**kwargs))
if self.unhealthy(response.status_code):
raise KubeHTTPException(response, message)
return response
def create(self, ingress, ingress_class, namespace, **kwargs):
url = self.api("/namespaces/{}/ingresses", namespace)
data = self.manifest(self.api_version, ingress, ingress_class, namespace, **kwargs)
response = self.http_post(url, json=data)
if not response.status_code == 201:
raise KubeHTTPException(response, "create Ingress {}".format(namespace))
return response
def put(self, ingress, ingress_class, namespace, version, **kwargs):
url = self.api("/namespaces/{}/ingresses/{}", namespace, ingress)
kwargs["version"] = version
data = self.manifest(self.api_version, ingress, ingress_class, namespace, **kwargs)
response = self.http_put(url, json=data)
if self.unhealthy(response.status_code):
raise KubeHTTPException(response, "put Ingress {}".format(namespace))
return response
def patch(self, ingress, namespace, data):
url = self.api("/namespaces/{}/ingresses/{}", namespace, ingress)
response = self.http_put(url, json=data)
if self.unhealthy(response.status_code):
raise KubeHTTPException(response, "patch Ingress {}".format(namespace))
return response
def delete(self, namespace, ingress):
url = self.api("/namespaces/{}/ingresses/{}", namespace, ingress)
response = self.http_delete(url)
if self.unhealthy(response.status_code):
raise KubeHTTPException(response, 'delete Ingress "{}"', namespace)
return response