From 0d3d5d321f4b041e086f4eb5834e2c7847b7759f Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Tue, 5 Dec 2023 16:45:34 -0800 Subject: [PATCH] Add support for label selectors in mock list_node Nublado now wants to list nodes with a label selector to support nodeSelector in the prepuller. Add support in the list_node function. Since this requires reconstructing the V1NodeList, store a simple list of nodes in set_nodes_for_test. --- changelog.d/20231205_164453_rra_DM_42029.md | 3 +++ src/safir/testing/kubernetes.py | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 changelog.d/20231205_164453_rra_DM_42029.md diff --git a/changelog.d/20231205_164453_rra_DM_42029.md b/changelog.d/20231205_164453_rra_DM_42029.md new file mode 100644 index 00000000..3b8a636a --- /dev/null +++ b/changelog.d/20231205_164453_rra_DM_42029.md @@ -0,0 +1,3 @@ +### New features + +- Add support for label selectors in the `list_node` method of the Kubernetes mock. diff --git a/src/safir/testing/kubernetes.py b/src/safir/testing/kubernetes.py index 3dc64f51..e67ac6d4 100644 --- a/src/safir/testing/kubernetes.py +++ b/src/safir/testing/kubernetes.py @@ -470,7 +470,7 @@ def set_nodes_for_test(self, nodes: list[V1Node]) -> None: nodes New node list to return. """ - self._nodes = V1NodeList(items=nodes) + self._nodes = nodes # CUSTOM OBJECT API @@ -1805,12 +1805,17 @@ async def read_namespaced_network_policy( # NODE API async def list_node( - self, *, _request_timeout: float | None = None + self, + *, + label_selector: str | None = None, + _request_timeout: float | None = None, ) -> V1NodeList: """List node information. Parameters ---------- + label_selector + Which objects to retrieve. All labels must match. _request_timeout Ignored, accepted for compatibility with the Kubernetes API. @@ -1821,7 +1826,12 @@ async def list_node( if any. """ self._maybe_error("list_node") - return self._nodes + nodes = [ + n + for n in self._nodes + if _check_labels(n.metadata.labels, label_selector) + ] + return V1NodeList(items=nodes) # PERSISTENTVOLUMECLAIM API