Add charmcraft.yaml debug_mode to charmed-osm
[osm/devops.git] / installers / charm / nbi / src / charm.py
index 1f5812a..0e36073 100755 (executable)
@@ -57,8 +57,11 @@ class ConfigModel(ModelValidator):
     max_file_size: int
     site_url: Optional[str]
     cluster_issuer: Optional[str]
+    ingress_class: Optional[str]
     ingress_whitelist_source_range: Optional[str]
     tls_secret_name: Optional[str]
+    mongodb_uri: Optional[str]
+    image_pull_policy: Optional[str]
 
     @validator("auth_backend")
     def validate_auth_backend(cls, v):
@@ -92,10 +95,34 @@ 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
+
+    @validator("image_pull_policy")
+    def validate_image_pull_policy(cls, v):
+        values = {
+            "always": "Always",
+            "ifnotpresent": "IfNotPresent",
+            "never": "Never",
+        }
+        v = v.lower()
+        if v not in values.keys():
+            raise ValueError("value must be always, ifnotpresent or never")
+        return values[v]
+
 
 class NbiCharm(CharmedOsmBase):
     def __init__(self, *args) -> NoReturn:
-        super().__init__(*args, oci_image="image")
+        super().__init__(
+            *args,
+            oci_image="image",
+            debug_mode_config_key="debug_mode",
+            debug_pubkey_config_key="debug_pubkey",
+            vscode_workspace=VSCODE_WORKSPACE,
+        )
 
         self.kafka_client = KafkaClient(self, "kafka")
         self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
@@ -134,7 +161,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")
@@ -148,10 +175,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(
             {
@@ -164,8 +197,11 @@ class NbiCharm(CharmedOsmBase):
                 ],
             }
         )
+
         # Build Container
-        container_builder = ContainerV3Builder(self.app.name, image_info)
+        container_builder = ContainerV3Builder(
+            self.app.name, image_info, config.image_pull_policy
+        )
         container_builder.add_port(name=self.app.name, port=PORT)
         container_builder.add_tcpsocket_readiness_probe(
             PORT,
@@ -189,13 +225,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,
@@ -219,8 +257,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)
@@ -232,6 +272,8 @@ class NbiCharm(CharmedOsmBase):
                 ),
                 "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
             }
+            if config.ingress_class:
+                annotations["kubernetes.io/ingress.class"] = config.ingress_class
             ingress_resource_builder = IngressResourceV3Builder(
                 f"{self.app.name}-ingress", annotations
             )
@@ -254,9 +296,33 @@ 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()
 
 
+VSCODE_WORKSPACE = {
+    "folders": [
+        {"path": "/usr/lib/python3/dist-packages/osm_nbi"},
+        {"path": "/usr/lib/python3/dist-packages/osm_common"},
+        {"path": "/usr/lib/python3/dist-packages/osm_im"},
+    ],
+    "settings": {},
+    "launch": {
+        "version": "0.2.0",
+        "configurations": [
+            {
+                "name": "NBI",
+                "type": "python",
+                "request": "launch",
+                "module": "osm_nbi.nbi",
+                "justMyCode": False,
+            }
+        ],
+    },
+}
+
+
 if __name__ == "__main__":
     main(NbiCharm)