X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Fnbi%2Fsrc%2Fcharm.py;h=bbbf5eced5f1acfc18d5d48d77ca686d07f51a6a;hb=refs%2Fchanges%2F22%2F10822%2F1;hp=9d62fe2fcbc4082430bb1d37658b5c72f92eaece;hpb=49379ced23b5e344a773ce77ac9cb59c1864e19b;p=osm%2Fdevops.git diff --git a/installers/charm/nbi/src/charm.py b/installers/charm/nbi/src/charm.py index 9d62fe2f..bbbf5ece 100755 --- a/installers/charm/nbi/src/charm.py +++ b/installers/charm/nbi/src/charm.py @@ -23,32 +23,25 @@ # pylint: disable=E0213 -import logging -from typing import Optional, NoReturn from ipaddress import ip_network +import logging +from typing import NoReturn, Optional from urllib.parse import urlparse -from ops.main import main +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 from opslib.osm.pod import ( ContainerV3Builder, - PodSpecV3Builder, IngressResourceV3Builder, + PodSpecV3Builder, ) - - -from opslib.osm.validator import ( - ModelValidator, - validator, -) - -from opslib.osm.interfaces.kafka import KafkaClient -from opslib.osm.interfaces.mongo import MongoClient -from opslib.osm.interfaces.prometheus import PrometheusClient -from opslib.osm.interfaces.keystone import KeystoneClient -from opslib.osm.interfaces.http import HttpServer +from opslib.osm.validator import ModelValidator, validator logger = logging.getLogger(__name__) @@ -63,8 +56,10 @@ class ConfigModel(ModelValidator): log_level: str max_file_size: int site_url: Optional[str] + cluster_issuer: Optional[str] ingress_whitelist_source_range: Optional[str] tls_secret_name: Optional[str] + mongodb_uri: Optional[str] @validator("auth_backend") def validate_auth_backend(cls, v): @@ -98,6 +93,12 @@ class ConfigModel(ModelValidator): ip_network(v) return v + @validator("mongodb_uri") + def validate_mongodb_uri(cls, v): + if v and not v.startswith("mongodb://"): + raise ValueError("mongodb_uri is not properly formed") + return v + class NbiCharm(CharmedOsmBase): def __init__(self, *args) -> NoReturn: @@ -140,7 +141,7 @@ class NbiCharm(CharmedOsmBase): if self.kafka_client.is_missing_data_in_unit(): missing_relations.append("kafka") - if self.mongodb_client.is_missing_data_in_unit(): + if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit(): missing_relations.append("mongodb") if self.prometheus_client.is_missing_data_in_app(): missing_relations.append("prometheus") @@ -154,10 +155,16 @@ 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") + # Check relations self._check_missing_dependencies(config) + # Create Builder for the PodSpec pod_spec_builder = PodSpecV3Builder() + # Build Init Container pod_spec_builder.add_init_container( { @@ -170,6 +177,7 @@ class NbiCharm(CharmedOsmBase): ], } ) + # Build Container container_builder = ContainerV3Builder(self.app.name, image_info) container_builder.add_port(name=self.app.name, port=PORT) @@ -195,13 +203,15 @@ class NbiCharm(CharmedOsmBase): "OSMNBI_MESSAGE_PORT": self.kafka_client.port, # Database configuration "OSMNBI_DATABASE_DRIVER": "mongo", - "OSMNBI_DATABASE_URI": self.mongodb_client.connection_string, + "OSMNBI_DATABASE_URI": config.mongodb_uri + or self.mongodb_client.connection_string, "OSMNBI_DATABASE_COMMONKEY": config.database_commonkey, # Storage configuration "OSMNBI_STORAGE_DRIVER": "mongo", "OSMNBI_STORAGE_PATH": "/app/storage", "OSMNBI_STORAGE_COLLECTION": "files", - "OSMNBI_STORAGE_URI": self.mongodb_client.connection_string, + "OSMNBI_STORAGE_URI": config.mongodb_uri + or self.mongodb_client.connection_string, # Prometheus configuration "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname, "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port, @@ -225,8 +235,10 @@ class NbiCharm(CharmedOsmBase): } ) container = container_builder.build() + # Add container to pod spec pod_spec_builder.add_container(container) + # Add ingress resources to pod spec if site url exists if config.site_url: parsed = urlparse(config.site_url) @@ -247,6 +259,9 @@ class NbiCharm(CharmedOsmBase): "nginx.ingress.kubernetes.io/whitelist-source-range" ] = config.ingress_whitelist_source_range + if config.cluster_issuer: + annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer + if parsed.scheme == "https": ingress_resource_builder.add_tls( [parsed.hostname], config.tls_secret_name @@ -257,7 +272,9 @@ class NbiCharm(CharmedOsmBase): ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT) ingress_resource = ingress_resource_builder.build() pod_spec_builder.add_ingress_resource(ingress_resource) + logger.debug(pod_spec_builder.build()) + return pod_spec_builder.build()