-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathservice.py
More file actions
88 lines (76 loc) · 2.77 KB
/
service.py
File metadata and controls
88 lines (76 loc) · 2.77 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
from scheduler.exceptions import KubeHTTPException
from scheduler.resources import Resource
from scheduler.utils import dict_merge
class Service(Resource):
short_name = 'svc'
def get(self, namespace, name=None, **kwargs):
"""
Fetch a single Service or a list
"""
url = '/namespaces/{}/services'
args = [namespace]
if name is not None:
args.append(name)
url += '/{}'
message = 'get Service "{}" in Namespace "{}"'
else:
message = 'get Services in Namespace "{}"'
url = self.api(url, *args)
response = self.http_get(url, params=self.query_params(**kwargs))
if self.unhealthy(response.status_code):
args.reverse() # error msg is in reverse order
raise KubeHTTPException(response, message, *args)
return response
def create(self, namespace, name, **kwargs):
# Ports and app type will be overwritten as required
manifest = {
'kind': 'Service',
'apiVersion': 'v1',
'metadata': {
'name': name,
'labels': {
'app': namespace,
'heritage': 'deis'
},
'annotations': {}
},
'spec': {
'ports': [{
'name': 'http',
'port': 80,
'targetPort': 5000,
'protocol': 'TCP'
}],
'selector': {
'app': namespace,
'heritage': 'deis'
}
}
}
data = dict_merge(manifest, kwargs.get('data', {}))
url = self.api("/namespaces/{}/services", namespace)
response = self.http_post(url, json=data)
if self.unhealthy(response.status_code):
raise KubeHTTPException(
response,
'create Service "{}" in Namespace "{}"', namespace, namespace
)
return response
def update(self, namespace, name, data):
url = self.api("/namespaces/{}/services/{}", namespace, name)
response = self.http_put(url, json=data)
if self.unhealthy(response.status_code):
raise KubeHTTPException(
response,
'update Service "{}" in Namespace "{}"', namespace, name
)
return response
def delete(self, namespace, name):
url = self.api("/namespaces/{}/services/{}", namespace, name)
response = self.http_delete(url)
if self.unhealthy(response.status_code):
raise KubeHTTPException(
response,
'delete Service "{}" in Namespace "{}"', name, namespace
)
return response