Merge "feature(prometheus): Configuration can be dynamically replaced"
[osm/devops.git] / installers / charm / nbi-k8s / reactive / nbi.py
1 # Copyright 2020 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from charms.layer.caas_base import pod_spec_set
15 from charms.reactive import endpoint_from_flag
16 from charms.reactive import when, when_not, hook
17 from charms.reactive.flags import set_flag, clear_flag
18 from charmhelpers.core.hookenv import (
19 log,
20 metadata,
21 config,
22 )
23 from charms import layer
24 from charms.osm.k8s import get_service_ip
25 import urllib.parse
26 import yaml
27 import traceback
28
29
30 @hook("upgrade-charm")
31 @when("leadership.is_leader")
32 def upgrade():
33 clear_flag("nbi-k8s.configured")
34
35
36 @when("config.changed")
37 @when("leadership.is_leader")
38 def restart():
39 clear_flag("nbi-k8s.configured")
40
41
42 @when("config.changed.auth-backend")
43 def clear_keystone_ready():
44 clear_flag("keystone.ready")
45
46
47 @when_not("kafka.ready")
48 @when_not("nbi-k8s.configured")
49 def waiting_for_kafka():
50 layer.status.waiting("Waiting for kafka to be ready")
51
52
53 @when_not("mongo.ready")
54 @when_not("nbi-k8s.configured")
55 def waiting_for_mongo():
56 layer.status.waiting("Waiting for mongo to be ready")
57
58
59 @when_not("endpoint.prometheus.available")
60 @when_not("nbi-k8s.configured")
61 def waiting_for_prometheus():
62 layer.status.waiting("Waiting for prometheus to be ready")
63
64
65 @when_not("keystone.ready")
66 @when_not("nbi-k8s.configured")
67 def waiting_for_keystone():
68 auth_backend = config().get("auth-backend")
69 if auth_backend == "keystone":
70 layer.status.waiting("Waiting for Keystone to be ready")
71 else:
72 set_flag("keystone.ready")
73
74
75 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available", "keystone.ready")
76 @when_not("nbi-k8s.configured")
77 @when("leadership.is_leader")
78 def configure():
79 layer.status.maintenance("Configuring nbi container")
80 try:
81 kafka = endpoint_from_flag("kafka.ready")
82 mongo = endpoint_from_flag("mongo.ready")
83 prometheus = endpoint_from_flag("endpoint.prometheus.available")
84
85 if kafka and mongo and prometheus:
86 kafka_units = kafka.kafkas()
87 kafka_unit = kafka_units[0]
88
89 mongo_uri = mongo.connection_string()
90 log("Mongo URI: {}".format(mongo_uri))
91
92 prometheus_uri = prometheus.targets()[0]["targets"][0]
93
94 if (
95 mongo_uri
96 and kafka_unit["host"]
97 and kafka_unit["port"]
98 and prometheus_uri
99 ):
100 spec = yaml.load(
101 make_pod_spec(
102 kafka_unit["host"],
103 kafka_unit["port"],
104 mongo_uri,
105 prometheus_uri,
106 )
107 )
108
109 auth_backend = config().get("auth-backend")
110
111 if auth_backend == "keystone":
112 keystone = endpoint_from_flag("keystone.ready")
113 if keystone:
114 keystone_units = keystone.keystones()
115 keystone_unit = keystone_units[0]
116 if (
117 keystone_unit["host"]
118 and keystone_unit["port"]
119 and keystone_unit["user_domain_name"]
120 and keystone_unit["project_domain_name"]
121 and keystone_unit["username"]
122 and keystone_unit["password"]
123 and keystone_unit["service"]
124 ):
125 auth_keystone = {
126 "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
127 "OSMNBI_AUTHENTICATION_AUTH_URL": keystone_unit["host"],
128 "OSMNBI_AUTHENTICATION_AUTH_PORT": keystone_unit[
129 "port"
130 ],
131 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": keystone_unit[
132 "user_domain_name"
133 ],
134 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": keystone_unit[
135 "project_domain_name"
136 ],
137 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": keystone_unit[
138 "username"
139 ],
140 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": keystone_unit[
141 "password"
142 ],
143 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": keystone_unit[
144 "service"
145 ],
146 }
147 spec["containers"][0]["config"].update(auth_keystone)
148 elif auth_backend == "internal":
149 spec["containers"][0]["config"][
150 "OSMNBI_AUTHENTICATION_BACKEND"
151 ] = auth_backend
152 else:
153 layer.status.blocked(
154 "Unknown authentication method: {}".format(auth_backend)
155 )
156 raise
157 log("set pod spec:\n{}".format(spec))
158 pod_spec_set(spec)
159 set_flag("nbi-k8s.configured")
160 except Exception as e:
161 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
162 log("Error: {}".format(traceback.format_exc()))
163
164
165 @when("nbi-k8s.configured")
166 def set_nbi_active():
167 layer.status.active("ready")
168
169
170 @when("nbi-k8s.configured", "nbi.joined")
171 def send_config():
172 layer.status.maintenance("Sending NBI configuration")
173 try:
174 nbi = endpoint_from_flag("nbi.joined")
175 if nbi:
176 service_ip = get_service_ip("nbi")
177 if service_ip:
178 nbi.send_connection(service_ip, get_nbi_port())
179 clear_flag("nbi.joined")
180 except Exception as e:
181 log("Fail sending NBI configuration: {}".format(e))
182
183
184 def make_pod_spec(kafka_host, kafka_port, mongo_uri, prometheus_uri):
185 """Make pod specification for Kubernetes
186
187 Args:
188 kafka_host (str): Kafka hostname or IP
189 kafka_port (int): Kafka port
190 mongo_uri (str): Mongo URI
191 prometheus_uri (str): Prometheus URI
192 Returns:
193 pod_spec: Pod specification for Kubernetes
194 """
195
196 with open("reactive/spec_template.yaml") as spec_file:
197 pod_spec_template = spec_file.read()
198
199 md = metadata()
200 cfg = config()
201 prometheus_host, prometheus_port = parse_hostport(prometheus_uri)
202 data = {
203 "name": md.get("name"),
204 "docker_image": cfg.get("image"),
205 "mongo_uri": mongo_uri,
206 "kafka_host": "{}".format(kafka_host),
207 "kafka_port": "{}".format(kafka_port),
208 "prometheus_host": "{}".format(prometheus_host),
209 "prometheus_port": "{}".format(prometheus_port),
210 }
211 data.update(cfg)
212
213 return pod_spec_template % data
214
215
216 def parse_hostport(uri):
217 if "//" in uri:
218 uri = uri.split("//")[1]
219 result = urllib.parse.urlsplit("//" + uri)
220 return result.hostname, result.port
221
222
223 def get_nbi_port():
224 """Returns NBI port"""
225 cfg = config()
226 return cfg.get("advertised-port")