blob: db047c0be964a30c8a52b456b213e3964983445a [file] [log] [blame]
sousaedu1dd4c0d2020-11-04 17:43:47 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaedu1dd4c0d2020-11-04 17:43:47 +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
25
David Garcia5d1ec6e2021-03-25 15:04:52 +010026import base64
sousaedu1dd4c0d2020-11-04 17:43:47 +000027import logging
David Garcia5d1ec6e2021-03-25 15:04:52 +010028from typing import NoReturn, Optional
sousaedu1dd4c0d2020-11-04 17:43:47 +000029
David Garciac753dc52021-03-17 15:28:47 +010030
David Garcia4a0db7c2022-02-21 11:48:11 +010031from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
sousaedu1dd4c0d2020-11-04 17:43:47 +000032from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010033from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garciac753dc52021-03-17 15:28:47 +010034from opslib.osm.interfaces.keystone import KeystoneClient
David Garcia49379ce2021-02-24 13:48:22 +010035from opslib.osm.interfaces.mongo import MongoClient
36from opslib.osm.interfaces.prometheus import PrometheusClient
David Garcia141d9352021-09-08 17:48:40 +020037from opslib.osm.pod import (
38 ContainerV3Builder,
39 FilesV3Builder,
40 PodRestartPolicy,
41 PodSpecV3Builder,
42)
David Garciac753dc52021-03-17 15:28:47 +010043from opslib.osm.validator import ModelValidator, validator
sousaedu1dd4c0d2020-11-04 17:43:47 +000044
45
David Garcia49379ce2021-02-24 13:48:22 +010046logger = logging.getLogger(__name__)
sousaedu1dd4c0d2020-11-04 17:43:47 +000047
David Garcia49379ce2021-02-24 13:48:22 +010048PORT = 8000
sousaedu1dd4c0d2020-11-04 17:43:47 +000049
50
David Garcia5d1ec6e2021-03-25 15:04:52 +010051def _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")
54
55
56def _extract_certificates(certs_config: str):
57 certificates = {}
58 if certs_config:
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
64 return certificates
65
66
67def decode(content: str):
68 return base64.b64decode(content.encode("utf-8")).decode("utf-8")
69
70
David Garcia49379ce2021-02-24 13:48:22 +010071class ConfigModel(ModelValidator):
calvinosanc1a43a22f2021-03-08 15:20:07 +010072 keystone_enabled: bool
David Garcia49379ce2021-02-24 13:48:22 +010073 vca_host: str
74 vca_user: str
David Garciac753dc52021-03-17 15:28:47 +010075 vca_secret: str
David Garcia49379ce2021-02-24 13:48:22 +010076 vca_cacert: str
77 database_commonkey: str
sousaedu996a5602021-05-03 00:22:43 +020078 mongodb_uri: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010079 log_level: str
80 openstack_default_granularity: int
81 global_request_timeout: int
82 collector_interval: int
Guillermo Calvino5ec6be52022-11-17 11:54:31 +010083 vm_infra_metrics: bool
David Garcia49379ce2021-02-24 13:48:22 +010084 evaluator_interval: int
85 grafana_url: str
86 grafana_user: str
87 grafana_password: str
David Garcia5d1ec6e2021-03-25 15:04:52 +010088 certificates: Optional[str]
sousaedu0dc25b32021-08-30 16:33:33 +010089 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010090 debug_mode: bool
91 security_context: bool
sousaedu1dd4c0d2020-11-04 17:43:47 +000092
David Garcia49379ce2021-02-24 13:48:22 +010093 @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")
97 return v
sousaedu1dd4c0d2020-11-04 17:43:47 +000098
David Garcia5d1ec6e2021-03-25 15:04:52 +010099 @validator("certificates")
100 def validate_certificates(cls, v):
101 # Raises an exception if it cannot extract the certificates
102 _extract_certificates(v)
103 return v
104
sousaedu996a5602021-05-03 00:22:43 +0200105 @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")
109 return v
110
sousaedu3ddbbd12021-08-24 19:57:24 +0100111 @validator("image_pull_policy")
112 def validate_image_pull_policy(cls, v):
113 values = {
114 "always": "Always",
115 "ifnotpresent": "IfNotPresent",
116 "never": "Never",
117 }
118 v = v.lower()
119 if v not in values.keys():
120 raise ValueError("value must be always, ifnotpresent or never")
121 return values[v]
122
David Garcia5d1ec6e2021-03-25 15:04:52 +0100123 @property
124 def certificates_dict(cls):
125 return _extract_certificates(cls.certificates) if cls.certificates else {}
126
sousaedu1dd4c0d2020-11-04 17:43:47 +0000127
David Garcia49379ce2021-02-24 13:48:22 +0100128class MonCharm(CharmedOsmBase):
David Garcia4a0db7c2022-02-21 11:48:11 +0100129
130 on = KafkaEvents()
131
sousaedu1dd4c0d2020-11-04 17:43:47 +0000132 def __init__(self, *args) -> NoReturn:
David Garciad680be42021-08-17 11:03:55 +0200133 super().__init__(
134 *args,
135 oci_image="image",
David Garciad680be42021-08-17 11:03:55 +0200136 vscode_workspace=VSCODE_WORKSPACE,
137 )
David Garciacafe31e2021-11-18 16:45:05 +0100138 if self.config.get("debug_mode"):
139 self.enable_debug_mode(
140 pubkey=self.config.get("debug_pubkey"),
141 hostpaths={
142 "MON": {
143 "hostpath": self.config.get("debug_mon_local_path"),
144 "container-path": "/usr/lib/python3/dist-packages/osm_mon",
145 },
146 "N2VC": {
147 "hostpath": self.config.get("debug_n2vc_local_path"),
148 "container-path": "/usr/lib/python3/dist-packages/n2vc",
149 },
150 "osm_common": {
151 "hostpath": self.config.get("debug_common_local_path"),
152 "container-path": "/usr/lib/python3/dist-packages/osm_common",
153 },
154 },
155 )
David Garcia4a0db7c2022-02-21 11:48:11 +0100156 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)
sousaedu1dd4c0d2020-11-04 17:43:47 +0000159
David Garcia49379ce2021-02-24 13:48:22 +0100160 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)
sousaedu1dd4c0d2020-11-04 17:43:47 +0000163
David Garcia49379ce2021-02-24 13:48:22 +0100164 self.prometheus_client = PrometheusClient(self, "prometheus")
sousaedu1dd4c0d2020-11-04 17:43:47 +0000165 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +0100166 self.on["prometheus"].relation_changed, self.configure_pod
sousaedu1dd4c0d2020-11-04 17:43:47 +0000167 )
168 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +0100169 self.on["prometheus"].relation_broken, self.configure_pod
sousaedu1dd4c0d2020-11-04 17:43:47 +0000170 )
171
calvinosanc1a43a22f2021-03-08 15:20:07 +0100172 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)
175
David Garcia49379ce2021-02-24 13:48:22 +0100176 def _check_missing_dependencies(self, config: ConfigModel):
177 missing_relations = []
178
David Garcia4a0db7c2022-02-21 11:48:11 +0100179 if not self.kafka.host or not self.kafka.port:
David Garcia49379ce2021-02-24 13:48:22 +0100180 missing_relations.append("kafka")
sousaedu996a5602021-05-03 00:22:43 +0200181 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100182 missing_relations.append("mongodb")
183 if self.prometheus_client.is_missing_data_in_app():
184 missing_relations.append("prometheus")
calvinosanc1a43a22f2021-03-08 15:20:07 +0100185 if config.keystone_enabled:
186 if self.keystone_client.is_missing_data_in_app():
187 missing_relations.append("keystone")
David Garcia49379ce2021-02-24 13:48:22 +0100188
189 if missing_relations:
190 raise RelationsMissing(missing_relations)
191
David Garcia5d1ec6e2021-03-25 15:04:52 +0100192 def _build_cert_files(
193 self,
194 config: ConfigModel,
195 ):
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()
200
David Garcia49379ce2021-02-24 13:48:22 +0100201 def build_pod_spec(self, image_info):
202 # Validate config
203 config = ConfigModel(**dict(self.config))
sousaedu996a5602021-05-03 00:22:43 +0200204
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")
207
David Garcia49379ce2021-02-24 13:48:22 +0100208 # Check relations
209 self._check_missing_dependencies(config)
sousaedu996a5602021-05-03 00:22:43 +0200210
sousaedu540d9372021-09-29 01:53:30 +0100211 security_context_enabled = (
212 config.security_context if not config.debug_mode else False
213 )
214
David Garcia49379ce2021-02-24 13:48:22 +0100215 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100216 pod_spec_builder = PodSpecV3Builder(
217 enable_security_context=security_context_enabled
218 )
sousaedu996a5602021-05-03 00:22:43 +0200219
David Garcia141d9352021-09-08 17:48:40 +0200220 # Add secrets to the pod
221 mongodb_secret_name = f"{self.app.name}-mongodb-secret"
222 pod_spec_builder.add_secret(
223 mongodb_secret_name,
224 {
225 "uri": config.mongodb_uri or self.mongodb_client.connection_string,
226 "commonkey": config.database_commonkey,
227 },
228 )
229 grafana_secret_name = f"{self.app.name}-grafana-secret"
230 pod_spec_builder.add_secret(
231 grafana_secret_name,
232 {
233 "url": config.grafana_url,
234 "user": config.grafana_user,
235 "password": config.grafana_password,
236 },
237 )
238
239 vca_secret_name = f"{self.app.name}-vca-secret"
240 pod_spec_builder.add_secret(
241 vca_secret_name,
242 {
243 "host": config.vca_host,
244 "user": config.vca_user,
245 "secret": config.vca_secret,
246 "cacert": config.vca_cacert,
247 },
248 )
249
David Garcia49379ce2021-02-24 13:48:22 +0100250 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100251 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100252 self.app.name,
253 image_info,
254 config.image_pull_policy,
255 run_as_non_root=security_context_enabled,
sousaedu3ddbbd12021-08-24 19:57:24 +0100256 )
David Garcia5d1ec6e2021-03-25 15:04:52 +0100257 certs_files = self._build_cert_files(config)
sousaedu996a5602021-05-03 00:22:43 +0200258
David Garcia5d1ec6e2021-03-25 15:04:52 +0100259 if certs_files:
260 container_builder.add_volume_config("certs", "/certs", certs_files)
sousaedu996a5602021-05-03 00:22:43 +0200261
David Garcia49379ce2021-02-24 13:48:22 +0100262 container_builder.add_port(name=self.app.name, port=PORT)
263 container_builder.add_envs(
264 {
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,
Guillermo Calvino5ec6be52022-11-17 11:54:31 +0100271 "OSMMON_COLLECTOR_VM_INFRA_METRICS": config.vm_infra_metrics,
David Garcia49379ce2021-02-24 13:48:22 +0100272 "OSMMON_EVALUATOR_INTERVAL": config.evaluator_interval,
273 # Kafka configuration
274 "OSMMON_MESSAGE_DRIVER": "kafka",
David Garcia4a0db7c2022-02-21 11:48:11 +0100275 "OSMMON_MESSAGE_HOST": self.kafka.host,
276 "OSMMON_MESSAGE_PORT": self.kafka.port,
David Garcia49379ce2021-02-24 13:48:22 +0100277 # Database configuration
278 "OSMMON_DATABASE_DRIVER": "mongo",
David Garcia49379ce2021-02-24 13:48:22 +0100279 # Prometheus configuration
280 "OSMMON_PROMETHEUS_URL": f"http://{self.prometheus_client.hostname}:{self.prometheus_client.port}",
David Garcia49379ce2021-02-24 13:48:22 +0100281 }
sousaedu1dd4c0d2020-11-04 17:43:47 +0000282 )
David Garciade440ed2021-10-11 19:56:53 +0200283 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(
287 {
288 "OSMMON_PROMETHEUS_USER": prometheus_user,
289 "OSMMON_PROMETHEUS_PASSWORD": prometheus_password,
290 }
291 )
David Garcia141d9352021-09-08 17:48:40 +0200292 container_builder.add_secret_envs(
293 secret_name=mongodb_secret_name,
294 envs={
295 "OSMMON_DATABASE_URI": "uri",
296 "OSMMON_DATABASE_COMMONKEY": "commonkey",
297 },
298 )
299 container_builder.add_secret_envs(
300 secret_name=vca_secret_name,
301 envs={
302 "OSMMON_VCA_HOST": "host",
303 "OSMMON_VCA_USER": "user",
304 "OSMMON_VCA_SECRET": "secret",
305 "OSMMON_VCA_CACERT": "cacert",
306 },
307 )
308 container_builder.add_secret_envs(
309 secret_name=grafana_secret_name,
310 envs={
311 "OSMMON_GRAFANA_URL": "url",
312 "OSMMON_GRAFANA_USER": "user",
313 "OSMMON_GRAFANA_PASSWORD": "password",
314 },
315 )
calvinosanc1a43a22f2021-03-08 15:20:07 +0100316 if config.keystone_enabled:
David Garcia141d9352021-09-08 17:48:40 +0200317 keystone_secret_name = f"{self.app.name}-keystone-secret"
318 pod_spec_builder.add_secret(
319 keystone_secret_name,
calvinosanc1a43a22f2021-03-08 15:20:07 +0100320 {
David Garcia141d9352021-09-08 17:48:40 +0200321 "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,
327 },
328 )
329 container_builder.add_env("OSMMON_KEYSTONE_ENABLED", True)
330 container_builder.add_secret_envs(
331 secret_name=keystone_secret_name,
332 envs={
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",
339 },
calvinosanc1a43a22f2021-03-08 15:20:07 +0100340 )
David Garcia49379ce2021-02-24 13:48:22 +0100341 container = container_builder.build()
sousaedu996a5602021-05-03 00:22:43 +0200342
David Garcia141d9352021-09-08 17:48:40 +0200343 # Add restart policy
344 restart_policy = PodRestartPolicy()
345 restart_policy.add_secrets()
346 pod_spec_builder.set_restart_policy(restart_policy)
347
David Garcia49379ce2021-02-24 13:48:22 +0100348 # Add container to pod spec
349 pod_spec_builder.add_container(container)
sousaedu996a5602021-05-03 00:22:43 +0200350
David Garcia49379ce2021-02-24 13:48:22 +0100351 return pod_spec_builder.build()
sousaedu1dd4c0d2020-11-04 17:43:47 +0000352
353
David Garciad680be42021-08-17 11:03:55 +0200354VSCODE_WORKSPACE = {
355 "folders": [
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"},
359 ],
360 "settings": {},
361 "launch": {
362 "version": "0.2.0",
363 "configurations": [
364 {
365 "name": "MON Server",
366 "type": "python",
367 "request": "launch",
368 "module": "osm_mon.cmd.mon_server",
369 "justMyCode": False,
370 },
371 {
372 "name": "MON evaluator",
373 "type": "python",
374 "request": "launch",
375 "module": "osm_mon.cmd.mon_evaluator",
376 "justMyCode": False,
377 },
378 {
379 "name": "MON collector",
380 "type": "python",
381 "request": "launch",
382 "module": "osm_mon.cmd.mon_collector",
383 "justMyCode": False,
384 },
385 {
386 "name": "MON dashboarder",
387 "type": "python",
388 "request": "launch",
389 "module": "osm_mon.cmd.mon_dashboarder",
390 "justMyCode": False,
391 },
392 ],
393 },
394}
sousaedu1dd4c0d2020-11-04 17:43:47 +0000395if __name__ == "__main__":
396 main(MonCharm)