This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
KubernetesPod.yaml
349 lines (300 loc) · 10.9 KB
/
KubernetesPod.yaml
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
Namespaces:
=: io.murano.apps.docker.kubernetes
docker: io.murano.apps.docker
std: io.murano
sys: io.murano.system
Name: KubernetesPod
Extends:
- docker:DockerContainerHost
- docker:DockerHelpers
Properties:
name:
Contract: $.string().notNull()
kubernetesCluster:
Contract: $.class(KubernetesCluster).notNull()
labels:
Contract: $.string().notNull() # convert to key-value map as soon as it will be possible to input it in UI
Default: ''
replicas:
Contract: $.int().notNull().check($ >= 0)
Usage: InOut
Methods:
.init:
Body:
- $._environment: $.find(std:Environment).require()
- $._podDefinition: null
# this is a workaround for early initialization. If docker application was deleted
# its destroy method will call deleteContainer method that will change _podDefinition
# on KubernetesPod loaded from ObjectsCopy
# but ininitialize method on a KubernetesPod from Objects is called before destruction
# and because we have 2 different KubernetesPod objects that share attributes but
# not private variables _podDefinition of real object will still contain outdated data
_loadCurrentPodDefinition:
Body:
- If: $._podDefinition = null
Then:
- $podName: $._getPodName()
- $podDefinition: $.getAttr(lastPodDeployed, null)
- If: $podDefinition = null
Then:
- $podDefinition:
apiVersion: v1
kind: Pod
metadata:
name: $podName
labels: $._getPodLabels($podName)
spec:
containers: []
volumes: []
- $.setAttr(lastPodDeployed, $podDefinition)
- $._podDefinition: $podDefinition
- Return: $._podDefinition
_getPodName:
Body:
- Return: $.name.toLower()
_getPodLabels:
Arguments:
- podName:
Contract: $.string().notNull()
Body:
Return: $.labels2Map($.labels.toLower()).mergeWith(dict(id => $podName))
hostContainer:
Arguments:
- container:
Contract: $.class(docker:DockerContainer)
Body:
- $._loadCurrentPodDefinition()
- $podName: $._getPodName()
- For: port
In: $container.ports
Do:
- $endpoints: $.kubernetesCluster.serviceEndpoints.where(
$.containerPort = $port.port and $.protocol = $port.protocol and $.podId = $podName)
- If: len($endpoints) > 0
Then:
- $msg: format('Port {0} is already used in the Pod {1}', $port.port, $.name)
- Throw: PortConflictException
Message: $msg
- $._deleteContainer($container.name)
- $containerDef:
name: $container.name.toLower()
image: $container.image
args: $container.commands
ports: $container.ports.select($this._getPortDefinition($))
volumeMounts: $container.volumes.keys().select(
dict(
name => $this._generateVolumeName($container.name, $container.volumes.get($)),
mountPath => $
))
env: $container.env.keys().select(dict(name => $, value => $container.env.get($)))
- $newVolumes: $container.volumes.values().select(
$this._buildVolumeEntry($container.name, $))
- $diff:
spec:
containers: [$containerDef]
volumes: $newVolumes
- $._podDefinition: $._podDefinition.mergeWith($diff)
- $.deploy()
- $._environment.reporter.report($, 'Creating services for Pod {0}'.format($.name))
- $.kubernetesCluster.createService(
applicationName => $container.name,
applicationPorts => $container.ports,
podId => $podName)
- Return: $.getEndpoints($container.name)
getEndpoints:
Arguments:
- applicationName:
Contract: $.string().notNull()
Body:
- Return: $.kubernetesCluster.serviceEndpoints.where($.applicationName = $applicationName)
_getPortDefinition:
Arguments:
- port:
Contract: $.class(docker:ApplicationPort).notNull()
Body:
- $result:
containerPort: $port.port
- If: $port.scope = node
Then:
$result.hostPort: $port.port
- Return: $result
_buildVolumeEntry:
Arguments:
- name:
Contract: $.string().notNull()
- volume:
Contract: $.class(docker:DockerVolume).notNull()
Body:
- $type: $volume.getType()
- Value: $type
Match:
HostDir:
- $spec:
hostDir:
path: $volume.getParameters()
TempVolume:
- $spec:
emptyDir: {}
Default:
- Throw: UnknownDockerVolumeType
Message: format('Unknown docker volume type {0}', $type)
- Return:
name: $._generateVolumeName($name, $volume)
source: $spec
_deleteContainer:
Arguments:
- name:
Contract: $.string().notNull()
Body:
- $lenBefore: len($._podDefinition.spec.containers) + len($._podDefinition.spec.volumes)
- $containerName: $name.toLower()
- $newContainers: $._podDefinition.spec.containers.where($.name != $containerName)
- $volumeNameMap: $.getAttr(volumeNameMap, dict())
- $newVolumeNameMap: {}
- $volumesToDelete: []
- $prefix: format('{0}/'.format($name))
- For: key
In: $volumeNameMap.keys()
Do:
- $volumeName: $volumeNameMap.get($key)
- If: $key.startsWith($prefix)
Then:
- $volumesToDelete: $volumesToDelete + list($volumeName)
Else:
- $newVolumeNameMap[$key]: $volumeName
- $.setAttr(volumeNameMap, $newVolumeNameMap)
- $newVolumes: $._podDefinition.spec.volumes.where(
not ($.name in $volumesToDelete))
- If: len($newContainers) + len($newVolumes) != $lenBefore
Then:
- $._podDefinition.spec.containers: $newContainers
- $._podDefinition.spec.volumes: $newVolumes
deleteContainer:
Arguments:
- name:
Contract: $.string().notNull()
Body:
- If: $.kubernetesCluster.isAvailable()
Then:
- $._loadCurrentPodDefinition()
- $._deleteContainer($name)
- $.kubernetesCluster.deleteServices(
applicationName => $name,
podId => $._getPodName())
- $.deploy()
_generateVolumeName:
Arguments:
- name:
Contract: $.string().notNull()
- volume:
Contract: $.class(docker:DockerVolume).notNull()
Body:
- $key: format('{0}/{1}', $name, $volume.name)
- $map: $.getAttr(volumeNameMap, dict())
- $volumeName: $map.get($key)
- If: $volumeName = null
Then:
- $volumeName: randomName()
- $map[$key]: $volumeName
- $.setAttr(volumeNameMap, $map)
- Return: $volumeName
deploy:
Body:
- $._loadCurrentPodDefinition()
- $prevPod: $.getAttr(lastPodDeployed, null)
- $prevReplicas: $.getAttr(lastReplicas, 0)
- $podDefinition: $._podDefinition
- $replicas: $.replicas
- If: len($podDefinition.spec.containers) = 0
Then:
- $replicas: 0
- $.setAttr(lastReplicas, $replicas)
- If: $replicas != $prevReplicas or $prevPod != $podDefinition
Then:
- If: $replicas > 0
Then:
- $._environment.reporter.report($, 'Deploying Replication Controller for Pod {0}'.format($.name))
- $rcDefinition: $._buildReplicationControllerDefinition($podDefinition)
- $.kubernetesCluster.createReplicationController(
definition => $rcDefinition, isNew => $prevReplicas = 0)
- If: $replicas = 0 and $prevReplicas > 0
Then:
- $.kubernetesCluster.deleteReplicationController($._getReplicationControllerId())
- If: $prevPod != $podDefinition and len($prevPod.spec.containers) > 0
Then:
- $.kubernetesCluster.deletePods(dict(id => $._getPodName()))
- If: $.replicas = 0 and len($podDefinition.spec.containers) > 0
Then:
- $.kubernetesCluster.createPod(definition => $podDefinition, isNew => true)
- $._environment.reporter.report($, 'Pod {0} is ready'.format($.name))
- $.setAttr(lastPodDeployed, $podDefinition)
_buildReplicationControllerDefinition:
Arguments:
- podDefinition:
Contract: {}
Body:
Return:
apiVersion: v1
kind: ReplicationController
metadata:
name: $._getReplicationControllerId()
labels: $podDefinition.metadata.labels
spec:
replicas: $.replicas
selector:
id: $._getPodName()
template:
metadata:
labels: $podDefinition.metadata.labels
spec: $podDefinition.spec
_getReplicationControllerId:
Body:
- Return: $._getPodName()
getInternalScopeId:
Body:
Return: $.kubernetesCluster.id()
scalePodDown:
Usage: Action
Body:
- If: $.replicas > 1
Then:
- $._environment.reporter.report($this, 'Scaling Pod down')
- $.replicas: $.replicas - 1
- $.kubernetesCluster.scaleRc(rcName => $._getReplicationControllerId(), newSize => $.replicas)
Else:
- $._environment.reporter.report($this, 'Cannot scale Pod down')
scalePodUp:
Usage: Action
Body:
- If: $.replicas > 0
Then:
- $._environment.reporter.report($this, 'Scaling Pod up')
- $.replicas: $.replicas + 1
- $.kubernetesCluster.scaleRc(rcName => $._getReplicationControllerId(), newSize => $.replicas)
Else:
- $._environment.reporter.report($this, 'Cannot scale Pod up')
recreatePod:
Usage: Action
Body:
- $._environment.reporter.report($this, 'Recreating Pod {0}'.format($.name))
- $.kubernetesCluster.deletePods(dict(id => $._getPodName()))
- If: $.replicas = 0
Then:
- $._loadCurrentPodDefinition()
- $.kubernetesCluster.createPod(definition => $._podDefinition, isNew => true)
- $._environment.reporter.report($this, 'Pod {0} recreated'.format($.name))
restartContainers:
Usage: Action
Body:
- $.kubernetesCluster.restartContainers(podName => $._getPodName())