Fix bug 1547: Add ingress.class annotation to OSM charms
[osm/devops.git] / installers / charm / grafana / src / charm.py
index d61873c..a7cc036 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,6 +50,7 @@ class ConfigModel(ModelValidator):
     max_file_size: int
     osm_dashboards: bool
     site_url: Optional[str]
+    cluster_issuer: Optional[str]
     ingress_whitelist_source_range: Optional[str]
     tls_secret_name: Optional[str]
 
@@ -90,8 +83,12 @@ 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()
@@ -178,6 +175,7 @@ class GrafanaCharm(CharmedOsmBase):
                     if config.max_file_size > 0
                     else config.max_file_size
                 ),
+                "kubernetes.io/ingress.class": "public",
             }
             ingress_resource_builder = IngressResourceV3Builder(
                 f"{self.app.name}-ingress", annotations
@@ -188,6 +186,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