diff --git a/DockerInterfacesLibrary/package/Classes/DockerApplication.yaml b/DockerInterfacesLibrary/package/Classes/DockerApplication.yaml index aba9fd1..78faae2 100644 --- a/DockerInterfacesLibrary/package/Classes/DockerApplication.yaml +++ b/DockerInterfacesLibrary/package/Classes/DockerApplication.yaml @@ -59,6 +59,16 @@ Methods: - $.onInstallationFinish() + destroy: + Body: + - $.host.deploy() + - If: $.getAttr(container, null) != null + Then: + - $container: $.getContainer() + - $.host.deleteContainer($container.name) + - $.setAttr(container, null) + + getConnectionTo: Arguments: - application: diff --git a/DockerInterfacesLibrary/package/Classes/DockerContainerHost.yaml b/DockerInterfacesLibrary/package/Classes/DockerContainerHost.yaml index 41df2f9..ad88d55 100644 --- a/DockerInterfacesLibrary/package/Classes/DockerContainerHost.yaml +++ b/DockerInterfacesLibrary/package/Classes/DockerContainerHost.yaml @@ -34,6 +34,7 @@ Methods: - name: Contract: $.string().notNull() + getEndpoints: Arguments: - applicationName: diff --git a/DockerStandaloneHost/package/Classes/DockerStandaloneHost.yaml b/DockerStandaloneHost/package/Classes/DockerStandaloneHost.yaml index a70f599..9ec11f0 100644 --- a/DockerStandaloneHost/package/Classes/DockerStandaloneHost.yaml +++ b/DockerStandaloneHost/package/Classes/DockerStandaloneHost.yaml @@ -200,7 +200,7 @@ Methods: - $applicationPorts: $.getAttr(applicationPorts, dict()) - $key: format('{0}-{1}-{2}', $applicationPort.port, $applicationPort.protocol, $applicationName) - - If: $key in $applicationPorts + - If: $key in $applicationPorts.keys() Then: - Return: $applicationPorts.get($key) @@ -242,7 +242,7 @@ Methods: - For: key In: $applicationPorts.keys() Do: - - $value: $applicationPorts.get($port) + - $value: $applicationPorts.get($key) - If: not $value in $portsToDelete Then: - $newApplicationPorts[$key]: $value diff --git a/Kubernetes/KubernetesCluster/package/Classes/KubernetesCluster.yaml b/Kubernetes/KubernetesCluster/package/Classes/KubernetesCluster.yaml index b4f760f..c275fbb 100644 --- a/Kubernetes/KubernetesCluster/package/Classes/KubernetesCluster.yaml +++ b/Kubernetes/KubernetesCluster/package/Classes/KubernetesCluster.yaml @@ -72,6 +72,7 @@ Methods: deploy: Body: + - $.serviceEndpoints: $.getAttr(serviceEndpoints, list()) - If: not $.getAttr(deployed, false) Then: - $._environment.reporter.report($this, 'Creating VMs for Kubernetes cluster') @@ -127,6 +128,7 @@ Methods: - $.setAttr(lastNodeCount, $.nodeCount) - $.setAttr(lastGatewayCount, $.gatewayCount) - $._environment.reporter.report($this, 'Kubernetes cluster is up and running') + - $.setAttr(serviceEndpoints, $.serviceEndpoints) getIp: @@ -201,6 +203,7 @@ Methods: - podId: Contract: $.string().notNull() Body: + - $.serviceEndpoints: $.getAttr(serviceEndpoints, list()) - $currentEndpoints: $.serviceEndpoints.where($.applicationName = $applicationName and $.podId = $podId and $.scope = internal) - $serviceName: format('svc-{0}', randomName()) - $endpointMap: {} @@ -256,6 +259,7 @@ Methods: serviceIp => $serviceIp ) - $._environment.stack.push() + - $.setAttr(serviceEndpoints, $.serviceEndpoints) _createOrUpdateService: @@ -396,18 +400,22 @@ Methods: - $.masterNode.instance.agent.call($template, $resources) - deleteService: + deleteServices: Arguments: - applicationName: Contract: $.string().notNull() - podId: Contract: $.string().notNull() Body: + - $.serviceEndpoints: $.getAttr(serviceEndpoints, list()) - $resources: new(sys:Resources) - - $template: $resources.yaml('DeleteService.template').bind(dict( - serviceId => $service + - $services: $.serviceEndpoints.where($.scope = internal and $.podId = $podId).select($.serviceName) + - $template: $resources.yaml('DeleteServices.template').bind(dict( + services => $services )) - $.masterNode.instance.agent.call($template, $resources) + - $.serviceEndpoints: $.serviceEndpoints.where($.podId != $podId) + - $.setAttr(serviceEndpoints, $.serviceEndpoints) _findUnusedPort: diff --git a/Kubernetes/KubernetesCluster/package/Resources/DeleteService.template b/Kubernetes/KubernetesCluster/package/Resources/DeleteServices.template similarity index 85% rename from Kubernetes/KubernetesCluster/package/Resources/DeleteService.template rename to Kubernetes/KubernetesCluster/package/Resources/DeleteServices.template index 69df2a3..774588c 100644 --- a/Kubernetes/KubernetesCluster/package/Resources/DeleteService.template +++ b/Kubernetes/KubernetesCluster/package/Resources/DeleteServices.template @@ -12,13 +12,15 @@ FormatVersion: 2.0.0 Version: 1.0.0 -Name: Delete Service +Name: Delete Services Parameters: - serviceId: $serviceId + services: $services Body: | - return deleteService('{0}'.format(args.serviceId)).stdout + services = set(args.services) + for service in services: + deleteService(service) Scripts: deleteService: diff --git a/Kubernetes/KubernetesCluster/package/Resources/scripts/deleteService.sh b/Kubernetes/KubernetesCluster/package/Resources/scripts/deleteService.sh index cba8d8f..b832c9e 100644 --- a/Kubernetes/KubernetesCluster/package/Resources/scripts/deleteService.sh +++ b/Kubernetes/KubernetesCluster/package/Resources/scripts/deleteService.sh @@ -1,3 +1,3 @@ #!/bin/bash echo "Deleting a Service" >> /tmp/murano-kube.log -/opt/bin/kubectl delete services $1 >> /tmp/murano-kube.log +/opt/bin/kubectl delete service $1 >> /tmp/murano-kube.log diff --git a/Kubernetes/KubernetesPod/package/Classes/KubernetesPod.yaml b/Kubernetes/KubernetesPod/package/Classes/KubernetesPod.yaml index 33bf95c..4ef87e1 100644 --- a/Kubernetes/KubernetesPod/package/Classes/KubernetesPod.yaml +++ b/Kubernetes/KubernetesPod/package/Classes/KubernetesPod.yaml @@ -41,23 +41,37 @@ Methods: initialize: Body: - $._environment: $.find(std:Environment).require() + - $._podDefinition: null - - $podName: $._getPodName() - - $podDefinition: $.getAttr(lastPodDeployed, null) - - If: $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: - - $podDefinition: - apiVersion: v1beta3 - kind: Pod - metadata: - name: $podName - labels: $._getPodLabels($podName) - spec: - containers: [] - volumes: [] + - $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 + - Return: $._podDefinition - - $.setAttr(lastPodDeployed, $podDefinition) - - $._podDefinition: $podDefinition _getPodName: Body: @@ -75,6 +89,7 @@ Methods: - container: Contract: $.class(docker:DockerContainer) Body: + - $._loadCurrentPodDefinition() - $podName: $._getPodName() - For: port In: $container.ports @@ -93,11 +108,15 @@ Methods: 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 => $)) + 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().where(not $this._generateVolumeName($) in $this._podDefinition.spec.volumes.name). - select($this._buildVolumeEntry($)) + - $newVolumes: $container.volumes.values().select( + $this._buildVolumeEntry($container.name, $)) - $diff: spec: @@ -136,6 +155,8 @@ Methods: _buildVolumeEntry: Arguments: + - name: + Contract: $.string().notNull() - volume: Contract: $.class(docker:DockerVolume).notNull() Body: @@ -153,7 +174,7 @@ Methods: - Throw: UnknownDockerVolumeType Message: format('Unknown docker volume type {0}', $type) - Return: - name: $._generateVolumeName($volume) + name: $._generateVolumeName($name, $volume) source: $spec @@ -163,9 +184,25 @@ Methods: Contract: $.string().notNull() Body: - $lenBefore: len($._podDefinition.spec.containers) + len($._podDefinition.spec.volumes) - - $newContainers: $._podDefinition.spec.containers.where($.name != $name) + - $containerName: toLower($name) + - $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( - $.name in $._podDefinition.spec.containers.volumeMounts.name) + not ($.name in $volumesToDelete)) - If: len($newContainers) + len($newVolumes) != $lenBefore Then: - $._podDefinition.spec.containers: $newContainers @@ -177,8 +214,9 @@ Methods: - name: Contract: $.string().notNull() Body: + - $._loadCurrentPodDefinition() - $._deleteContainer($name) - - $.kubernetesCluster.deleteService( + - $.kubernetesCluster.deleteServices( applicationName => $name, podId => $._getPodName()) - $.deploy() @@ -186,14 +224,25 @@ Methods: _generateVolumeName: Arguments: + - name: + Contract: $.string().notNull() - volume: Contract: $.class(docker:DockerVolume).notNull() Body: - Return: toLower($volume.name) + - $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)