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
28 from typing
import NoReturn
, Optional
31 from charms
.kafka_k8s
.v0
.kafka
import KafkaEvents
, KafkaRequires
32 from ops
.main
import main
33 from opslib
.osm
.charm
import CharmedOsmBase
, RelationsMissing
34 from opslib
.osm
.interfaces
.keystone
import KeystoneClient
35 from opslib
.osm
.interfaces
.mongo
import MongoClient
36 from opslib
.osm
.interfaces
.prometheus
import PrometheusClient
37 from opslib
.osm
.pod
import (
43 from opslib
.osm
.validator
import ModelValidator
, validator
46 logger
= logging
.getLogger(__name__
)
51 def _check_certificate_data(name
: str, content
: str):
52 if not name
or not content
:
53 raise ValueError("certificate name and content must be a non-empty string")
56 def _extract_certificates(certs_config
: str):
59 cert_list
= certs_config
.split(",")
60 for cert
in cert_list
:
61 name
, content
= cert
.split(":")
62 _check_certificate_data(name
, content
)
63 certificates
[name
] = content
67 def decode(content
: str):
68 return base64
.b64decode(content
.encode("utf-8")).decode("utf-8")
71 class ConfigModel(ModelValidator
):
72 keystone_enabled
: bool
77 database_commonkey
: str
78 mongodb_uri
: Optional
[str]
80 openstack_default_granularity
: int
81 global_request_timeout
: int
82 collector_interval
: int
83 vm_infra_metrics
: bool
84 evaluator_interval
: int
88 certificates
: Optional
[str]
89 image_pull_policy
: str
91 security_context
: bool
93 @validator("log_level")
94 def validate_log_level(cls
, v
):
95 if v
not in {"INFO", "DEBUG"}:
96 raise ValueError("value must be INFO or DEBUG")
99 @validator("certificates")
100 def validate_certificates(cls
, v
):
101 # Raises an exception if it cannot extract the certificates
102 _extract_certificates(v
)
105 @validator("mongodb_uri")
106 def validate_mongodb_uri(cls
, v
):
107 if v
and not v
.startswith("mongodb://"):
108 raise ValueError("mongodb_uri is not properly formed")
111 @validator("image_pull_policy")
112 def validate_image_pull_policy(cls
, v
):
115 "ifnotpresent": "IfNotPresent",
119 if v
not in values
.keys():
120 raise ValueError("value must be always, ifnotpresent or never")
124 def certificates_dict(cls
):
125 return _extract_certificates(cls
.certificates
) if cls
.certificates
else {}
128 class MonCharm(CharmedOsmBase
):
132 def __init__(self
, *args
) -> NoReturn
:
136 vscode_workspace
=VSCODE_WORKSPACE
,
138 if self
.config
.get("debug_mode"):
139 self
.enable_debug_mode(
140 pubkey
=self
.config
.get("debug_pubkey"),
143 "hostpath": self
.config
.get("debug_mon_local_path"),
144 "container-path": "/usr/lib/python3/dist-packages/osm_mon",
147 "hostpath": self
.config
.get("debug_n2vc_local_path"),
148 "container-path": "/usr/lib/python3/dist-packages/n2vc",
151 "hostpath": self
.config
.get("debug_common_local_path"),
152 "container-path": "/usr/lib/python3/dist-packages/osm_common",
156 self
.kafka
= KafkaRequires(self
)
157 self
.framework
.observe(self
.on
.kafka_available
, self
.configure_pod
)
158 self
.framework
.observe(self
.on
.kafka_broken
, self
.configure_pod
)
160 self
.mongodb_client
= MongoClient(self
, "mongodb")
161 self
.framework
.observe(self
.on
["mongodb"].relation_changed
, self
.configure_pod
)
162 self
.framework
.observe(self
.on
["mongodb"].relation_broken
, self
.configure_pod
)
164 self
.prometheus_client
= PrometheusClient(self
, "prometheus")
165 self
.framework
.observe(
166 self
.on
["prometheus"].relation_changed
, self
.configure_pod
168 self
.framework
.observe(
169 self
.on
["prometheus"].relation_broken
, self
.configure_pod
172 self
.keystone_client
= KeystoneClient(self
, "keystone")
173 self
.framework
.observe(self
.on
["keystone"].relation_changed
, self
.configure_pod
)
174 self
.framework
.observe(self
.on
["keystone"].relation_broken
, self
.configure_pod
)
176 def _check_missing_dependencies(self
, config
: ConfigModel
):
177 missing_relations
= []
179 if not self
.kafka
.host
or not self
.kafka
.port
:
180 missing_relations
.append("kafka")
181 if not config
.mongodb_uri
and self
.mongodb_client
.is_missing_data_in_unit():
182 missing_relations
.append("mongodb")
183 if self
.prometheus_client
.is_missing_data_in_app():
184 missing_relations
.append("prometheus")
185 if config
.keystone_enabled
:
186 if self
.keystone_client
.is_missing_data_in_app():
187 missing_relations
.append("keystone")
189 if missing_relations
:
190 raise RelationsMissing(missing_relations
)
192 def _build_cert_files(
196 cert_files_builder
= FilesV3Builder()
197 for name
, content
in config
.certificates_dict
.items():
198 cert_files_builder
.add_file(name
, decode(content
), mode
=0o600)
199 return cert_files_builder
.build()
201 def build_pod_spec(self
, image_info
):
203 config
= ConfigModel(**dict(self
.config
))
205 if config
.mongodb_uri
and not self
.mongodb_client
.is_missing_data_in_unit():
206 raise Exception("Mongodb data cannot be provided via config and relation")
209 self
._check
_missing
_dependencies
(config
)
211 security_context_enabled
= (
212 config
.security_context
if not config
.debug_mode
else False
215 # Create Builder for the PodSpec
216 pod_spec_builder
= PodSpecV3Builder(
217 enable_security_context
=security_context_enabled
220 # Add secrets to the pod
221 mongodb_secret_name
= f
"{self.app.name}-mongodb-secret"
222 pod_spec_builder
.add_secret(
225 "uri": config
.mongodb_uri
or self
.mongodb_client
.connection_string
,
226 "commonkey": config
.database_commonkey
,
229 grafana_secret_name
= f
"{self.app.name}-grafana-secret"
230 pod_spec_builder
.add_secret(
233 "url": config
.grafana_url
,
234 "user": config
.grafana_user
,
235 "password": config
.grafana_password
,
239 vca_secret_name
= f
"{self.app.name}-vca-secret"
240 pod_spec_builder
.add_secret(
243 "host": config
.vca_host
,
244 "user": config
.vca_user
,
245 "secret": config
.vca_secret
,
246 "cacert": config
.vca_cacert
,
251 container_builder
= ContainerV3Builder(
254 config
.image_pull_policy
,
255 run_as_non_root
=security_context_enabled
,
257 certs_files
= self
._build
_cert
_files
(config
)
260 container_builder
.add_volume_config("certs", "/certs", certs_files
)
262 container_builder
.add_port(name
=self
.app
.name
, port
=PORT
)
263 container_builder
.add_envs(
265 # General configuration
266 "ALLOW_ANONYMOUS_LOGIN": "yes",
267 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
.openstack_default_granularity
,
268 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config
.global_request_timeout
,
269 "OSMMON_GLOBAL_LOGLEVEL": config
.log_level
,
270 "OSMMON_COLLECTOR_INTERVAL": config
.collector_interval
,
271 "OSMMON_COLLECTOR_VM_INFRA_METRICS": config
.vm_infra_metrics
,
272 "OSMMON_EVALUATOR_INTERVAL": config
.evaluator_interval
,
273 # Kafka configuration
274 "OSMMON_MESSAGE_DRIVER": "kafka",
275 "OSMMON_MESSAGE_HOST": self
.kafka
.host
,
276 "OSMMON_MESSAGE_PORT": self
.kafka
.port
,
277 # Database configuration
278 "OSMMON_DATABASE_DRIVER": "mongo",
279 # Prometheus configuration
280 "OSMMON_PROMETHEUS_URL": f
"http://{self.prometheus_client.hostname}:{self.prometheus_client.port}",
283 prometheus_user
= self
.prometheus_client
.user
284 prometheus_password
= self
.prometheus_client
.password
285 if prometheus_user
and prometheus_password
:
286 container_builder
.add_envs(
288 "OSMMON_PROMETHEUS_USER": prometheus_user
,
289 "OSMMON_PROMETHEUS_PASSWORD": prometheus_password
,
292 container_builder
.add_secret_envs(
293 secret_name
=mongodb_secret_name
,
295 "OSMMON_DATABASE_URI": "uri",
296 "OSMMON_DATABASE_COMMONKEY": "commonkey",
299 container_builder
.add_secret_envs(
300 secret_name
=vca_secret_name
,
302 "OSMMON_VCA_HOST": "host",
303 "OSMMON_VCA_USER": "user",
304 "OSMMON_VCA_SECRET": "secret",
305 "OSMMON_VCA_CACERT": "cacert",
308 container_builder
.add_secret_envs(
309 secret_name
=grafana_secret_name
,
311 "OSMMON_GRAFANA_URL": "url",
312 "OSMMON_GRAFANA_USER": "user",
313 "OSMMON_GRAFANA_PASSWORD": "password",
316 if config
.keystone_enabled
:
317 keystone_secret_name
= f
"{self.app.name}-keystone-secret"
318 pod_spec_builder
.add_secret(
319 keystone_secret_name
,
321 "url": self
.keystone_client
.host
,
322 "user_domain": self
.keystone_client
.user_domain_name
,
323 "project_domain": self
.keystone_client
.project_domain_name
,
324 "service_username": self
.keystone_client
.username
,
325 "service_password": self
.keystone_client
.password
,
326 "service_project": self
.keystone_client
.service
,
329 container_builder
.add_env("OSMMON_KEYSTONE_ENABLED", True)
330 container_builder
.add_secret_envs(
331 secret_name
=keystone_secret_name
,
333 "OSMMON_KEYSTONE_URL": "url",
334 "OSMMON_KEYSTONE_DOMAIN_NAME": "user_domain",
335 "OSMMON_KEYSTONE_PROJECT_DOMAIN_NAME": "project_domain",
336 "OSMMON_KEYSTONE_SERVICE_USER": "service_username",
337 "OSMMON_KEYSTONE_SERVICE_PASSWORD": "service_password",
338 "OSMMON_KEYSTONE_SERVICE_PROJECT": "service_project",
341 container
= container_builder
.build()
344 restart_policy
= PodRestartPolicy()
345 restart_policy
.add_secrets()
346 pod_spec_builder
.set_restart_policy(restart_policy
)
348 # Add container to pod spec
349 pod_spec_builder
.add_container(container
)
351 return pod_spec_builder
.build()
356 {"path": "/usr/lib/python3/dist-packages/osm_mon"},
357 {"path": "/usr/lib/python3/dist-packages/osm_common"},
358 {"path": "/usr/lib/python3/dist-packages/n2vc"},
365 "name": "MON Server",
368 "module": "osm_mon.cmd.mon_server",
372 "name": "MON evaluator",
375 "module": "osm_mon.cmd.mon_evaluator",
379 "name": "MON collector",
382 "module": "osm_mon.cmd.mon_collector",
386 "name": "MON dashboarder",
389 "module": "osm_mon.cmd.mon_dashboarder",
395 if __name__
== "__main__":