-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathingress.py
More file actions
145 lines (117 loc) · 5.08 KB
/
ingress.py
File metadata and controls
145 lines (117 loc) · 5.08 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
from scheduler.exceptions import KubeHTTPException
from scheduler.resources import Resource
MANIFEAT_CLASSES = {}
class BaseManifest(object):
def manifest(self, 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": "extensions/v1beta1",
"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, ingress, ingress_class, namespace, **kwargs):
data = BaseManifest.manifest(
self, ingress, ingress_class, namespace, **kwargs)
if "whitelist" in kwargs:
whitelist = ", ".join(kwargs.pop("whitelist"))
data["metadata"]["annotations"].update({
"nginx.ingress.kubernetes.io/whitelist-source-range": whitelist
})
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, ingress, ingress_class, namespace, **kwargs):
data = BaseManifest.manifest(
self, ingress, ingress_class, namespace, **kwargs)
if "whitelist" in kwargs:
whitelist = ", ".join(kwargs.pop("whitelist"))
data["metadata"]["annotations"].update({
"ingress.kubernetes.io/whitelist-x-forwarded-for": "true",
"traefik.ingress.kubernetes.io/whitelist-source-range": whitelist
})
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):
short_name = 'ingress'
def manifest(self, ingress, ingress_class, namespace, **kwargs):
return MANIFEAT_CLASSES.get(ingress_class, BaseManifest)().manifest(
ingress, ingress_class, namespace, **kwargs
)
def get(self, namespace, name=None, **kwargs):
"""
Fetch a single Ingress or a list of Ingresses
"""
if name is not None:
url = "/apis/extensions/v1beta1/namespaces/%s/ingresses/%s" % (namespace, name)
message = 'get Ingress ' + name
else:
url = "/apis/extensions/v1beta1/namespaces/%s/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 = "/apis/extensions/v1beta1/namespaces/%s/ingresses" % namespace
data = self.manifest(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 = "/apis/extensions/v1beta1/namespaces/%s/ingresses/%s" % (namespace, ingress)
kwargs["version"] = version
data = self.manifest(ingress, ingress_class, namespace, **kwargs)
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 = "/apis/extensions/v1beta1/namespaces/%s/ingresses/%s" % (namespace, ingress)
response = self.http_delete(url)
if self.unhealthy(response.status_code):
raise KubeHTTPException(response, 'delete Ingress "{}"', namespace)
return response