Make tcpsocket readiness and liveness configurable
[osm/devops.git] / installers / charm / kafka-exporter / src / charm.py
index e6b2bf7..07a854f 100755 (executable)
@@ -54,6 +54,7 @@ class ConfigModel(ModelValidator):
     tls_secret_name: Optional[str]
     image_pull_policy: str
     security_context: bool
+    kafka_endpoint: Optional[str]
 
     @validator("site_url")
     def validate_site_url(cls, v):
@@ -81,9 +82,20 @@ class ConfigModel(ModelValidator):
             raise ValueError("value must be always, ifnotpresent or never")
         return values[v]
 
+    @validator("kafka_endpoint")
+    def validate_kafka_endpoint(cls, v):
+        if v and len(v.split(":")) != 2:
+            raise ValueError("value must be in the format <host>:<port>")
+        return v
 
-class KafkaExporterCharm(CharmedOsmBase):
 
+class KafkaEndpoint:
+    def __init__(self, host: str, port: str) -> None:
+        self.host = host
+        self.port = port
+
+
+class KafkaExporterCharm(CharmedOsmBase):
     on = KafkaEvents()
 
     def __init__(self, *args) -> NoReturn:
@@ -144,22 +156,23 @@ class KafkaExporterCharm(CharmedOsmBase):
                 dashboard=Path("templates/kafka_exporter_dashboard.json").read_text(),
             )
 
-    def _check_missing_dependencies(self, config: ConfigModel):
-        """Check if there is any relation missing.
+    def _is_kafka_endpoint_set(self, config: ConfigModel) -> bool:
+        """Check if Kafka endpoint is set."""
+        return config.kafka_endpoint or self._is_kafka_relation_set()
 
-        Args:
-            config (ConfigModel): object with configuration information.
-
-        Raises:
-            RelationsMissing: if kafka is missing.
-        """
-        missing_relations = []
+    def _is_kafka_relation_set(self) -> bool:
+        """Check if the Kafka relation is set or not."""
+        return self.kafka.host and self.kafka.port
 
-        if not self.kafka.host or not self.kafka.port:
-            missing_relations.append("kafka")
-
-        if missing_relations:
-            raise RelationsMissing(missing_relations)
+    @property
+    def kafka_endpoint(self) -> KafkaEndpoint:
+        config = ConfigModel(**dict(self.config))
+        if config.kafka_endpoint:
+            host, port = config.kafka_endpoint.split(":")
+        else:
+            host = self.kafka.host
+            port = self.kafka.port
+        return KafkaEndpoint(host, port)
 
     def build_pod_spec(self, image_info):
         """Build the PodSpec to be used.
@@ -174,7 +187,8 @@ class KafkaExporterCharm(CharmedOsmBase):
         config = ConfigModel(**dict(self.config))
 
         # Check relations
-        self._check_missing_dependencies(config)
+        if not self._is_kafka_endpoint_set(config):
+            raise RelationsMissing(["kafka"])
 
         # Create Builder for the PodSpec
         pod_spec_builder = PodSpecV3Builder(
@@ -188,7 +202,7 @@ class KafkaExporterCharm(CharmedOsmBase):
             config.image_pull_policy,
             run_as_non_root=config.security_context,
         )
-        container_builder.add_port(name=self.app.name, port=PORT)
+        container_builder.add_port(name="exporter", port=PORT)
         container_builder.add_http_readiness_probe(
             path="/api/health",
             port=PORT,
@@ -208,7 +222,7 @@ class KafkaExporterCharm(CharmedOsmBase):
         container_builder.add_command(
             [
                 "kafka_exporter",
-                f"--kafka.server={self.kafka.host}:{self.kafka.port}",
+                f"--kafka.server={self.kafka_endpoint.host}:{self.kafka_endpoint.port}",
             ]
         )
         container = container_builder.build()