Skip to content

Commit

Permalink
Fix Upgrade Check and Literals (#185)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Dyess <[email protected]>
  • Loading branch information
mateoflorido and addyess authored Nov 22, 2024
1 parent 8320b91 commit 69c2566
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
23 changes: 18 additions & 5 deletions charms/worker/k8s/src/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,27 @@ def get_nodes(self, labels: LabelSelector) -> Optional[List[Node]]:
ClusterInspectorError: If the nodes cannot be retrieved.
"""
client = self._get_client()
unready_nodes = []
try:
for node in client.list(Node, labels=labels):
if node.status != "Ready":
unready_nodes.append(node)

def is_node_not_ready(node: Node) -> bool:
"""Check if a node is not ready.
Args:
node: The node to check.
Returns:
True if the node is not ready, False otherwise.
"""
if not node.status or not node.status.conditions:
return True
return any(
condition.type == "Ready" and condition.status != "True"
for condition in node.status.conditions
)

return [node for node in client.list(Node, labels=labels) if is_node_not_ready(node)]
except ApiError as e:
raise ClusterInspector.ClusterInspectorError(f"Failed to get nodes: {e}") from e
return unready_nodes or None

def verify_pods_running(self, namespaces: List[str]) -> Optional[str]:
"""Verify that all pods in the specified namespaces are running.
Expand Down
4 changes: 2 additions & 2 deletions charms/worker/k8s/src/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
},
# NOTE: Update the dependencies for the k8s-service before releasing.
"k8s_service": {
"dependencies": {"k8s-worker": "^1.31.0"},
"dependencies": {"k8s-worker": "^1.30, < 1.32"},
"name": "k8s",
"upgrade_supported": "^1.30.0",
"upgrade_supported": "^1.30, < 1.32",
"version": "1.31.2",
},
}
2 changes: 1 addition & 1 deletion charms/worker/k8s/src/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def pre_upgrade_check(self) -> None:
if unready_nodes:
raise ClusterNotReadyError(
message="Cluster is not ready for an upgrade",
cause=f"Nodes not ready: {', '.join(unready_nodes)}",
cause=f"Nodes not ready: {', '.join(node.metadata.name for node in unready_nodes)}",
resolution="""Node(s) may be in a bad state.
Please check the node(s) for more information.""",
)
Expand Down
5 changes: 3 additions & 2 deletions charms/worker/k8s/tests/unit/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from inspector import ClusterInspector
from lightkube.core.exceptions import ApiError
from lightkube.models.core_v1 import NodeCondition
from lightkube.resources.core_v1 import Node, Pod


Expand All @@ -25,11 +26,11 @@ def setUp(self):
def test_get_nodes_returns_unready(self):
"""Test that get_nodes returns unready nodes."""
mock_node1 = MagicMock(spec=Node)
mock_node1.status = "Ready"
mock_node1.status.conditions = [NodeCondition(type="Ready", status="True")]
mock_node1.metadata.name = "node1"

mock_node2 = MagicMock(spec=Node)
mock_node2.status = "NotReady"
mock_node2.status.conditions = [NodeCondition(type="Ready", status="False")]
mock_node2.metadata.name = "node2"

self.mock_client.list.return_value = [mock_node1, mock_node2]
Expand Down
8 changes: 5 additions & 3 deletions charms/worker/k8s/tests/unit/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from charms.data_platform_libs.v0.upgrade import ClusterNotReadyError
from inspector import ClusterInspector
from lightkube.models.core_v1 import Node
from lightkube.models.meta_v1 import ObjectMeta
from upgrade import K8sDependenciesModel, K8sUpgrade


Expand Down Expand Up @@ -66,9 +68,9 @@ def test_pre_upgrade_check_unready_nodes(self):
"""Test pre_upgrade_check fails when nodes are not ready."""
self.charm.is_worker = True
self.node_manager.get_nodes.return_value = [
"worker-1",
"worker-2",
"worker-3",
Node(metadata=ObjectMeta(name="worker-1")),
Node(metadata=ObjectMeta(name="worker-2")),
Node(metadata=ObjectMeta(name="worker-3")),
]

with self.assertRaises(ClusterNotReadyError):
Expand Down

0 comments on commit 69c2566

Please sign in to comment.