6758cd0145b9739b1ffe023a2ea94a43edb88466
[dcaegen2/platform/plugins.git] / k8s / configure / configure.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 _CONFIG_PATH = "/opt/onap/config.txt"   # Path to config file on the Cloudify Manager host
22 _CONSUL_KEY = "k8s-plugin"              # Key under which CM configuration is stored in Consul
23
24 # Default configuration values
25 DCAE_NAMESPACE = "dcae"
26 CONSUL_DNS_NAME = "consul"
27
28 FB_LOG_PATH = "/var/log/onap"
29 FB_DATA_PATH = "/usr/share/filebeat/data"
30 FB_CONFIG_PATH = "/usr/share/filebeat/filebeat.yml"
31 FB_CONFIG_SUBPATH = "filebeat.yml"
32 FB_CONFIG_MAP = "filebeat-conf"
33 FB_IMAGE = "docker.elastic.co/beats/filebeat:5.5.0"
34
35 TLS_CERT_PATH = "/opt/tls/shared"
36 TLS_IMAGE = "onap/org.onap.dcaegen2.deployments.tls-init-container:1.0.0"
37
38 def _set_defaults():
39     """ Set default configuration parameters """
40     return {
41         "namespace" : DCAE_NAMESPACE,               # k8s namespace to use for DCAE
42         "consul_dns_name" : CONSUL_DNS_NAME,        # k8s internal DNS name for Consul
43         "image_pull_secrets" : [],                  # list of k8s secrets for accessing Docker registries
44         "filebeat": {                               # Configuration for setting up filebeat container
45             "log_path" : FB_LOG_PATH,               # mount point for log volume in filebeat container
46             "data_path" : FB_DATA_PATH,             # mount point for data volume in filebeat container
47             "config_path" : FB_CONFIG_PATH,         # mount point for config volume in filebeat container
48             "config_subpath" : FB_CONFIG_SUBPATH,   # subpath for config data in filebeat container
49             "config_map" : FB_CONFIG_MAP,           # ConfigMap holding the filebeat configuration
50             "image": FB_IMAGE                       # Docker image to use for filebeat
51         },
52         "tls": {                                    # Configuration for setting up TLS init container
53             "cert_path" : TLS_CERT_PATH,            # mount point for certificate volume in TLS init container
54             "image": TLS_IMAGE                      # Docker image to use for TLS init container
55         }
56     }
57
58 def configure(config_path=_CONFIG_PATH, key = _CONSUL_KEY):
59     """
60     Get configuration information from local file and Consul.
61     Note that the Cloudify context ("ctx") isn't available at
62     module load time.
63     """
64
65     from cloudify.exceptions import NonRecoverableError
66     import ConfigParser
67     from k8splugin import discovery
68     config = _set_defaults()
69
70     try:
71         # Get Consul address from a config file
72         c = ConfigParser.ConfigParser()
73         c.read(config_path)
74         config["consul_host"] = c.get('consul','address')
75
76         # Get the rest of the config from Consul
77         conn = discovery.create_kv_conn(config["consul_host"])
78         val = discovery.get_kv_value(conn, key)
79
80         # Merge Consul results into the config
81         config.update(val)
82
83     except discovery.DiscoveryKVEntryNotFoundError as e:
84         # Don't reraise error, assume defaults are wanted.
85         pass
86
87     except Exception as e:
88         raise NonRecoverableError(e)
89
90     return config