-
Notifications
You must be signed in to change notification settings - Fork 1
/
pymanifest.py
184 lines (153 loc) · 6.24 KB
/
pymanifest.py
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
183
184
#build manifest
#always required: api_version, kind, object_name, namespace
def manifest_builder(**kwargs):
results = None
msg =""
error = ""
msg += '\nmanifest_builder started.'
#check basic requirements to build a manifest
if 'api_version' not in kwargs or 'kind' not in kwargs or 'object_name' not in kwargs or 'namespace' not in kwargs:
error += '\n required fields are not given, i.e., api_version, kind, object_name, or namespace.'
return results, msg, error
#pick a builder
#TrafficSplit
if kwargs['kind'] == 'TrafficSplit':
manifest, msg_child, error = trafficSplit(**kwargs)
msg += msg_child
elif kwargs['kind'] == 'Function':
manifest, msg_child, error = function(**kwargs)
msg += msg_child
elif kwargs['kind'] == 'Deployment':
manifest, msg_child, error = deployment(**kwargs)
msg += msg_child
elif kwargs['kind'] == 'Service':
manifest, msg_child, error = service(**kwargs)
msg += msg_child
else:
error +='\nKind: ' + kwargs['kind'] + ' not implemented'
msg += '\nmanifest_builder stopped'
return manifest, msg, error
# manifest builder for trafficSplit
def trafficSplit(**kwargs):
results= None; msg=""; error=""
msg +="manifest builder for trafficSplit started."
#verify especial fileds for a TrafficSplit, e.g., backend and service
if not 'backends' in kwargs or not 'service' in kwargs:
error += '\nNo backends and/or service are given in kwargs'
return results, msg, error
#manifest
manifest = {
"apiVersion": kwargs['api_version'],
"kind": kwargs['kind'],
"metadata": {
"name": kwargs['object_name'],
"namespace": kwargs['namespace']
},
"spec": {
"backends": kwargs['backends'],
"service": kwargs['service']
}
}
msg += '\ntrafficSplit: stopped'
return manifest, msg, error
# manifest builder for Function
def function(**kwargs):
results= None; msg=""; error=""
msg +="manifest builder for Function started."
#verify especial fileds for a Function, e.g., image
if not 'image' in kwargs:
error += '\nNo image is given in kwargs'
return results, msg, error
#manifest
manifest = {
"apiVersion": kwargs['api_version'],
"kind": kwargs['kind'],
"metadata": {
"name": kwargs['object_name'],
"namespace": kwargs['namespace']
},
"spec": {
"name": kwargs['object_name'],
'image': kwargs['image'],
'labels': kwargs['labels'],
'annotations': kwargs['annotations'],
'constraints': kwargs['constraints'],
}
}
msg +="\nmanifest builder for Function stopped"
return manifest, msg, error
# manifest builder for Deployment
def deployment(**kwargs):
results= None; msg=""; error=""
msg +="manifest builder for Deployment started."
#verify especial fileds for a Deployment, e.g., image
if not 'image' in kwargs:
error += '\nNo image is given in kwargs'
return results, msg, error
#manifest
manifest = {
"apiVersion": kwargs['api_version'],
"kind": kwargs['kind'],
"metadata": {
"name": kwargs['object_name'],
"namespace": kwargs['namespace'],
},
"spec": {
"replicas": kwargs['replicas'] if 'replicas' in kwargs else 1,
"selector": {
"matchLabels": kwargs['matchLabels'] if 'matchLabels' in kwargs else {"app": kwargs['object_name']},
},
"template": {
"metadata": {
"labels": kwargs['labels'] if 'labels' in kwargs else {"app": kwargs['object_name']},
"annotations": kwargs['annotations'] if 'annotations' in kwargs else {},
},
"spec":{
"hostNetwork": kwargs['hostNetwork'] if 'hostNetwork' in kwargs else False,
"nodeName": kwargs['nodeName'] if 'nodeName' in kwargs else "",
"containers": kwargs['containers'] if 'containers' in kwargs else [
{
"name": kwargs['container_name'] if 'container_name' in kwargs else kwargs['object_name'],
"image": kwargs['image'],
"imagePullPolicy": kwargs['imagePullPolicy'] if 'imagePullPolicy' in kwargs else 'IfNotPresent',
"ports": kwargs['ports'] if 'ports' in kwargs else
[{"containerPort": 8080 },],
"env": kwargs['env'] if 'env' in kwargs else [],
"securityContext": kwargs['securityContext'] if 'securityContext' in kwargs else {},
"volumeMounts": kwargs['volumeMounts'] if 'volumeMounts' in kwargs else [],
}
],
"volumes": kwargs['volumes'] if 'volumes' in kwargs else [],
},
},
},
}
msg +="\nmanifest builder for Deployment stopped"
return manifest, msg, error
# manifest builder for Service
def service(**kwargs):
results= None; msg=""; error=""
msg +="manifest builder for Service started."
#manifest
manifest = {
"apiVersion": kwargs['api_version'],
"kind": kwargs['kind'],
"metadata": {
"name": kwargs['object_name'],
"namespace": kwargs['namespace']
},
"spec": {
"clusterIP": kwargs['clusterIP'] if 'clusterIP' in kwargs else None,
"clusterIPs": kwargs['clusterIPs'] if 'clusterIPs' in kwargs else [kwargs['clusterIP']] if 'clusterIP' in kwargs else None,
"selector": kwargs['selector'] if 'selector' in kwargs else {"app": kwargs['object_name']},
"ports": kwargs['ports'] if 'ports' in kwargs else [
{'protocol': 'TCP',
'port': 8080,
'targetport': 8080},
],
}
}
#clean manifest
manifest = {k: v for k, v in manifest.items() if v is not None}
msg +="\nmanifest builder for Service stopped"
return manifest, msg, error