Stan Lagun 342ef70801 Added missing execution plan
Execution plan to patch existing k8s services to include node IPs
when number of nodes changed and we are not using gateways
or we was using gateways and now we don't

These 2 files were supposed to be in
https://review.openstack.org/#/c/175506/
but I forgot to commit them

Also fixes typo that is already fixed in stable/kilo

Change-Id: I6af644511bf25ae7ae31c88914d33b8c518a4eec
2015-04-22 03:28:37 +03:00

257 lines
7.8 KiB
YAML

# 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)
Methods:
initialize:
Body:
- $._environment: $.find(std:Environment).require()
- $podName: $._getPodName()
- $podDefinition: $.getAttr(lastPodDeployed, null)
- If: $podDefinition = null
Then:
- $podDefinition:
apiVersion: v1beta3
kind: Pod
metadata:
name: $podName
labels: $._getPodLabels($podName)
spec:
containers: []
volumes: []
- $.setAttr(lastPodDeployed, $podDefinition)
- $._podDefinition: $podDefinition
_getPodName:
Body:
- Return: toLower($.name)
_getPodLabels:
Arguments:
- podName:
Contract: $.string().notNull()
Body:
Return: $.labels2Map(toLower($.labels)).mergeWith(dict(id => $podName))
hostContainer:
Arguments:
- container:
Contract: $.class(docker:DockerContainer)
Body:
- $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: toLower($container.name)
image: $container.image
args: $container.commands
ports: $container.ports.select($this._getPortDefinition($))
volumeMounts: $container.volumes.keys().select(dict(name => $this._generateVolumeName($container.volumes.get($)), mountPath => $))
env: $container.env.keys().select(dict(key => $, value => $container.env.get($)))
- $newVolumes: $container.volumes.values().where(not $this._generateVolumeName($) in $this._podDefinition.spec.volumes.name).
select($this._buildVolumeEntry($))
- $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:
- 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($volume)
source: $spec
_deleteContainer:
Arguments:
- name:
Contract: $.string().notNull()
Body:
- $lenBefore: len($._podDefinition.spec.containers) + len($._podDefinition.spec.volumes)
- $newContainers: $._podDefinition.spec.containers.where($.name != $name)
- $newVolumes: $._podDefinition.spec.volumes.where(
$.name in $._podDefinition.spec.containers.volumeMounts.name)
- If: len($newContainers) + len($newVolumes) != $lenBefore
Then:
- $._podDefinition.spec.containers: $newContainers
- $._podDefinition.spec.volumes: $newVolumes
deleteContainer:
Arguments:
- name:
Contract: $.string().notNull()
Body:
- $._deleteContainer($name)
- $.kubernetesCluster.deleteService(
applicationName => $name,
podId => $._getPodName())
- $.deploy()
_generateVolumeName:
Arguments:
- volume:
Contract: $.class(docker:DockerVolume).notNull()
Body:
Return: toLower($volume.name)
deploy:
Body:
- $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: v1beta3
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()