Skip to content

Commit

Permalink
Replace main with primary
Browse files Browse the repository at this point in the history
  • Loading branch information
amandahla committed Feb 21, 2024
1 parent 3bf9d9f commit c9ff640
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/charms/redis_k8s/v0/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 11 additions & 11 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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"] = ""
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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)

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit c9ff640

Please sign in to comment.