Skip to content

Commit 4c88418

Browse files
Add upgrade UnitState enum (#233)
Ported from canonical/mysql-router-operator#134
1 parent 50b19ac commit 4c88418

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

src/abstract_charm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def reconcile(self, event=None) -> None: # noqa: C901
254254
# Run before checking `self._upgrade.is_compatible` in case incompatible upgrade was
255255
# forced & completed on all units.
256256
self._upgrade.set_versions_in_app_databag()
257-
if self._upgrade.unit_state == "restarting": # Kubernetes only
257+
if self._upgrade.unit_state is upgrade.UnitState.RESTARTING: # Kubernetes only
258258
if not self._upgrade.is_compatible:
259259
logger.info(
260260
"Upgrade incompatible. If you accept potential *data loss* and *downtime*, you can continue with `resume-upgrade force=true`"
@@ -268,7 +268,7 @@ def reconcile(self, event=None) -> None: # noqa: C901
268268
if not self._upgrade.is_compatible:
269269
self.set_status(event=event)
270270
return
271-
if self._upgrade.unit_state == "outdated":
271+
if self._upgrade.unit_state is upgrade.UnitState.OUTDATED:
272272
if self._upgrade.authorized:
273273
self._upgrade.upgrade_unit(
274274
event=event,
@@ -330,7 +330,7 @@ def reconcile(self, event=None) -> None: # noqa: C901
330330
# Empty waiting status means we're waiting for database requires relation before
331331
# starting workload
332332
if not workload_.status or workload_.status == ops.WaitingStatus():
333-
self._upgrade.unit_state = "healthy"
333+
self._upgrade.unit_state = upgrade.UnitState.HEALTHY
334334
if self._unit_lifecycle.authorized_leader:
335335
self._upgrade.reconcile_partition()
336336
self.set_status(event=event)

src/kubernetes_charm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def _on_stop(self, _) -> None:
286286
if not self._upgrade:
287287
logger.debug("Peer relation missing during stop event")
288288
return
289-
self._upgrade.unit_state = "restarting"
289+
self._upgrade.unit_state = upgrade.UnitState.RESTARTING
290290

291291

292292
if __name__ == "__main__":

src/kubernetes_upgrade.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def _app_workload_container_version(self) -> str:
159159
)
160160
return stateful_set.status.updateRevision
161161

162-
def reconcile_partition(self, *, action_event: ops.ActionEvent = None) -> None:
162+
def reconcile_partition(self, *, action_event: ops.ActionEvent = None) -> None: # noqa: C901
163163
"""If ready, lower partition to upgrade next unit.
164164
165165
If upgrade is not in progress, set partition to 0. (If a unit receives a stop event, it may
@@ -182,8 +182,11 @@ def determine_partition() -> int:
182182
logger.debug(f"{self._peer_relation.data=}")
183183
for upgrade_order_index, unit in enumerate(units):
184184
# Note: upgrade_order_index != unit number
185+
state = self._peer_relation.data[unit].get("state")
186+
if state:
187+
state = upgrade.UnitState(state)
185188
if (
186-
not force and self._peer_relation.data[unit].get("state") != "healthy"
189+
not force and state is not upgrade.UnitState.HEALTHY
187190
) or self._unit_workload_container_versions[
188191
unit.name
189192
] != self._app_workload_container_version:
@@ -227,7 +230,7 @@ def determine_partition() -> int:
227230
# not which units upgrade. Kubernetes may not upgrade a unit even if the partition
228231
# allows it (e.g. if the charm container of a higher unit is not ready). This is
229232
# also applicable `if not force`, but is unlikely to happen since all units are
230-
# "healthy" `if not force`.
233+
# healthy `if not force`.
231234
message = f"Attempting to upgrade unit {self._partition}"
232235
else:
233236
message = f"Upgrade resumed. Unit {self._partition} is upgrading next"

src/upgrade.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import abc
1111
import copy
12+
import enum
1213
import json
1314
import logging
1415
import pathlib
@@ -34,6 +35,15 @@ class PeerRelationNotReady(Exception):
3435
"""Upgrade peer relation not available (to this unit)"""
3536

3637

38+
class UnitState(str, enum.Enum):
39+
"""Unit upgrade state"""
40+
41+
HEALTHY = "healthy"
42+
RESTARTING = "restarting" # Kubernetes only
43+
UPGRADING = "upgrading" # Machines only
44+
OUTDATED = "outdated" # Machines only
45+
46+
3747
class Upgrade(abc.ABC):
3848
"""In-place upgrades"""
3949

@@ -55,13 +65,14 @@ def __init__(self, charm_: ops.CharmBase) -> None:
5565
self._current_versions[version] = pathlib.Path(file_name).read_text().strip()
5666

5767
@property
58-
def unit_state(self) -> typing.Optional[str]:
68+
def unit_state(self) -> typing.Optional[UnitState]:
5969
"""Unit upgrade state"""
60-
return self._unit_databag.get("state")
70+
if state := self._unit_databag.get("state"):
71+
return UnitState(state)
6172

6273
@unit_state.setter
63-
def unit_state(self, value: str) -> None:
64-
self._unit_databag["state"] = value
74+
def unit_state(self, value: UnitState) -> None:
75+
self._unit_databag["state"] = value.value
6576

6677
@property
6778
def is_compatible(self) -> bool:

0 commit comments

Comments
 (0)