From c9ff640a95ccc7135709441c6f88ee1820bbd3cf Mon Sep 17 00:00:00 2001 From: Amanda Hager Lopes de Andrade Katz Date: Wed, 21 Feb 2024 10:17:27 -0300 Subject: [PATCH] Replace main with primary --- lib/charms/redis_k8s/v0/redis.py | 2 +- src/charm.py | 22 +++++++++++----------- src/sentinel.py | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/charms/redis_k8s/v0/redis.py b/lib/charms/redis_k8s/v0/redis.py index 0b05180..da3b3b8 100644 --- a/lib/charms/redis_k8s/v0/redis.py +++ b/lib/charms/redis_k8s/v0/redis.py @@ -148,4 +148,4 @@ def _bind_address(self, event): def _get_main_ip(self) -> str: """Gets the ip of the current redis main process.""" - return socket.gethostbyname(self._charm.current_main) + return socket.gethostbyname(self._charm.current_primary) diff --git a/src/charm.py b/src/charm.py index 1789f1c..97f673c 100755 --- a/src/charm.py +++ b/src/charm.py @@ -88,13 +88,13 @@ def _redis_pebble_ready(self, event) -> None: # In the event of a pod restart on the same node the upgrade event is not fired. # The IP might change, so the data needs to be propagated for relation in self.model.relations[REDIS_REL_NAME]: - relation.data[self.model.unit]["hostname"] = socket.gethostbyname(self.current_main) + relation.data[self.model.unit]["hostname"] = socket.gethostbyname(self.current_primary) def _upgrade_charm(self, event: UpgradeCharmEvent) -> None: """Handle the upgrade_charm event. Check for failover status and update connection information for redis relation and - current_main. + current_primary. Also tries to store the certificates on the redis container, as new `juju attach-resource` will trigger this event. """ @@ -150,10 +150,10 @@ def _leader_elected(self, event) -> None: if not self.get_sentinel_password(): logger.info("Creating sentinel password") self._peers.data[self.app][SENTINEL_PASSWORD_KEY] = self._generate_password() - # NOTE: if current_main is not set yet, the application is being deployed for the + # NOTE: if current_primary is not set yet, the application is being deployed for the # first time. Otherwise, we check for failover in case previous juju leader was redis # master as well. - if self.current_main is None: + if self.current_primary is None: logger.info( "Initial replication, setting leader-host to {}".format(self.unit_pod_hostname) ) @@ -224,7 +224,7 @@ def _peer_relation_changed(self, event): if relations: for relation in relations: relation.data[self.model.unit]["hostname"] = socket.gethostbyname( - self.current_main + self.current_primary ) if self._peers.data[self.unit].get("upgrading", "false") == "true": self._peers.data[self.unit]["upgrading"] = "" @@ -377,8 +377,8 @@ def _redis_extra_flags(self) -> str: ] # Check that current unit is master - if self.current_main != self.unit_pod_hostname: - extra_flags += [f"--replicaof {self.current_main} {REDIS_PORT}"] + if self.current_primary != self.unit_pod_hostname: + extra_flags += [f"--replicaof {self.current_primary} {REDIS_PORT}"] if self.config["enable-tls"]: extra_flags += ["--tls-replication yes"] @@ -455,7 +455,7 @@ def unit_pod_hostname(self, name="") -> str: return socket.getfqdn(name) @property - def current_main(self) -> Optional[str]: + def current_primary(self) -> Optional[str]: """Get the current master.""" return self._peers.data[self.app].get(LEADER_HOST_KEY) @@ -472,7 +472,7 @@ def _valid_app_databag(self) -> bool: if self._peers.data[self.app].get("enable-password", "true") == "false": password = True - return bool(password and self.current_main) + return bool(password and self.current_primary) def _generate_password(self) -> str: """Generate a random 16 character password string. @@ -583,7 +583,7 @@ def _master_up_to_date(self, host="0.0.0.0") -> bool: info = self.sentinel.get_master_info(host=host) if info is None: return False - elif (info["ip"] == self.current_main) and ("s_down" not in info["flags"]): + elif (info["ip"] == self.current_primary) and ("s_down" not in info["flags"]): return True return False @@ -605,7 +605,7 @@ def _sentinel_failover(self, departing_unit_name: str) -> None: This method should only be called from juju leader, to avoid more than one sentinel sending failovers concurrently. """ - if self._k8s_hostname(departing_unit_name) != self.current_main: + if self._k8s_hostname(departing_unit_name) != self.current_primary: # No failover needed return diff --git a/src/sentinel.py b/src/sentinel.py index e99054d..1243f1c 100644 --- a/src/sentinel.py +++ b/src/sentinel.py @@ -110,7 +110,7 @@ def _render_sentinel_config_file(self) -> None: hostname=self.charm.unit_pod_hostname, master_name=self.charm._name, sentinel_port=SENTINEL_PORT, - redis_master=self.charm.current_main, + redis_master=self.charm.current_primary, redis_port=REDIS_PORT, quorum=self.expected_quorum, master_password=self.charm._get_password(),