Update exporters: migration to CharmHub 43/11743/1
authorDavid Garcia <david.garcia@canonical.com>
Fri, 11 Mar 2022 15:58:35 +0000 (16:58 +0100)
committerDavid Garcia <david.garcia@canonical.com>
Fri, 11 Mar 2022 16:00:30 +0000 (17:00 +0100)
Also, added kafka_endpoint config to kafka exporter

Change-Id: I24459e33841da33455b6780a4de2c808ddf30d0b
Signed-off-by: David Garcia <david.garcia@canonical.com>
installers/charm/kafka-exporter/config.yaml
installers/charm/kafka-exporter/metadata.yaml
installers/charm/kafka-exporter/src/charm.py
installers/charm/mongodb-exporter/metadata.yaml
installers/charm/mongodb-exporter/src/charm.py
installers/charm/mysqld-exporter/metadata.yaml
installers/charm/mysqld-exporter/src/charm.py
installers/charm/mysqld-exporter/tox.ini

index 22e9387..5931336 100644 (file)
@@ -56,3 +56,6 @@ options:
     description: Enables the security context of the pods
     type: boolean
     default: false
+  kafka_endpoint:
+    description: Host and port of Kafka in the format <host>:<port>
+    type: string
index 1339d27..a70b3b6 100644 (file)
@@ -19,7 +19,7 @@
 # osm-charmers@lists.launchpad.net
 ##
 
-name: kafka-exporter
+name: kafka-exporter-k8s
 summary: OSM Prometheus Kafka Exporter
 description: |
   A CAAS charm to deploy OSM's Prometheus Kafka Exporter.
@@ -38,7 +38,7 @@ resources:
   image:
     type: oci-image
     description: Image of kafka-exporter
-    upstream-source: "bitnami/kafka-exporter:latest"
+    upstream-source: "bitnami/kafka-exporter:1.4.2"
 requires:
   kafka:
     interface: kafka
index e6b2bf7..ec6eaab 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,6 +82,18 @@ 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 KafkaEndpoint:
+    def __init__(self, host: str, port: str) -> None:
+        self.host = host
+        self.port = port
+
 
 class KafkaExporterCharm(CharmedOsmBase):
 
@@ -144,22 +157,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.
+    def _is_kafka_relation_set(self) -> bool:
+        """Check if the Kafka relation is set or not."""
+        return self.kafka.host and self.kafka.port
 
-        Raises:
-            RelationsMissing: if kafka is missing.
-        """
-        missing_relations = []
-
-        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 +188,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 +203,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 +223,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()
index 26b6dad..c3a0b77 100644 (file)
@@ -19,7 +19,7 @@
 # osm-charmers@lists.launchpad.net
 ##
 
-name: mongodb-exporter
+name: mongodb-exporter-k8s
 summary: OSM Prometheus Mongodb Exporter
 description: |
   A CAAS charm to deploy OSM's Prometheus Mongodb Exporter.
@@ -38,7 +38,7 @@ resources:
   image:
     type: oci-image
     description: Image of mongodb-exporter
-    upstream-source: "bitnami/mongodb-exporter:latest"
+    upstream-source: "bitnami/mongodb-exporter:0.30.0"
 provides:
   prometheus-scrape:
     interface: prometheus
index 500a1e3..0ee127c 100755 (executable)
@@ -210,7 +210,7 @@ class MongodbExporterCharm(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,
index 5720b96..7f6fb6e 100644 (file)
@@ -19,7 +19,7 @@
 # osm-charmers@lists.launchpad.net
 ##
 
-name: mysqld-exporter
+name: mysqld-exporter-k8s
 summary: OSM Prometheus Mysql Exporter
 description: |
   A CAAS charm to deploy OSM's Prometheus Mysql Exporter.
@@ -38,7 +38,7 @@ resources:
   image:
     type: oci-image
     description: Image of mysqld-exporter
-    upstream-source: "bitnami/mysqld-exporter:latest"
+    upstream-source: "bitnami/mysqld-exporter:0.14.0"
 provides:
   prometheus-scrape:
     interface: prometheus
index 46588b9..153dbfd 100755 (executable)
@@ -209,7 +209,7 @@ class MysqlExporterCharm(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,
index 8e3318a..4c7970d 100644 (file)
@@ -99,7 +99,7 @@ whitelist_externals =
   charmcraft
   sh
 commands =
-  charmcraft build
+  charmcraft pack
   sh -c 'ubuntu_version=20.04; \
         architectures="amd64-aarch64-arm64"; \
         charm_name=`cat metadata.yaml | grep -E "^name: " | cut -f 2 -d " "`; \