Fix validation error for ImagePullPolicy in charms
[osm/devops.git] / installers / charm / mysqld-exporter / src / charm.py
index 85a1e67..a0015cc 100755 (executable)
@@ -49,9 +49,11 @@ PORT = 9104
 class ConfigModel(ModelValidator):
     site_url: Optional[str]
     cluster_issuer: Optional[str]
+    ingress_class: Optional[str]
     ingress_whitelist_source_range: Optional[str]
     tls_secret_name: Optional[str]
     mysql_uri: Optional[str]
+    image_pull_policy: str
 
     @validator("site_url")
     def validate_site_url(cls, v):
@@ -73,6 +75,18 @@ class ConfigModel(ModelValidator):
             raise ValueError("mysql_uri is not properly formed")
         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 MysqlExporterCharm(CharmedOsmBase):
     def __init__(self, *args) -> NoReturn:
@@ -130,7 +144,7 @@ class MysqlExporterCharm(CharmedOsmBase):
         if self.unit.is_leader():
             self.dashboard_target.publish_info(
                 name="osm-mysql",
-                dashboard=Path("files/mysql_exporter_dashboard.json").read_text(),
+                dashboard=Path("templates/mysql_exporter_dashboard.json").read_text(),
             )
 
     def _check_missing_dependencies(self, config: ConfigModel):
@@ -172,7 +186,9 @@ class MysqlExporterCharm(CharmedOsmBase):
         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(
             path="/api/health",
@@ -210,7 +226,9 @@ class MysqlExporterCharm(CharmedOsmBase):
         # Add ingress resources to PodSpec if site url exists
         if config.site_url:
             parsed = urlparse(config.site_url)
-            annotations = {"kubernetes.io/ingress.class": "public"}
+            annotations = {}
+            if config.ingress_class:
+                annotations["kubernetes.io/ingress.class"] = config.ingress_class
             ingress_resource_builder = IngressResourceV3Builder(
                 f"{self.app.name}-ingress", annotations
             )