2 # Copyright 2021 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 # pylint: disable=E0213
27 from typing
import Dict
, NoReturn
, Optional
29 from ops
.main
import main
30 from opslib
.osm
.charm
import CharmedOsmBase
, RelationsMissing
31 from opslib
.osm
.interfaces
.kafka
import KafkaClient
32 from opslib
.osm
.interfaces
.mongo
import MongoClient
33 from opslib
.osm
.interfaces
.mysql
import MysqlClient
34 from opslib
.osm
.pod
import (
40 from opslib
.osm
.validator
import ModelValidator
, validator
42 logger
= logging
.getLogger(__name__
)
47 def _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")
52 def _extract_certificates(certs_config
: str):
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
63 def decode(content
: str):
64 return base64
.b64decode(content
.encode("utf-8")).decode("utf-8")
67 class ConfigModel(ModelValidator
):
69 database_commonkey
: str
70 mongodb_uri
: Optional
[str]
72 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]
80 certificates
: Optional
[str]
81 image_pull_policy
: str
83 security_context
: bool
85 @validator("log_level")
86 def validate_log_level(cls
, v
):
87 if v
not in {"INFO", "DEBUG"}:
88 raise ValueError("value must be INFO or DEBUG")
91 @validator("certificates")
92 def validate_certificates(cls
, v
):
93 # Raises an exception if it cannot extract the certificates
94 _extract_certificates(v
)
97 @validator("mongodb_uri")
98 def validate_mongodb_uri(cls
, v
):
99 if v
and not v
.startswith("mongodb://"):
100 raise ValueError("mongodb_uri is not properly formed")
103 @validator("mysql_port")
104 def validate_mysql_port(cls
, v
):
105 if v
and (v
<= 0 or v
>= 65535):
106 raise ValueError("Mysql port out of range")
109 @validator("image_pull_policy")
110 def validate_image_pull_policy(cls
, v
):
113 "ifnotpresent": "IfNotPresent",
117 if v
not in values
.keys():
118 raise ValueError("value must be always, ifnotpresent or never")
122 def certificates_dict(cls
):
123 return _extract_certificates(cls
.certificates
) if cls
.certificates
else {}
126 class RoCharm(CharmedOsmBase
):
127 """GrafanaCharm Charm."""
129 def __init__(self
, *args
) -> NoReturn
:
130 """Prometheus Charm constructor."""
134 vscode_workspace
=VSCODE_WORKSPACE
,
136 if self
.config
.get("debug_mode"):
137 self
.enable_debug_mode(
138 pubkey
=self
.config
.get("debug_pubkey"),
141 "hostpath": self
.config
.get("debug_common_local_path"),
142 "container-path": "/usr/lib/python3/dist-packages/osm_common",
144 **_get_ro_host_paths(self
.config
.get("debug_ro_local_path")),
147 self
.kafka_client
= KafkaClient(self
, "kafka")
148 self
.framework
.observe(self
.on
["kafka"].relation_changed
, self
.configure_pod
)
149 self
.framework
.observe(self
.on
["kafka"].relation_broken
, self
.configure_pod
)
151 self
.mysql_client
= MysqlClient(self
, "mysql")
152 self
.framework
.observe(self
.on
["mysql"].relation_changed
, self
.configure_pod
)
153 self
.framework
.observe(self
.on
["mysql"].relation_broken
, self
.configure_pod
)
155 self
.mongodb_client
= MongoClient(self
, "mongodb")
156 self
.framework
.observe(self
.on
["mongodb"].relation_changed
, self
.configure_pod
)
157 self
.framework
.observe(self
.on
["mongodb"].relation_broken
, self
.configure_pod
)
159 self
.framework
.observe(self
.on
["ro"].relation_joined
, self
._publish
_ro
_info
)
161 def _publish_ro_info(self
, event
):
162 """Publishes RO information.
165 event (EventBase): RO relation event.
167 if self
.unit
.is_leader():
169 "host": self
.model
.app
.name
,
172 for k
, v
in rel_data
.items():
173 event
.relation
.data
[self
.app
][k
] = v
175 def _check_missing_dependencies(self
, config
: ConfigModel
):
176 missing_relations
= []
178 if config
.enable_ng_ro
:
180 self
.kafka_client
.is_missing_data_in_unit()
181 and self
.kafka_client
.is_missing_data_in_app()
183 missing_relations
.append("kafka")
184 if not config
.mongodb_uri
and self
.mongodb_client
.is_missing_data_in_unit():
185 missing_relations
.append("mongodb")
187 if not config
.mysql_host
and self
.mysql_client
.is_missing_data_in_unit():
188 missing_relations
.append("mysql")
189 if missing_relations
:
190 raise RelationsMissing(missing_relations
)
192 def _validate_mysql_config(self
, config
: ConfigModel
):
194 if not config
.mysql_user
:
195 invalid_values
.append("Mysql user is empty")
196 if not config
.mysql_password
:
197 invalid_values
.append("Mysql password is empty")
198 if not config
.mysql_root_password
:
199 invalid_values
.append("Mysql root password empty")
202 raise ValueError("Invalid values: " + ", ".join(invalid_values
))
204 def _build_cert_files(
208 cert_files_builder
= FilesV3Builder()
209 for name
, content
in config
.certificates_dict
.items():
210 cert_files_builder
.add_file(name
, decode(content
), mode
=0o600)
211 return cert_files_builder
.build()
213 def build_pod_spec(self
, image_info
):
215 config
= ConfigModel(**dict(self
.config
))
217 if config
.enable_ng_ro
:
218 if config
.mongodb_uri
and not self
.mongodb_client
.is_missing_data_in_unit():
220 "Mongodb data cannot be provided via config and relation"
223 if config
.mysql_host
and not self
.mysql_client
.is_missing_data_in_unit():
224 raise Exception("Mysql data cannot be provided via config and relation")
226 if config
.mysql_host
:
227 self
._validate
_mysql
_config
(config
)
230 self
._check
_missing
_dependencies
(config
)
232 security_context_enabled
= (
233 config
.security_context
if not config
.debug_mode
else False
236 # Create Builder for the PodSpec
237 pod_spec_builder
= PodSpecV3Builder(
238 enable_security_context
=security_context_enabled
242 container_builder
= ContainerV3Builder(
245 config
.image_pull_policy
,
246 run_as_non_root
=security_context_enabled
,
248 certs_files
= self
._build
_cert
_files
(config
)
251 container_builder
.add_volume_config("certs", "/certs", certs_files
)
253 container_builder
.add_port(name
=self
.app
.name
, port
=PORT
)
254 container_builder
.add_http_readiness_probe(
255 "/ro/" if config
.enable_ng_ro
else "/openmano/tenants",
257 initial_delay_seconds
=10,
262 container_builder
.add_http_liveness_probe(
263 "/ro/" if config
.enable_ng_ro
else "/openmano/tenants",
265 initial_delay_seconds
=600,
270 container_builder
.add_envs(
272 "OSMRO_LOG_LEVEL": config
.log_level
,
276 if config
.enable_ng_ro
:
277 # Add secrets to the pod
278 mongodb_secret_name
= f
"{self.app.name}-mongodb-secret"
279 pod_spec_builder
.add_secret(
282 "uri": config
.mongodb_uri
or self
.mongodb_client
.connection_string
,
283 "commonkey": config
.database_commonkey
,
286 container_builder
.add_envs(
288 "OSMRO_MESSAGE_DRIVER": "kafka",
289 "OSMRO_MESSAGE_HOST": self
.kafka_client
.host
,
290 "OSMRO_MESSAGE_PORT": self
.kafka_client
.port
,
291 # MongoDB configuration
292 "OSMRO_DATABASE_DRIVER": "mongo",
295 container_builder
.add_secret_envs(
296 secret_name
=mongodb_secret_name
,
298 "OSMRO_DATABASE_URI": "uri",
299 "OSMRO_DATABASE_COMMONKEY": "commonkey",
302 restart_policy
= PodRestartPolicy()
303 restart_policy
.add_secrets(secret_names
=(mongodb_secret_name
,))
304 pod_spec_builder
.set_restart_policy(restart_policy
)
307 container_builder
.add_envs(
309 "RO_DB_HOST": config
.mysql_host
or self
.mysql_client
.host
,
310 "RO_DB_OVIM_HOST": config
.mysql_host
or self
.mysql_client
.host
,
311 "RO_DB_PORT": config
.mysql_port
or self
.mysql_client
.port
,
312 "RO_DB_OVIM_PORT": config
.mysql_port
or self
.mysql_client
.port
,
313 "RO_DB_USER": config
.mysql_user
or self
.mysql_client
.user
,
314 "RO_DB_OVIM_USER": config
.mysql_user
or self
.mysql_client
.user
,
315 "RO_DB_PASSWORD": config
.mysql_password
316 or self
.mysql_client
.password
,
317 "RO_DB_OVIM_PASSWORD": config
.mysql_password
318 or self
.mysql_client
.password
,
319 "RO_DB_ROOT_PASSWORD": config
.mysql_root_password
320 or self
.mysql_client
.root_password
,
321 "RO_DB_OVIM_ROOT_PASSWORD": config
.mysql_root_password
322 or self
.mysql_client
.root_password
,
323 "RO_DB_NAME": config
.ro_database
,
324 "RO_DB_OVIM_NAME": config
.vim_database
,
325 "OPENMANO_TENANT": config
.openmano_tenant
,
328 container
= container_builder
.build()
330 # Add container to pod spec
331 pod_spec_builder
.add_container(container
)
333 return pod_spec_builder
.build()
338 {"path": "/usr/lib/python3/dist-packages/osm_ng_ro"},
339 {"path": "/usr/lib/python3/dist-packages/osm_common"},
340 {"path": "/usr/lib/python3/dist-packages/osm_ro_plugin"},
341 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_arista_cloudvision"},
342 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dpb"},
343 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dynpac"},
344 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_floodlightof"},
345 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_ietfl2vpn"},
346 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_juniper_contrail"},
347 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_odlof"},
348 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onos_vpls"},
349 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onosof"},
350 {"path": "/usr/lib/python3/dist-packages/osm_rovim_aws"},
351 {"path": "/usr/lib/python3/dist-packages/osm_rovim_azure"},
352 {"path": "/usr/lib/python3/dist-packages/osm_rovim_gcp"},
353 {"path": "/usr/lib/python3/dist-packages/osm_rovim_fos"},
354 # {"path": "/usr/lib/python3/dist-packages/osm_rovim_opennebula"},
355 {"path": "/usr/lib/python3/dist-packages/osm_rovim_openstack"},
356 {"path": "/usr/lib/python3/dist-packages/osm_rovim_openvim"},
357 {"path": "/usr/lib/python3/dist-packages/osm_rovim_vmware"},
362 "module": "osm_ng_ro.ro_main",
375 def _get_ro_host_paths(ro_host_path
: str) -> Dict
:
376 """Get RO host paths"""
380 "hostpath": f
"{ro_host_path}/NG-RO",
381 "container-path": "/usr/lib/python3/dist-packages/osm_ng_ro",
384 "hostpath": f
"{ro_host_path}/RO-plugin",
385 "container-path": "/usr/lib/python3/dist-packages/osm_ro_plugin",
387 "RO-SDN-arista_cloudvision": {
388 "hostpath": f
"{ro_host_path}/RO-SDN-arista_cloudvision",
389 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_arista_cloudvision",
392 "hostpath": f
"{ro_host_path}/RO-SDN-dpb",
393 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_dpb",
396 "hostpath": f
"{ro_host_path}/RO-SDN-dynpac",
397 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_dynpac",
399 "RO-SDN-floodlight_openflow": {
400 "hostpath": f
"{ro_host_path}/RO-SDN-floodlight_openflow",
401 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_floodlightof",
403 "RO-SDN-ietfl2vpn": {
404 "hostpath": f
"{ro_host_path}/RO-SDN-ietfl2vpn",
405 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_ietfl2vpn",
407 "RO-SDN-juniper_contrail": {
408 "hostpath": f
"{ro_host_path}/RO-SDN-juniper_contrail",
409 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_juniper_contrail",
411 "RO-SDN-odl_openflow": {
412 "hostpath": f
"{ro_host_path}/RO-SDN-odl_openflow",
413 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_odlof",
415 "RO-SDN-onos_openflow": {
416 "hostpath": f
"{ro_host_path}/RO-SDN-onos_openflow",
417 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_onosof",
419 "RO-SDN-onos_vpls": {
420 "hostpath": f
"{ro_host_path}/RO-SDN-onos_vpls",
421 "container-path": "/usr/lib/python3/dist-packages/osm_rosdn_onos_vpls",
424 "hostpath": f
"{ro_host_path}/RO-VIM-aws",
425 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_aws",
428 "hostpath": f
"{ro_host_path}/RO-VIM-azure",
429 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_azure",
432 "hostpath": f
"{ro_host_path}/RO-VIM-gcp",
433 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_gcp",
436 "hostpath": f
"{ro_host_path}/RO-VIM-fos",
437 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_fos",
439 "RO-VIM-opennebula": {
440 "hostpath": f
"{ro_host_path}/RO-VIM-opennebula",
441 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_opennebula",
443 "RO-VIM-openstack": {
444 "hostpath": f
"{ro_host_path}/RO-VIM-openstack",
445 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_openstack",
448 "hostpath": f
"{ro_host_path}/RO-VIM-openvim",
449 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_openvim",
452 "hostpath": f
"{ro_host_path}/RO-VIM-vmware",
453 "container-path": "/usr/lib/python3/dist-packages/osm_rovim_vmware",
461 if __name__
== "__main__":