X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fdevops.git;a=blobdiff_plain;f=installers%2Fcharm%2Fkeystone%2Fsrc%2Fcharm.py;h=637461e35f1964e14ca03bab8136db01788e07b9;hp=ef16690a743eacc29eed57ff2a05f32dc2d5567d;hb=ab11f84f8279b4951295488c3016f5437bc747f1;hpb=2323c36435b2f6769fb77e72e0771e9ec60004e0 diff --git a/installers/charm/keystone/src/charm.py b/installers/charm/keystone/src/charm.py index ef16690a..637461e3 100755 --- a/installers/charm/keystone/src/charm.py +++ b/installers/charm/keystone/src/charm.py @@ -27,8 +27,8 @@ from urllib.parse import urlparse from cryptography.fernet import Fernet -from ops.charm import CharmBase, EventBase -from ops.framework import StoredState +from ops.charm import CharmBase, EventBase, CharmEvents +from ops.framework import StoredState, EventSource from ops.main import main from ops.model import ( ActiveStatus, @@ -40,7 +40,7 @@ from ops.model import ( LOGGER = logging.getLogger(__name__) -REQUIRED_SETTINGS = [] +REQUIRED_SETTINGS = ["token_expiration"] # This is hardcoded in the keystone container script DATABASE_NAME = "keystone" @@ -57,10 +57,22 @@ CREDENTIAL_KEYS_PATH = "/etc/keystone/credential-keys" FERNET_KEYS_PATH = "/etc/keystone/fernet-keys" +class ConfigurePodEvent(EventBase): + """Configure Pod event""" + + pass + + +class KeystoneEvents(CharmEvents): + """Keystone Events""" + + configure_pod = EventSource(ConfigurePodEvent) + class KeystoneCharm(CharmBase): """Keystone K8s Charm""" state = StoredState() + on = KeystoneEvents() def __init__(self, *args) -> NoReturn: """Constructor of the Charm object. @@ -83,10 +95,16 @@ class KeystoneCharm(CharmBase): self.framework.observe(self.on.leader_elected, self.configure_pod) self.framework.observe(self.on.update_status, self.configure_pod) + # Registering custom internal events + self.framework.observe(self.on.configure_pod, self.configure_pod) + # Register relation events self.framework.observe( self.on.db_relation_changed, self._on_db_relation_changed ) + self.framework.observe( + self.on.db_relation_departed, self._on_db_relation_departed + ) self.framework.observe( self.on.keystone_relation_joined, self._publish_keystone_info ) @@ -99,23 +117,22 @@ class KeystoneCharm(CharmBase): event (EventBase): Keystone relation event to update NBI. """ config = self.model.config - if self.unit.is_leader(): - rel_data = { - "host": f"http://{self.app.name}:{KEYSTONE_PORT}/v3", - "port": str(KEYSTONE_PORT), - "keystone_db_password": config["keystone_db_password"], - "region_id": config["region_id"], - "user_domain_name": config["user_domain_name"], - "project_domain_name": config["project_domain_name"], - "admin_username": config["admin_username"], - "admin_password": config["admin_password"], - "admin_project_name": config["admin_project"], - "username": config["service_username"], - "password": config["service_password"], - "service": config["service_project"], - } - for k, v in rel_data.items(): - event.relation.data[self.model.unit][k] = v + rel_data = { + "host": f"http://{self.app.name}:{KEYSTONE_PORT}/v3", + "port": str(KEYSTONE_PORT), + "keystone_db_password": config["keystone_db_password"], + "region_id": config["region_id"], + "user_domain_name": config["user_domain_name"], + "project_domain_name": config["project_domain_name"], + "admin_username": config["admin_username"], + "admin_password": config["admin_password"], + "admin_project_name": config["admin_project"], + "username": config["service_username"], + "password": config["service_password"], + "service": config["service_project"], + } + for k, v in rel_data.items(): + event.relation.data[self.model.unit][k] = v def _on_db_relation_changed(self, event: EventBase) -> NoReturn: """Reads information about the DB relation, in order for keystone to @@ -125,12 +142,45 @@ class KeystoneCharm(CharmBase): event (EventBase): DB relation event to access database information. """ - self.state.db_host = event.relation.data[event.unit].get("host") - self.state.db_port = event.relation.data[event.unit].get("port", 3306) - self.state.db_user = "root" # event.relation.data[event.unit].get("user") - self.state.db_password = event.relation.data[event.unit].get("root_password") - if self.state.db_host: - self.configure_pod(event) + if not event.unit in event.relation.data: + return + relation_data = event.relation.data[event.unit] + db_host = relation_data.get("host") + db_port = int(relation_data.get("port", 3306)) + db_user = "root" + db_password = relation_data.get("root_password") + + if ( + db_host + and db_port + and db_user + and db_password + and ( + self.state.db_host != db_host + or self.state.db_port != db_port + or self.state.db_user != db_user + or self.state.db_password != db_password + ) + ): + self.state.db_host = db_host + self.state.db_port = db_port + self.state.db_user = db_user + self.state.db_password = db_password + self.on.configure_pod.emit() + + + def _on_db_relation_departed(self, event: EventBase) -> NoReturn: + """Clears data from db relation. + + Args: + event (EventBase): DB relation event. + + """ + self.state.db_host = None + self.state.db_port = None + self.state.db_user = None + self.state.db_password = None + self.on.configure_pod.emit() def _check_settings(self) -> str: """Check if there any settings missing from Keystone configuration.