Add charmcraft.yaml debug_mode to charmed-osm
[osm/devops.git] / installers / charm / grafana / src / charm.py
index d61873c..87776aa 100755 (executable)
 
 # pylint: disable=E0213
 
-import logging
-from typing import Optional, NoReturn
 from ipaddress import ip_network
+import logging
+from pathlib import Path
+from string import Template
+from typing import NoReturn, Optional
+from urllib.parse import urlparse
 
 from ops.main import main
-
 from opslib.osm.charm import CharmedOsmBase, RelationsMissing
-
+from opslib.osm.interfaces.prometheus import PrometheusClient
 from opslib.osm.pod import (
-    IngressResourceV3Builder,
-    FilesV3Builder,
     ContainerV3Builder,
+    FilesV3Builder,
+    IngressResourceV3Builder,
     PodSpecV3Builder,
 )
+from opslib.osm.validator import ModelValidator, validator
 
 
-from opslib.osm.validator import (
-    ModelValidator,
-    validator,
-)
-
-from opslib.osm.interfaces.prometheus import PrometheusClient
-
-from urllib.parse import urlparse
-from string import Template
-from pathlib import Path
-
 logger = logging.getLogger(__name__)
 
 PORT = 3000
@@ -58,8 +50,11 @@ class ConfigModel(ModelValidator):
     max_file_size: int
     osm_dashboards: bool
     site_url: Optional[str]
+    cluster_issuer: Optional[str]
+    ingress_class: Optional[str]
     ingress_whitelist_source_range: Optional[str]
     tls_secret_name: Optional[str]
+    image_pull_policy: Optional[str]
 
     @validator("max_file_size")
     def validate_max_file_size(cls, v):
@@ -81,6 +76,18 @@ class ConfigModel(ModelValidator):
             ip_network(v)
         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 GrafanaCharm(CharmedOsmBase):
     """GrafanaCharm Charm."""
@@ -90,22 +97,26 @@ class GrafanaCharm(CharmedOsmBase):
         super().__init__(*args, oci_image="image")
 
         self.prometheus_client = PrometheusClient(self, "prometheus")
-        self.framework.observe(self.on["prometheus"].relation_changed, self.configure_pod)
-        self.framework.observe(self.on["prometheus"].relation_broken, self.configure_pod)
+        self.framework.observe(
+            self.on["prometheus"].relation_changed, self.configure_pod
+        )
+        self.framework.observe(
+            self.on["prometheus"].relation_broken, self.configure_pod
+        )
 
     def _build_dashboard_files(self, config: ConfigModel):
         files_builder = FilesV3Builder()
         files_builder.add_file(
             "dashboard_osm.yaml",
-            Path("files/default_dashboards.yaml").read_text(),
+            Path("templates/default_dashboards.yaml").read_text(),
         )
         if config.osm_dashboards:
             osm_dashboards_mapping = {
-                "kafka_exporter_dashboard.json": "files/kafka_exporter_dashboard.json",
-                "mongodb_exporter_dashboard.json": "files/mongodb_exporter_dashboard.json",
-                "mysql_exporter_dashboard.json": "files/mysql_exporter_dashboard.json",
-                "nodes_exporter_dashboard.json": "files/nodes_exporter_dashboard.json",
-                "summary_dashboard.json": "files/summary_dashboard.json",
+                "kafka_exporter_dashboard.json": "templates/kafka_exporter_dashboard.json",
+                "mongodb_exporter_dashboard.json": "templates/mongodb_exporter_dashboard.json",
+                "mysql_exporter_dashboard.json": "templates/mysql_exporter_dashboard.json",
+                "nodes_exporter_dashboard.json": "templates/nodes_exporter_dashboard.json",
+                "summary_dashboard.json": "templates/summary_dashboard.json",
             }
             for file_name, path in osm_dashboards_mapping.items():
                 files_builder.add_file(file_name, Path(path).read_text())
@@ -115,7 +126,7 @@ class GrafanaCharm(CharmedOsmBase):
         files_builder = FilesV3Builder()
         files_builder.add_file(
             "datasource_prometheus.yaml",
-            Template(Path("files/default_datasources.yaml").read_text()).substitute(
+            Template(Path("templates/default_datasources.yaml").read_text()).substitute(
                 prometheus_host=self.prometheus_client.hostname,
                 prometheus_port=self.prometheus_client.port,
             ),
@@ -139,7 +150,9 @@ class GrafanaCharm(CharmedOsmBase):
         # Create Builder for the PodSpec
         pod_spec_builder = PodSpecV3Builder()
         # 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_http_readiness_probe(
             "/api/health",
@@ -177,8 +190,10 @@ class GrafanaCharm(CharmedOsmBase):
                     str(config.max_file_size) + "m"
                     if config.max_file_size > 0
                     else config.max_file_size
-                ),
+                )
             }
+            if config.ingress_class:
+                annotations["kubernetes.io/ingress.class"] = config.ingress_class
             ingress_resource_builder = IngressResourceV3Builder(
                 f"{self.app.name}-ingress", annotations
             )
@@ -188,6 +203,9 @@ class GrafanaCharm(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