X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Fnbi%2Fsrc%2Fcharm.py;h=e12075680f230efa29bd510863f73d0db5effac1;hb=5b78193597bfcf487d4700d68809d0390bc68857;hp=c6a7c1eab9e5eaf51418d9457a7d4c16f67e4c61;hpb=de440ed8935a4ad8b7acaad9a6356cedada7bf2b;p=osm%2Fdevops.git diff --git a/installers/charm/nbi/src/charm.py b/installers/charm/nbi/src/charm.py index c6a7c1ea..e1207568 100755 --- a/installers/charm/nbi/src/charm.py +++ b/installers/charm/nbi/src/charm.py @@ -24,15 +24,16 @@ from ipaddress import ip_network +import json import logging from typing import NoReturn, Optional from urllib.parse import urlparse +from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires from ops.main import main from opslib.osm.charm import CharmedOsmBase, RelationsMissing from opslib.osm.interfaces.http import HttpServer -from opslib.osm.interfaces.kafka import KafkaClient from opslib.osm.interfaces.keystone import KeystoneClient from opslib.osm.interfaces.mongo import MongoClient from opslib.osm.interfaces.prometheus import PrometheusClient @@ -65,6 +66,8 @@ class ConfigModel(ModelValidator): image_pull_policy: str debug_mode: bool security_context: bool + tcpsocket_liveness_probe: Optional[str] + tcpsocket_readiness_probe: Optional[str] @validator("auth_backend") def validate_auth_backend(cls, v): @@ -116,20 +119,66 @@ class ConfigModel(ModelValidator): raise ValueError("value must be always, ifnotpresent or never") return values[v] + @staticmethod + def _validate_tcpsocket_probe(probe_str) -> dict: + valid_attributes = ( + "initial_delay_seconds", + "timeout_seconds", + "period_seconds", + "success_threshold", + "failure_threshold", + ) + if probe_str: + probe_dict = json.loads(probe_str) + if all(attribute in valid_attributes for attribute in probe_dict): + return probe_dict + raise ValueError( + "One or more attributes are not accepted by the tcpsocket probe configuration" + ) + return {} + + @validator("tcpsocket_readiness_probe") + def validate_tcpsocket_readiness_probe(cls, v): + try: + return ConfigModel._validate_tcpsocket_probe(v) + except Exception as e: + raise ValueError(f"tcpsocket_readiness_probe configuration error: {e}") + + @validator("tcpsocket_liveness_probe") + def validate_tcpsocket_liveness_probe(cls, v): + try: + return ConfigModel._validate_tcpsocket_probe(v) + except Exception as e: + raise ValueError(f"tcpsocket_liveness_probe configuration error: {e}") + class NbiCharm(CharmedOsmBase): + on = KafkaEvents() + def __init__(self, *args) -> NoReturn: super().__init__( *args, oci_image="image", - debug_mode_config_key="debug_mode", - debug_pubkey_config_key="debug_pubkey", vscode_workspace=VSCODE_WORKSPACE, ) + if self.config.get("debug_mode"): + self.enable_debug_mode( + pubkey=self.config.get("debug_pubkey"), + hostpaths={ + "NBI": { + "hostpath": self.config.get("debug_nbi_local_path"), + "container-path": "/usr/lib/python3/dist-packages/osm_nbi", + }, + "osm_common": { + "hostpath": self.config.get("debug_common_local_path"), + "container-path": "/usr/lib/python3/dist-packages/osm_common", + }, + }, + ) - self.kafka_client = KafkaClient(self, "kafka") - self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod) - self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod) + self.kafka = KafkaRequires(self) + self.framework.observe(self.on.kafka_available, self.configure_pod) + self.framework.observe(self.on.kafka_broken, self.configure_pod) self.mongodb_client = MongoClient(self, "mongodb") self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod) @@ -162,10 +211,7 @@ class NbiCharm(CharmedOsmBase): def _check_missing_dependencies(self, config: ConfigModel): missing_relations = [] - if ( - self.kafka_client.is_missing_data_in_unit() - and self.kafka_client.is_missing_data_in_app() - ): + if not self.kafka.host or not self.kafka.port: missing_relations.append("kafka") if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit(): missing_relations.append("mongodb") @@ -181,7 +227,6 @@ class NbiCharm(CharmedOsmBase): def build_pod_spec(self, image_info): # Validate config config = ConfigModel(**dict(self.config)) - if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit(): raise Exception("Mongodb data cannot be provided via config and relation") @@ -215,7 +260,7 @@ class NbiCharm(CharmedOsmBase): "command": [ "sh", "-c", - f"until (nc -zvw1 {self.kafka_client.host} {self.kafka_client.port} ); do sleep 3; done; exit 0", + f"until (nc -zvw1 {self.kafka.host} {self.kafka.port} ); do sleep 3; done; exit 0", ], } ) @@ -230,13 +275,31 @@ class NbiCharm(CharmedOsmBase): container_builder.add_port(name=self.app.name, port=PORT) container_builder.add_tcpsocket_readiness_probe( PORT, - initial_delay_seconds=5, - timeout_seconds=5, + initial_delay_seconds=config.tcpsocket_readiness_probe.get( + "initial_delay_seconds", 5 + ), + timeout_seconds=config.tcpsocket_readiness_probe.get("timeout_seconds", 5), + period_seconds=config.tcpsocket_readiness_probe.get("period_seconds", 10), + success_threshold=config.tcpsocket_readiness_probe.get( + "success_threshold", 1 + ), + failure_threshold=config.tcpsocket_readiness_probe.get( + "failure_threshold", 3 + ), ) container_builder.add_tcpsocket_liveness_probe( PORT, - initial_delay_seconds=45, - timeout_seconds=10, + initial_delay_seconds=config.tcpsocket_liveness_probe.get( + "initial_delay_seconds", 45 + ), + timeout_seconds=config.tcpsocket_liveness_probe.get("timeout_seconds", 10), + period_seconds=config.tcpsocket_liveness_probe.get("period_seconds", 10), + success_threshold=config.tcpsocket_liveness_probe.get( + "success_threshold", 1 + ), + failure_threshold=config.tcpsocket_liveness_probe.get( + "failure_threshold", 3 + ), ) container_builder.add_envs( { @@ -245,9 +308,9 @@ class NbiCharm(CharmedOsmBase): "OSMNBI_SERVER_ENABLE_TEST": config.enable_test, "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public", # Kafka configuration - "OSMNBI_MESSAGE_HOST": self.kafka_client.host, + "OSMNBI_MESSAGE_HOST": self.kafka.host, "OSMNBI_MESSAGE_DRIVER": "kafka", - "OSMNBI_MESSAGE_PORT": self.kafka_client.port, + "OSMNBI_MESSAGE_PORT": self.kafka.port, # Database configuration "OSMNBI_DATABASE_DRIVER": "mongo", # Storage configuration