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 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 ContainerV3Builder
, FilesV3Builder
, PodSpecV3Builder
35 from opslib
.osm
.validator
import ModelValidator
, validator
37 logger
= logging
.getLogger(__name__
)
42 def _check_certificate_data(name
: str, content
: str):
43 if not name
or not content
:
44 raise ValueError("certificate name and content must be a non-empty string")
47 def _extract_certificates(certs_config
: str):
50 cert_list
= certs_config
.split(",")
51 for cert
in cert_list
:
52 name
, content
= cert
.split(":")
53 _check_certificate_data(name
, content
)
54 certificates
[name
] = content
58 def decode(content
: str):
59 return base64
.b64decode(content
.encode("utf-8")).decode("utf-8")
62 class ConfigModel(ModelValidator
):
64 database_commonkey
: str
69 certificates
: Optional
[str]
71 @validator("log_level")
72 def validate_log_level(cls
, v
):
73 if v
not in {"INFO", "DEBUG"}:
74 raise ValueError("value must be INFO or DEBUG")
77 @validator("certificates")
78 def validate_certificates(cls
, v
):
79 # Raises an exception if it cannot extract the certificates
80 _extract_certificates(v
)
84 def certificates_dict(cls
):
85 return _extract_certificates(cls
.certificates
) if cls
.certificates
else {}
88 class RoCharm(CharmedOsmBase
):
89 """GrafanaCharm Charm."""
91 def __init__(self
, *args
) -> NoReturn
:
92 """Prometheus Charm constructor."""
93 super().__init
__(*args
, oci_image
="image")
95 self
.kafka_client
= KafkaClient(self
, "kafka")
96 self
.framework
.observe(self
.on
["kafka"].relation_changed
, self
.configure_pod
)
97 self
.framework
.observe(self
.on
["kafka"].relation_broken
, self
.configure_pod
)
99 self
.mysql_client
= MysqlClient(self
, "mysql")
100 self
.framework
.observe(self
.on
["mysql"].relation_changed
, self
.configure_pod
)
101 self
.framework
.observe(self
.on
["mysql"].relation_broken
, self
.configure_pod
)
103 self
.mongodb_client
= MongoClient(self
, "mongodb")
104 self
.framework
.observe(self
.on
["mongodb"].relation_changed
, self
.configure_pod
)
105 self
.framework
.observe(self
.on
["mongodb"].relation_broken
, self
.configure_pod
)
107 self
.framework
.observe(self
.on
["ro"].relation_joined
, self
._publish
_ro
_info
)
109 def _publish_ro_info(self
, event
):
110 """Publishes RO information.
113 event (EventBase): RO relation event.
115 if self
.unit
.is_leader():
117 "host": self
.model
.app
.name
,
120 for k
, v
in rel_data
.items():
121 event
.relation
.data
[self
.app
][k
] = v
123 def _check_missing_dependencies(self
, config
: ConfigModel
):
124 missing_relations
= []
126 if config
.enable_ng_ro
:
127 if self
.kafka_client
.is_missing_data_in_unit():
128 missing_relations
.append("kafka")
129 if self
.mongodb_client
.is_missing_data_in_unit():
130 missing_relations
.append("mongodb")
132 if self
.mysql_client
.is_missing_data_in_unit():
133 missing_relations
.append("mysql")
134 if missing_relations
:
135 raise RelationsMissing(missing_relations
)
137 def _build_cert_files(
141 cert_files_builder
= FilesV3Builder()
142 for name
, content
in config
.certificates_dict
.items():
143 cert_files_builder
.add_file(name
, decode(content
), mode
=0o600)
144 return cert_files_builder
.build()
146 def build_pod_spec(self
, image_info
):
148 config
= ConfigModel(**dict(self
.config
))
150 self
._check
_missing
_dependencies
(config
)
151 # Create Builder for the PodSpec
152 pod_spec_builder
= PodSpecV3Builder()
154 container_builder
= ContainerV3Builder(self
.app
.name
, image_info
)
155 certs_files
= self
._build
_cert
_files
(config
)
157 container_builder
.add_volume_config("certs", "/certs", certs_files
)
158 container_builder
.add_port(name
=self
.app
.name
, port
=PORT
)
159 container_builder
.add_http_readiness_probe(
160 "/ro/" if config
.enable_ng_ro
else "/openmano/tenants",
162 initial_delay_seconds
=10,
167 container_builder
.add_http_liveness_probe(
168 "/ro/" if config
.enable_ng_ro
else "/openmano/tenants",
170 initial_delay_seconds
=600,
175 container_builder
.add_envs(
177 "OSMRO_LOG_LEVEL": config
.log_level
,
180 if config
.enable_ng_ro
:
181 container_builder
.add_envs(
183 "OSMRO_MESSAGE_DRIVER": "kafka",
184 "OSMRO_MESSAGE_HOST": self
.kafka_client
.host
,
185 "OSMRO_MESSAGE_PORT": self
.kafka_client
.port
,
186 # MongoDB configuration
187 "OSMRO_DATABASE_DRIVER": "mongo",
188 "OSMRO_DATABASE_URI": self
.mongodb_client
.connection_string
,
189 "OSMRO_DATABASE_COMMONKEY": config
.database_commonkey
,
194 container_builder
.add_envs(
196 "RO_DB_HOST": self
.mysql_client
.host
,
197 "RO_DB_OVIM_HOST": self
.mysql_client
.host
,
198 "RO_DB_PORT": self
.mysql_client
.port
,
199 "RO_DB_OVIM_PORT": self
.mysql_client
.port
,
200 "RO_DB_USER": self
.mysql_client
.user
,
201 "RO_DB_OVIM_USER": self
.mysql_client
.user
,
202 "RO_DB_PASSWORD": self
.mysql_client
.password
,
203 "RO_DB_OVIM_PASSWORD": self
.mysql_client
.password
,
204 "RO_DB_ROOT_PASSWORD": self
.mysql_client
.root_password
,
205 "RO_DB_OVIM_ROOT_PASSWORD": self
.mysql_client
.root_password
,
206 "RO_DB_NAME": config
.ro_database
,
207 "RO_DB_OVIM_NAME": config
.vim_database
,
208 "OPENMANO_TENANT": config
.openmano_tenant
,
211 container
= container_builder
.build()
212 # Add container to pod spec
213 pod_spec_builder
.add_container(container
)
214 return pod_spec_builder
.build()
217 if __name__
== "__main__":