Make tcpsocket readiness and liveness configurable
[osm/devops.git] / installers / charm / nbi / src / charm.py
index 4aaecb9..e120756 100755 (executable)
@@ -24,6 +24,7 @@
 
 
 from ipaddress import ip_network
+import json
 import logging
 from typing import NoReturn, Optional
 from urllib.parse import urlparse
@@ -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,9 +119,40 @@ 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 {}
 
-class NbiCharm(CharmedOsmBase):
+    @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:
@@ -193,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")
 
@@ -242,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(
             {