blob: 028dc0a4f0c1afd6cd7758fa2c88952fa35cacab [file] [log] [blame]
sousaeduccfacbb2020-11-04 21:44:01 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaeduccfacbb2020-11-04 21:44:01 +00003#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# 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, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
16# For those usages not covered by the Apache License, Version 2.0 please
17# contact: legal@canonical.com
18#
19# To get in touch with the maintainers, please contact:
20# osm-charmers@lists.launchpad.net
21##
22
David Garcia49379ce2021-02-24 13:48:22 +010023# pylint: disable=E0213
24
David Garcia5d1ec6e2021-03-25 15:04:52 +010025import base64
sousaeduccfacbb2020-11-04 21:44:01 +000026import logging
David Garciacafe31e2021-11-18 16:45:05 +010027from typing import Dict, NoReturn, Optional
sousaeduccfacbb2020-11-04 21:44:01 +000028
David Garcia4a0db7c2022-02-21 11:48:11 +010029from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
sousaeduccfacbb2020-11-04 21:44:01 +000030from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010031from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garciac753dc52021-03-17 15:28:47 +010032from opslib.osm.interfaces.mongo import MongoClient
33from opslib.osm.interfaces.mysql import MysqlClient
David Garcia141d9352021-09-08 17:48:40 +020034from opslib.osm.pod import (
35 ContainerV3Builder,
36 FilesV3Builder,
37 PodRestartPolicy,
38 PodSpecV3Builder,
39)
David Garciac753dc52021-03-17 15:28:47 +010040from opslib.osm.validator import ModelValidator, validator
David Garcia49379ce2021-02-24 13:48:22 +010041
sousaeduccfacbb2020-11-04 21:44:01 +000042logger = logging.getLogger(__name__)
43
David Garcia49379ce2021-02-24 13:48:22 +010044PORT = 9090
sousaeduccfacbb2020-11-04 21:44:01 +000045
46
David Garcia5d1ec6e2021-03-25 15:04:52 +010047def _check_certificate_data(name: str, content: str):
48 if not name or not content:
49 raise ValueError("certificate name and content must be a non-empty string")
50
51
52def _extract_certificates(certs_config: str):
53 certificates = {}
54 if certs_config:
55 cert_list = certs_config.split(",")
56 for cert in cert_list:
57 name, content = cert.split(":")
58 _check_certificate_data(name, content)
59 certificates[name] = content
60 return certificates
61
62
63def decode(content: str):
64 return base64.b64decode(content.encode("utf-8")).decode("utf-8")
65
66
David Garcia49379ce2021-02-24 13:48:22 +010067class ConfigModel(ModelValidator):
68 enable_ng_ro: bool
69 database_commonkey: str
sousaedu996a5602021-05-03 00:22:43 +020070 mongodb_uri: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010071 log_level: str
sousaedu996a5602021-05-03 00:22:43 +020072 mysql_host: Optional[str]
73 mysql_port: Optional[int]
74 mysql_user: Optional[str]
75 mysql_password: Optional[str]
76 mysql_root_password: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010077 vim_database: str
78 ro_database: str
79 openmano_tenant: str
David Garcia5d1ec6e2021-03-25 15:04:52 +010080 certificates: Optional[str]
sousaedu0dc25b32021-08-30 16:33:33 +010081 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010082 debug_mode: bool
83 security_context: bool
aticigbd324852022-06-30 03:50:07 +030084 period_refresh_active: Optional[int]
David Garcia49379ce2021-02-24 13:48:22 +010085
86 @validator("log_level")
87 def validate_log_level(cls, v):
88 if v not in {"INFO", "DEBUG"}:
89 raise ValueError("value must be INFO or DEBUG")
90 return v
sousaeduccfacbb2020-11-04 21:44:01 +000091
David Garcia5d1ec6e2021-03-25 15:04:52 +010092 @validator("certificates")
93 def validate_certificates(cls, v):
94 # Raises an exception if it cannot extract the certificates
95 _extract_certificates(v)
96 return v
97
sousaedu996a5602021-05-03 00:22:43 +020098 @validator("mongodb_uri")
99 def validate_mongodb_uri(cls, v):
100 if v and not v.startswith("mongodb://"):
101 raise ValueError("mongodb_uri is not properly formed")
102 return v
103
104 @validator("mysql_port")
105 def validate_mysql_port(cls, v):
106 if v and (v <= 0 or v >= 65535):
107 raise ValueError("Mysql port out of range")
108 return v
109
sousaedu3ddbbd12021-08-24 19:57:24 +0100110 @validator("image_pull_policy")
111 def validate_image_pull_policy(cls, v):
112 values = {
113 "always": "Always",
114 "ifnotpresent": "IfNotPresent",
115 "never": "Never",
116 }
117 v = v.lower()
118 if v not in values.keys():
119 raise ValueError("value must be always, ifnotpresent or never")
120 return values[v]
121
David Garcia5d1ec6e2021-03-25 15:04:52 +0100122 @property
123 def certificates_dict(cls):
124 return _extract_certificates(cls.certificates) if cls.certificates else {}
125
aticigbd324852022-06-30 03:50:07 +0300126 @validator("period_refresh_active")
127 def validate_vim_refresh_period(cls, v):
128 if v and v < 60 and v != -1:
129 raise ValueError(
130 "Refresh Period is too tight, insert >= 60 seconds or disable using -1"
131 )
132 return v
133
sousaeduccfacbb2020-11-04 21:44:01 +0000134
David Garcia49379ce2021-02-24 13:48:22 +0100135class RoCharm(CharmedOsmBase):
136 """GrafanaCharm Charm."""
sousaeduccfacbb2020-11-04 21:44:01 +0000137
David Garcia4a0db7c2022-02-21 11:48:11 +0100138 on = KafkaEvents()
139
sousaeduccfacbb2020-11-04 21:44:01 +0000140 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +0100141 """Prometheus Charm constructor."""
David Garciad680be42021-08-17 11:03:55 +0200142 super().__init__(
143 *args,
144 oci_image="image",
David Garciad680be42021-08-17 11:03:55 +0200145 vscode_workspace=VSCODE_WORKSPACE,
146 )
David Garciacafe31e2021-11-18 16:45:05 +0100147 if self.config.get("debug_mode"):
148 self.enable_debug_mode(
149 pubkey=self.config.get("debug_pubkey"),
150 hostpaths={
151 "osm_common": {
152 "hostpath": self.config.get("debug_common_local_path"),
153 "container-path": "/usr/lib/python3/dist-packages/osm_common",
154 },
155 **_get_ro_host_paths(self.config.get("debug_ro_local_path")),
156 },
157 )
David Garcia4a0db7c2022-02-21 11:48:11 +0100158 self.kafka = KafkaRequires(self)
159 self.framework.observe(self.on.kafka_available, self.configure_pod)
160 self.framework.observe(self.on.kafka_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +0000161
David Garcia49379ce2021-02-24 13:48:22 +0100162 self.mysql_client = MysqlClient(self, "mysql")
163 self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod)
164 self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +0000165
David Garcia49379ce2021-02-24 13:48:22 +0100166 self.mongodb_client = MongoClient(self, "mongodb")
167 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
168 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +0000169
David Garcia49379ce2021-02-24 13:48:22 +0100170 self.framework.observe(self.on["ro"].relation_joined, self._publish_ro_info)
sousaeduccfacbb2020-11-04 21:44:01 +0000171
David Garcia49379ce2021-02-24 13:48:22 +0100172 def _publish_ro_info(self, event):
sousaeduccfacbb2020-11-04 21:44:01 +0000173 """Publishes RO information.
174
175 Args:
176 event (EventBase): RO relation event.
177 """
178 if self.unit.is_leader():
179 rel_data = {
180 "host": self.model.app.name,
David Garcia49379ce2021-02-24 13:48:22 +0100181 "port": str(PORT),
sousaeduccfacbb2020-11-04 21:44:01 +0000182 }
183 for k, v in rel_data.items():
184 event.relation.data[self.app][k] = v
185
David Garcia49379ce2021-02-24 13:48:22 +0100186 def _check_missing_dependencies(self, config: ConfigModel):
187 missing_relations = []
188
189 if config.enable_ng_ro:
David Garcia4a0db7c2022-02-21 11:48:11 +0100190 if not self.kafka.host or not self.kafka.port:
David Garcia49379ce2021-02-24 13:48:22 +0100191 missing_relations.append("kafka")
sousaedu996a5602021-05-03 00:22:43 +0200192 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100193 missing_relations.append("mongodb")
sousaeduccfacbb2020-11-04 21:44:01 +0000194 else:
sousaedu996a5602021-05-03 00:22:43 +0200195 if not config.mysql_host and self.mysql_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100196 missing_relations.append("mysql")
197 if missing_relations:
198 raise RelationsMissing(missing_relations)
sousaeduccfacbb2020-11-04 21:44:01 +0000199
sousaedu996a5602021-05-03 00:22:43 +0200200 def _validate_mysql_config(self, config: ConfigModel):
201 invalid_values = []
202 if not config.mysql_user:
203 invalid_values.append("Mysql user is empty")
204 if not config.mysql_password:
205 invalid_values.append("Mysql password is empty")
206 if not config.mysql_root_password:
207 invalid_values.append("Mysql root password empty")
208
209 if invalid_values:
210 raise ValueError("Invalid values: " + ", ".join(invalid_values))
211
David Garcia5d1ec6e2021-03-25 15:04:52 +0100212 def _build_cert_files(
213 self,
214 config: ConfigModel,
215 ):
216 cert_files_builder = FilesV3Builder()
217 for name, content in config.certificates_dict.items():
218 cert_files_builder.add_file(name, decode(content), mode=0o600)
219 return cert_files_builder.build()
220
David Garcia49379ce2021-02-24 13:48:22 +0100221 def build_pod_spec(self, image_info):
222 # Validate config
223 config = ConfigModel(**dict(self.config))
sousaedu996a5602021-05-03 00:22:43 +0200224
225 if config.enable_ng_ro:
226 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
227 raise Exception(
228 "Mongodb data cannot be provided via config and relation"
229 )
230 else:
231 if config.mysql_host and not self.mysql_client.is_missing_data_in_unit():
232 raise Exception("Mysql data cannot be provided via config and relation")
233
234 if config.mysql_host:
235 self._validate_mysql_config(config)
236
David Garcia49379ce2021-02-24 13:48:22 +0100237 # Check relations
238 self._check_missing_dependencies(config)
sousaedu996a5602021-05-03 00:22:43 +0200239
sousaedu540d9372021-09-29 01:53:30 +0100240 security_context_enabled = (
241 config.security_context if not config.debug_mode else False
242 )
243
David Garcia49379ce2021-02-24 13:48:22 +0100244 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100245 pod_spec_builder = PodSpecV3Builder(
246 enable_security_context=security_context_enabled
247 )
sousaedu996a5602021-05-03 00:22:43 +0200248
David Garcia49379ce2021-02-24 13:48:22 +0100249 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100250 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100251 self.app.name,
252 image_info,
253 config.image_pull_policy,
254 run_as_non_root=security_context_enabled,
sousaedu3ddbbd12021-08-24 19:57:24 +0100255 )
David Garcia5d1ec6e2021-03-25 15:04:52 +0100256 certs_files = self._build_cert_files(config)
sousaedu996a5602021-05-03 00:22:43 +0200257
David Garcia5d1ec6e2021-03-25 15:04:52 +0100258 if certs_files:
259 container_builder.add_volume_config("certs", "/certs", certs_files)
sousaedu996a5602021-05-03 00:22:43 +0200260
David Garcia49379ce2021-02-24 13:48:22 +0100261 container_builder.add_port(name=self.app.name, port=PORT)
262 container_builder.add_http_readiness_probe(
263 "/ro/" if config.enable_ng_ro else "/openmano/tenants",
264 PORT,
265 initial_delay_seconds=10,
266 period_seconds=10,
267 timeout_seconds=5,
268 failure_threshold=3,
269 )
270 container_builder.add_http_liveness_probe(
271 "/ro/" if config.enable_ng_ro else "/openmano/tenants",
272 PORT,
273 initial_delay_seconds=600,
274 period_seconds=10,
275 timeout_seconds=5,
276 failure_threshold=3,
277 )
278 container_builder.add_envs(
279 {
280 "OSMRO_LOG_LEVEL": config.log_level,
281 }
282 )
aticigbd324852022-06-30 03:50:07 +0300283 if config.period_refresh_active:
284 container_builder.add_envs(
285 {
286 "OSMRO_PERIOD_REFRESH_ACTIVE": config.period_refresh_active,
287 }
288 )
David Garcia49379ce2021-02-24 13:48:22 +0100289 if config.enable_ng_ro:
David Garcia141d9352021-09-08 17:48:40 +0200290 # Add secrets to the pod
291 mongodb_secret_name = f"{self.app.name}-mongodb-secret"
292 pod_spec_builder.add_secret(
293 mongodb_secret_name,
294 {
295 "uri": config.mongodb_uri or self.mongodb_client.connection_string,
296 "commonkey": config.database_commonkey,
297 },
298 )
David Garcia49379ce2021-02-24 13:48:22 +0100299 container_builder.add_envs(
300 {
301 "OSMRO_MESSAGE_DRIVER": "kafka",
David Garcia4a0db7c2022-02-21 11:48:11 +0100302 "OSMRO_MESSAGE_HOST": self.kafka.host,
303 "OSMRO_MESSAGE_PORT": self.kafka.port,
David Garcia49379ce2021-02-24 13:48:22 +0100304 # MongoDB configuration
305 "OSMRO_DATABASE_DRIVER": "mongo",
David Garcia49379ce2021-02-24 13:48:22 +0100306 }
sousaeduccfacbb2020-11-04 21:44:01 +0000307 )
David Garcia141d9352021-09-08 17:48:40 +0200308 container_builder.add_secret_envs(
309 secret_name=mongodb_secret_name,
310 envs={
311 "OSMRO_DATABASE_URI": "uri",
312 "OSMRO_DATABASE_COMMONKEY": "commonkey",
313 },
314 )
315 restart_policy = PodRestartPolicy()
316 restart_policy.add_secrets(secret_names=(mongodb_secret_name,))
317 pod_spec_builder.set_restart_policy(restart_policy)
sousaeduccfacbb2020-11-04 21:44:01 +0000318
David Garcia49379ce2021-02-24 13:48:22 +0100319 else:
320 container_builder.add_envs(
321 {
sousaedu996a5602021-05-03 00:22:43 +0200322 "RO_DB_HOST": config.mysql_host or self.mysql_client.host,
323 "RO_DB_OVIM_HOST": config.mysql_host or self.mysql_client.host,
324 "RO_DB_PORT": config.mysql_port or self.mysql_client.port,
325 "RO_DB_OVIM_PORT": config.mysql_port or self.mysql_client.port,
326 "RO_DB_USER": config.mysql_user or self.mysql_client.user,
327 "RO_DB_OVIM_USER": config.mysql_user or self.mysql_client.user,
328 "RO_DB_PASSWORD": config.mysql_password
329 or self.mysql_client.password,
330 "RO_DB_OVIM_PASSWORD": config.mysql_password
331 or self.mysql_client.password,
332 "RO_DB_ROOT_PASSWORD": config.mysql_root_password
333 or self.mysql_client.root_password,
334 "RO_DB_OVIM_ROOT_PASSWORD": config.mysql_root_password
335 or self.mysql_client.root_password,
David Garcia49379ce2021-02-24 13:48:22 +0100336 "RO_DB_NAME": config.ro_database,
337 "RO_DB_OVIM_NAME": config.vim_database,
338 "OPENMANO_TENANT": config.openmano_tenant,
339 }
340 )
341 container = container_builder.build()
sousaedu996a5602021-05-03 00:22:43 +0200342
David Garcia49379ce2021-02-24 13:48:22 +0100343 # Add container to pod spec
344 pod_spec_builder.add_container(container)
sousaedu996a5602021-05-03 00:22:43 +0200345
David Garcia49379ce2021-02-24 13:48:22 +0100346 return pod_spec_builder.build()
sousaeduccfacbb2020-11-04 21:44:01 +0000347
348
David Garciad680be42021-08-17 11:03:55 +0200349VSCODE_WORKSPACE = {
350 "folders": [
351 {"path": "/usr/lib/python3/dist-packages/osm_ng_ro"},
352 {"path": "/usr/lib/python3/dist-packages/osm_common"},
353 {"path": "/usr/lib/python3/dist-packages/osm_ro_plugin"},
354 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_arista_cloudvision"},
355 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dpb"},
356 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dynpac"},
357 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_floodlightof"},
358 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_ietfl2vpn"},
359 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_juniper_contrail"},
360 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_odlof"},
361 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onos_vpls"},
362 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onosof"},
363 {"path": "/usr/lib/python3/dist-packages/osm_rovim_aws"},
364 {"path": "/usr/lib/python3/dist-packages/osm_rovim_azure"},
garciadeblasf854a612021-11-09 23:30:47 +0100365 {"path": "/usr/lib/python3/dist-packages/osm_rovim_gcp"},
David Garciad680be42021-08-17 11:03:55 +0200366 {"path": "/usr/lib/python3/dist-packages/osm_rovim_openstack"},
367 {"path": "/usr/lib/python3/dist-packages/osm_rovim_openvim"},
368 {"path": "/usr/lib/python3/dist-packages/osm_rovim_vmware"},
369 ],
370 "launch": {
371 "configurations": [
372 {
373 "module": "osm_ng_ro.ro_main",
374 "name": "NG RO",
375 "request": "launch",
376 "type": "python",
377 "justMyCode": False,
378 }
379 ],
380 "version": "0.2.0",
381 },
382 "settings": {},
383}
384
David Garciacafe31e2021-11-18 16:45:05 +0100385
386def _get_ro_host_paths(ro_host_path: str) -> Dict:
387 """Get RO host paths"""
388 return (
389 {
390 "NG-RO": {
391 "hostpath": f"{ro_host_path}/NG-RO",
392 "container-path": "/usr/lib/python3/dist-packages/osm_ng_ro",
393 },
394 "RO-plugin": {
395 "hostpath": f"{ro_host_path}/RO-plugin",
396 "container-path": "/usr/lib/python3/dist-packages/osm_ro_plugin",
397 },
398 "RO-SDN-arista_cloudvision": {
399 "hostpath": f"{ro_host_path}/RO-SDN-arista_cloudvision",
400 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_arista_cloudvision",
401 },
402 "RO-SDN-dpb": {
403 "hostpath": f"{ro_host_path}/RO-SDN-dpb",
404 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_dpb",
405 },
406 "RO-SDN-dynpac": {
407 "hostpath": f"{ro_host_path}/RO-SDN-dynpac",
408 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_dynpac",
409 },
410 "RO-SDN-floodlight_openflow": {
411 "hostpath": f"{ro_host_path}/RO-SDN-floodlight_openflow",
412 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_floodlightof",
413 },
414 "RO-SDN-ietfl2vpn": {
415 "hostpath": f"{ro_host_path}/RO-SDN-ietfl2vpn",
416 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_ietfl2vpn",
417 },
418 "RO-SDN-juniper_contrail": {
419 "hostpath": f"{ro_host_path}/RO-SDN-juniper_contrail",
420 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_juniper_contrail",
421 },
422 "RO-SDN-odl_openflow": {
423 "hostpath": f"{ro_host_path}/RO-SDN-odl_openflow",
424 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_odlof",
425 },
426 "RO-SDN-onos_openflow": {
427 "hostpath": f"{ro_host_path}/RO-SDN-onos_openflow",
428 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_onosof",
429 },
430 "RO-SDN-onos_vpls": {
431 "hostpath": f"{ro_host_path}/RO-SDN-onos_vpls",
432 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_onos_vpls",
433 },
434 "RO-VIM-aws": {
435 "hostpath": f"{ro_host_path}/RO-VIM-aws",
436 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_aws",
437 },
438 "RO-VIM-azure": {
439 "hostpath": f"{ro_host_path}/RO-VIM-azure",
440 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_azure",
441 },
442 "RO-VIM-gcp": {
443 "hostpath": f"{ro_host_path}/RO-VIM-gcp",
444 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_gcp",
445 },
David Garciacafe31e2021-11-18 16:45:05 +0100446 "RO-VIM-openstack": {
447 "hostpath": f"{ro_host_path}/RO-VIM-openstack",
448 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_openstack",
449 },
450 "RO-VIM-openvim": {
451 "hostpath": f"{ro_host_path}/RO-VIM-openvim",
452 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_openvim",
453 },
454 "RO-VIM-vmware": {
455 "hostpath": f"{ro_host_path}/RO-VIM-vmware",
456 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_vmware",
457 },
458 }
459 if ro_host_path
460 else {}
461 )
462
463
sousaeduccfacbb2020-11-04 21:44:01 +0000464if __name__ == "__main__":
465 main(RoCharm)