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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#jinja2: trim_blocks: "true", lstrip_blocks: "false"
---
apiVersion: v1
kind: Template
metadata:
name: {{ name | default(kaas_project) }}-pods
annotations:
descriptions: {{ kaas_project_config.description | default(name | default(kaas_project) ~ " auto-generated pod template") }}
objects:
{% for name, pod in pods.iteritems() %}
{% set kind = pod.kind | default('DeploymentConfig') %}
{% if pod.enabled | default(true) %}
{% set pubkey = "kaas_" ~ name ~ "_pubkey" %}
{% set privkey = "kaas_" ~ name ~ "_privkey" %}
{% set cakey = "kaas_" ~ name ~ "_ca" %}
{% if pod.variant is defined %}
{% set pod = pod[pod.variant] %}
{% endif %}
{% set sched = pod.sched | default({}) %}
{% set node_selector = (sched.selector is defined) | ternary(sched.selector, ands_default_node_selector | combine(sched.restrict | default({}))) %}
{% if pod.service is defined %}
{% if kind == 'StatefulSet' and pod.service.ports is defined %}
- apiVersion: v1
kind: Service
metadata:
name: {{ pod.name | default(name) }}-ss
annotations: {{ pod.service.annotations | default({}) | combine({"service.alpha.kubernetes.io/tolerate-unready-endpoints": "true" }) | to_json }}
spec:
clusterIP: None
publishNotReadyAddresses: True
selector:
name: {{ pod.name | default(name) }}
ports:
{% for port in pod.service.ports %}
{% set portmap = (port | string).split('/') %}
- name: "{{ portmap[0] }}"
port: {{ portmap[0] }}
targetPort: {{ (portmap[1] is defined) | ternary(portmap[1], portmap[0]) }}
{% endfor %}
{% endif %}
- apiVersion: v1
kind: Service
metadata:
name: {{ pod.name | default(name) }}
{% if pod.service.annotations is defined %}
annotations: {{ pod.service.annotations | to_json }}
{% endif %}
spec:
selector:
name: {{ pod.name | default(name) }}
{% if pod.service.ip is defined %}
clusterIP: {{ pod.service.ip }}
{% endif %}
{% if pod.service.ports is defined %}
ports:
{% for port in pod.service.ports %}
{% set portmap = (port | string).split('/') %}
- name: "{{ portmap[0] }}"
port: {{ portmap[0] }}
targetPort: {{ (portmap[1] is defined) | ternary(portmap[1], portmap[0]) }}
{% endfor %}
{% endif %}
{% if (pod.service.ports is defined) and (pod.service.host is defined) %}
{% set first_port = (pod.service.ports[0] | string).split('/') %}
- apiVersion: v1
kind: Route
metadata:
name: {{ pod.name | default(name) }}
spec:
host: {{ pod.service.host }}
to:
kind: Service
name: {{ pod.name | default(name) }}
port:
targetPort: {{ (first_port[1] is defined) | ternary(first_port[1], first_port[0]) }}
{% if (first_port[0] == "80") %}
tls:
termination: edge
insecureEdgeTerminationPolicy: Allow
{% if hostvars[inventory_hostname][pubkey] is defined %}
certificate: |-
{{ hostvars[inventory_hostname][pubkey] | indent(10) }}
{% endif %}
{% if hostvars[inventory_hostname][privkey] is defined %}
key: |-
{{ hostvars[inventory_hostname][privkey] | indent(10) }}
{% endif %}
{% if hostvars[inventory_hostname][cakey] is defined %}
caCertificate: |-
{{ hostvars[inventory_hostname][cakey] | indent(10) }}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
- apiVersion: {{ kaas_openshift_api_versions[kind] | default('v1') }}
kind: {{ kind }}
metadata:
name: {{ pod.name | default(name) }}
spec:
replicas: {{ ( sched | default({})).replicas | default(1) }}
revisionHistoryLimit: 2
strategy:
type: {{ (sched | default({})).strategy | default('Rolling') }}
updateStrategy:
{% if pod.update %}
type: {{ pod.update.strategy | default('OnDelete') }}
{% if pod.update.min_ready is defined %}
minReadySeconds: {{ pod.update.min_ready }}
{% endif %}
{% endif %}
triggers:
- type: ConfigChange
{% if kind == 'StatefulSet' %}
serviceName: {{ pod.name | default(name) }}-ss
selector:
matchLabels:
name: {{ pod.name | default(name) }}
{% else %}
selector:
name: {{ pod.name | default(name) }}
{% endif %}
template:
metadata:
name: {{ pod.name | default(name) }}
{% if kind == 'StatefulSet' %}
annotations: {{ pod.annotations | default({}) | combine({"pod.alpha.kubernetes.io/initialized": "true"}) | to_json }}
{% elif pod.annotations is defined %}
annotations: {{ pod.annotations | to_json }}
{% endif %}
labels:
name: {{ pod.name | default(name) }}
spec:
{% if node_selector | length > 0 %}
nodeSelector: {{ node_selector | to_json }}
{% endif %}
{% set mappings = (pod.images | json_query('[*].mappings') | length) %}
{% set paths = (pod.images | json_query('[*].hostpath') | length) %}
{% if mappings > 0 or paths > 0 %}
volumes:
{% for img in pod.images %}
{% set imgidx = loop.index %}
{% for vol in (img.mappings | default([])) %}
{% set oc_name = vol.name | default(name) | regex_replace('_','-') %}
- name: vol-{{imgidx}}-{{loop.index}}
persistentVolumeClaim:
claimName: {{ oc_name }}
{% endfor %}
{% for vol in (img.hostpath | default([])) %}
- name: host-{{imgidx}}-{{loop.index}}
hostPath:
path: {{ vol.path }}
{% endfor %}
{% endfor %}
{% endif %}
{% if (pod.groups is defined) or (pod.run_as is defined) %}
securityContext:
{% if (pod.run_as is defined) %}
runAsUser: {{ (kaas_project_uids[pod.run_as] is defined) | ternary(kaas_project_uids[pod.run_as].id, pod.run_as) }}
{% endif %}
{% if (pod.groups is defined) %}
{% if (ands_openshift_gid_mode | default('')) == "RunAsAny" %}
fsGroup: {{ (kaas_project_gids[pod.groups[0]] is defined) | ternary(kaas_project_gids[pod.groups[0]].id, pod.groups[0]) }}
{% endif %}
supplementalGroups:
{% for group in pod.groups %}
- {{ (kaas_project_gids[group] is defined) | ternary(kaas_project_gids[group].id, group) }}
{% endfor %}
{% endif %}
{% endif %}
containers:
{% for img in pod.images %}
{% set imgidx = loop.index %}
- name: {{ img.name | default(pod.name) | default(name) }}
image: {{ img.image }}
imagePullPolicy: {{ img.pull | default('Always') }}
{% if (img.command is defined) %}
command: {{ img.command | to_json }}
{% endif %}
{% if img.ports is defined %}
ports:
{% for port in img.ports %}
- containerPort: {{ port }}
{% endfor %}
{% elif pod.service.ports is defined %}
ports:
{% for port in pod.service.ports %}
{% set portmap = (port | string).split('/') %}
- containerPort: {{ (portmap[1] is defined) | ternary(portmap[1], portmap[0]) }}
{% endfor %}
{% endif %}
{% if kind == 'StatefulSet' %}
{% set extra_env = [ { "name": "POD_NAMESPACE", "value": "fieldref@metadata.namespace" }, { "name": "POD_REPLICAS", "value": sched.replicas } ] %}
{% set env = img.env | default([]) | union(extra_env) %}
{% elif img.env is defined %}
{% set env = img.env %}
{% endif %}
{% if img.env is defined %}
env:
{% for env_item in env %}
{% set env_name = env_item.name %}
{% set env_val = env_item.value %}
{% set env_parts = (env_val | string).split('@') %}
- name: "{{ env_name }}"
{% if env_parts[0] == "secret" %}
{% set env_sec = (env_parts[1] | string).split('/') %}
valueFrom:
secretKeyRef:
name: {{ env_sec[0] }}
key: {{ env_sec[1] }}
{% elif env_parts[0] == "cm" %}
{% set env_cm = (env_parts[1] | string).split('/') %}
valueFrom:
configMapKeyRef:
name: {{ env_cm[0] }}
key: {{ env_cm[1] }}
{% elif env_parts[0] == "fieldref" %}
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: {{ env_parts[1] }}
{% else %}
value: "{{ env_val }}"
{% endif %}
{% endfor %}
{% endif %}
{% if img.mappings is defined or img.hostpath is defined %}
volumeMounts:
{% for vol in (img.mappings | default([])) %}
- name: vol-{{imgidx}}-{{loop.index}}
subPath: {{ vol.path | default("") }}
mountPath: {{ vol.mount }}
{% endfor %}
{% for vol in (img.hostpath | default([])) %}
- name: host-{{imgidx}}-{{loop.index}}
mountPath: {{ vol.mount }}
{% endfor %}
{% endif %}
{% if img.resources is defined %}
resources:
{% if img.resources.request is defined %}
{% set res = img.resources.request %}
requests:
{% if res.cpu %}
cpu: {{ res.cpu }}
{% endif %}
{% if res.cpu %}
memory: {{ res.mem }}
{% endif %}
{% endif %}
{% if img.resources.limit is defined %}
{% set res = img.resources.limit %}
limits:
{% if res.cpu %}
cpu: {{ res.cpu }}
{% endif %}
{% if res.cpu %}
memory: {{ res.mem }}
{% endif %}
{% endif %}
{% endif %}
{% if img.probes is defined %}
{% for probe in img.probes %}
{% if (probe.type is undefined) %}
{% set seq = ['livenessProbe', 'readinessProbe'] %}
{% elif (probe.type == "liveness") %}
{% set seq = ['livenessProbe'] %}
{% else %}
{% set seq = ['readinessProbe'] %}
{% endif %}
{% for type in seq %}
{{ type }}:
timeoutSeconds: {{ probe.timeout | default(1) }}
initialDelaySeconds: {{ probe.delay | default(10) }}
{% if (probe.command is defined) %}
exec:
command: {{ probe.command | to_json }}
{% elif (probe.path is defined) %}
httpGet:
path: {{ probe.path }}
port: {{ probe.port | default(80) }}
{% else %}
tcpSocket:
port: {{ probe.port | default(80) }}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% if img.hooks is defined %}
lifecycle:
{% for hook in img.hooks %}
{{ hook.type }}:
exec:
command: {{ hook.command | to_json }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
|