Add secret-management in Charmed OSM
[osm/devops.git] / installers / charm / pla / src / charm.py
index ba3b1ff..d0df179 100755 (executable)
@@ -32,6 +32,7 @@ from opslib.osm.interfaces.kafka import KafkaClient
 from opslib.osm.interfaces.mongo import MongoClient
 from opslib.osm.pod import (
     ContainerV3Builder,
+    PodRestartPolicy,
     PodSpecV3Builder,
 )
 from opslib.osm.validator import ModelValidator, validator
@@ -109,6 +110,16 @@ class PlaCharm(CharmedOsmBase):
         # Create Builder for the PodSpec
         pod_spec_builder = PodSpecV3Builder()
 
+        # Add secrets to the pod
+        mongodb_secret_name = f"{self.app.name}-mongodb-secret"
+        pod_spec_builder.add_secret(
+            mongodb_secret_name,
+            {
+                "uri": config.mongodb_uri or self.mongodb_client.connection_string,
+                "commonkey": config.database_commonkey,
+            },
+        )
+
         # Build Container
         container_builder = ContainerV3Builder(
             self.app.name, image_info, config.image_pull_policy
@@ -125,14 +136,24 @@ class PlaCharm(CharmedOsmBase):
                 "OSMPLA_MESSAGE_PORT": self.kafka_client.port,
                 # Database configuration
                 "OSMPLA_DATABASE_DRIVER": "mongo",
-                "OSMPLA_DATABASE_URI": config.mongodb_uri
-                or self.mongodb_client.connection_string,
-                "OSMPLA_DATABASE_COMMONKEY": config.database_commonkey,
             }
         )
 
+        container_builder.add_secret_envs(
+            secret_name=mongodb_secret_name,
+            envs={
+                "OSMPLA_DATABASE_URI": "uri",
+                "OSMPLA_DATABASE_COMMONKEY": "commonkey",
+            },
+        )
+
         container = container_builder.build()
 
+        # Add Pod restart policy
+        restart_policy = PodRestartPolicy()
+        restart_policy.add_secrets(secret_names=(mongodb_secret_name))
+        pod_spec_builder.set_restart_policy(restart_policy)
+
         # Add container to pod spec
         pod_spec_builder.add_container(container)