Prepare installer and pods for Rel TWELVE
[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 @when("leadership.is_leader")
50 def waiting_for_kafka():
51 layer.status.waiting("Waiting for kafka to be ready")
52
53
54 @when_not("mongo.ready")
55 @when_not("nbi-k8s.configured")
56 @when("leadership.is_leader")
57 def waiting_for_mongo():
58 layer.status.waiting("Waiting for mongo to be ready")
59
60
61 @when_not("endpoint.prometheus.available")
62 @when_not("nbi-k8s.configured")
63 @when("leadership.is_leader")
64 def waiting_for_prometheus():
65 layer.status.waiting("Waiting for prometheus to be ready")
66
67
68 @when_not("keystone.ready")
69 @when("leadership.is_leader")
70 @when_not("nbi-k8s.configured")
71 def waiting_for_keystone():
72 auth_backend = config().get("auth-backend")
73 if auth_backend == "keystone":
74 layer.status.waiting("Waiting for Keystone to be ready")
75 else:
76 set_flag("keystone.ready")
77
78
79 @when_not("keystone.ready")
80 @when("leadership.is_leader")
81 @when_not("nbi-k8s.configured")
82 def waiting_for_keystone():
83 auth_backend = config().get("auth-backend")
84 if auth_backend == "keystone":
85 layer.status.waiting("Waiting for Keystone to be ready")
86 else:
87 set_flag("keystone.ready")
88
89
90 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available", "keystone.ready")
91 @when_not("nbi-k8s.configured")
92 @when("leadership.is_leader")
93 def configure():
94 layer.status.maintenance("Configuring nbi container")
95 try:
96 kafka = endpoint_from_flag("kafka.ready")
97 mongo = endpoint_from_flag("mongo.ready")
98 prometheus = endpoint_from_flag("endpoint.prometheus.available")
99
100 if kafka and mongo and prometheus:
101 kafka_units = kafka.kafkas()
102 kafka_unit = kafka_units[0]
103
104 mongo_uri = mongo.connection_string()
105 log("Mongo URI: {}".format(mongo_uri))
106
107 prometheus_uri = prometheus.targets()[0]["targets"][0]
108
109 if (
110 mongo_uri
111 and kafka_unit["host"]
112 and kafka_unit["port"]
113 and prometheus_uri
114 ):
115 spec = yaml.load(
116 make_pod_spec(
117 kafka_unit["host"],
118 kafka_unit["port"],
119 mongo_uri,
120 prometheus_uri,
121 )
122 )
123
124 auth_backend = config().get("auth-backend")
125
126 if auth_backend == "keystone":
127 keystone = endpoint_from_flag("keystone.ready")
128 if keystone:
129 keystone_units = keystone.keystones()
130 keystone_unit = keystone_units[0]
131 if (
132 keystone_unit["host"]
133 and keystone_unit["port"]
134 and keystone_unit["user_domain_name"]
135 and keystone_unit["project_domain_name"]
136 and keystone_unit["username"]
137 and keystone_unit["password"]
138 and keystone_unit["service"]
139 ):
140 auth_keystone = {
141 "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
142 "OSMNBI_AUTHENTICATION_AUTH_URL": keystone_unit["host"],
143 "OSMNBI_AUTHENTICATION_AUTH_PORT": keystone_unit[
144 "port"
145 ],
146 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": keystone_unit[
147 "user_domain_name"
148 ],
149 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": keystone_unit[
150 "project_domain_name"
151 ],
152 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": keystone_unit[
153 "username"
154 ],
155 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": keystone_unit[
156 "password"
157 ],
158 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": keystone_unit[
159 "service"
160 ],
161 }
162 spec["containers"][0]["config"].update(auth_keystone)
163 elif auth_backend == "internal":
164 spec["containers"][0]["config"][
165 "OSMNBI_AUTHENTICATION_BACKEND"
166 ] = auth_backend
167 else:
168 layer.status.blocked(
169 "Unknown authentication method: {}".format(auth_backend)
170 )
171 raise
172 log("set pod spec:\n{}".format(spec))
173 pod_spec_set(spec)
174 set_flag("nbi-k8s.configured")
175 except Exception as e:
176 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
177 log("Error: {}".format(traceback.format_exc()))
178
179
180 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
181 @when_not("leadership.is_leader")
182 def non_leaders_active():
183 layer.status.active("ready")
184
185
186 @when("nbi-k8s.configured")
187 def set_nbi_active():
188 layer.status.active("ready")
189
190
191 @when("nbi-k8s.configured", "nbi.joined")
192 def send_config():
193 layer.status.maintenance("Sending NBI configuration")
194 try:
195 nbi = endpoint_from_flag("nbi.joined")
196 if nbi:
197 service_ip = get_service_ip("nbi")
198 if service_ip:
199 nbi.send_connection(service_ip, get_nbi_port())
200 clear_flag("nbi.joined")
201 except Exception as e:
202 log("Fail sending NBI configuration: {}".format(e))
203
204
205 def make_pod_spec(kafka_host, kafka_port, mongo_uri, prometheus_uri):
206 """Make pod specification for Kubernetes
207
208 Args:
209 kafka_host (str): Kafka hostname or IP
210 kafka_port (int): Kafka port
211 mongo_uri (str): Mongo URI
212 prometheus_uri (str): Prometheus URI
213 Returns:
214 pod_spec: Pod specification for Kubernetes
215 """
216
217 with open("reactive/spec_template.yaml") as spec_file:
218 pod_spec_template = spec_file.read()
219
220 md = metadata()
221 cfg = config()
222 prometheus_host, prometheus_port = parse_hostport(prometheus_uri)
223 data = {
224 "name": md.get("name"),
225 "mongo_uri": mongo_uri,
226 "kafka_host": "{}".format(kafka_host),
227 "kafka_port": "{}".format(kafka_port),
228 "prometheus_host": "{}".format(prometheus_host),
229 "prometheus_port": "{}".format(prometheus_port),
230 }
231 data.update(cfg)
232
233 return pod_spec_template % data
234
235
236 def parse_hostport(uri):
237 if "//" in uri:
238 uri = uri.split("//")[1]
239 result = urllib.parse.urlsplit("//" + uri)
240 return result.hostname, result.port
241
242
243 def get_nbi_port():
244 """Returns NBI port"""
245 cfg = config()
246 return cfg.get("advertised-port")