Cloudify support for OOM
[oom.git] / cloudify-onap / plugins / onap-installation-plugin / k8s_installer / common / namespace.py
1 ########
2 # Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 #    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #    * See the License for the specific language governing permissions and
14 #    * limitations under the License.
15
16 import cloudify_kubernetes.tasks as kubernetes_plugin
17 from cloudify import ctx
18 from cloudify.exceptions import NonRecoverableError
19
20 import deployment_result
21
22
23 def do_create_namespace():
24     namespace = _retrieve_namespace()
25     ctx.logger.info('Creating namespace: {0}'.format(namespace))
26
27     namespace_resource_template = _prepare_namespace_resource_template(
28         namespace
29     )
30
31     ctx.logger.debug(
32         'Kubernetes object which will be deployed: {0}'
33             .format(namespace_resource_template)
34     )
35
36     kubernetes_plugin.custom_resource_create(**namespace_resource_template)
37     deployment_result.save_deployment_result('namespace')
38     ctx.logger.info('Namespace created successfully')
39
40
41 def do_delete_namespace():
42     namespace = _retrieve_namespace()
43     ctx.logger.info('Deleting namespace: {0}'.format(namespace))
44
45     namespace_resource_template = _prepare_namespace_resource_template(
46         namespace
47     )
48
49     ctx.logger.debug(
50         'Kubernetes object which will be deleted: {0}'
51             .format(namespace_resource_template)
52     )
53
54     deployment_result.set_deployment_result('namespace')
55     kubernetes_plugin.custom_resource_delete(**namespace_resource_template)
56     ctx.logger.info('Namespace deleted successfully')
57
58
59
60 def _retrieve_namespace():
61
62     default_namespace = ctx.node.properties.get('options', {}).get('namespace')
63     namespace = ctx.node.properties.get('namespace', default_namespace)
64
65     if not namespace:
66         raise NonRecoverableError(
67             'Namespace is not defined (node={})'.format(ctx.node.name)
68         )
69
70     return namespace
71
72
73 def _prepare_namespace_resource_template(name):
74     return {
75         'definition': {
76             'apiVersion': 'v1',
77             'kind': 'Namespace',
78             'metadata': {
79                 'name': name,
80                 'labels': {
81                     'name': name
82                 },
83             },
84         },
85         'api_mapping': {
86             'create': {
87                 'api': 'CoreV1Api',
88                 'method': 'create_namespace',
89                 'payload': 'V1Namespace'
90             },
91             'read': {
92                 'api': 'CoreV1Api',
93                 'method': 'read_namespace',
94             },
95             'delete': {
96                 'api': 'CoreV1Api',
97                 'method': 'delete_namespace',
98                 'payload': 'V1DeleteOptions'
99             }
100         }
101     }