diff --git a/charms/worker/k8s/src/inspector.py b/charms/worker/k8s/src/inspector.py index 1b3f1a18..783b5ea1 100644 --- a/charms/worker/k8s/src/inspector.py +++ b/charms/worker/k8s/src/inspector.py @@ -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. diff --git a/charms/worker/k8s/src/literals.py b/charms/worker/k8s/src/literals.py index 656f95af..950b9edd 100644 --- a/charms/worker/k8s/src/literals.py +++ b/charms/worker/k8s/src/literals.py @@ -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", }, } diff --git a/charms/worker/k8s/src/upgrade.py b/charms/worker/k8s/src/upgrade.py index fc2548f6..4c8efef1 100644 --- a/charms/worker/k8s/src/upgrade.py +++ b/charms/worker/k8s/src/upgrade.py @@ -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.""", ) diff --git a/charms/worker/k8s/tests/unit/test_inspector.py b/charms/worker/k8s/tests/unit/test_inspector.py index 2e532095..cb3c76eb 100644 --- a/charms/worker/k8s/tests/unit/test_inspector.py +++ b/charms/worker/k8s/tests/unit/test_inspector.py @@ -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 @@ -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] diff --git a/charms/worker/k8s/tests/unit/test_upgrade.py b/charms/worker/k8s/tests/unit/test_upgrade.py index edeb5003..60e66bb4 100644 --- a/charms/worker/k8s/tests/unit/test_upgrade.py +++ b/charms/worker/k8s/tests/unit/test_upgrade.py @@ -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 @@ -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):