Skip to content

Commit 204fe65

Browse files
committed
Allow require_resources to match all resources of a kind
require_resources rejected a selector with neither match_name nor match_labels set. Crossplane now treats such a selector as matching all resources of the given apiVersion and kind (crossplane/crossplane#7241). This change relaxes the validation to only reject match_name and match_labels both being set. Signed-off-by: Nic Cope <nicc@rk0n.org>
1 parent 8eaf18b commit 204fe65

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

crossplane/function/response.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ def require_resources( # noqa: PLR0913
163163
namespace: The namespace to search in (optional).
164164
165165
Raises:
166-
ValueError: If both match_name and match_labels are provided, or neither.
166+
ValueError: If both match_name and match_labels are provided.
167167
168168
This tells Crossplane to fetch the specified resources and include them
169169
in the next call to the function in req.required_resources[name].
170+
171+
If neither match_name nor match_labels is provided, all resources of the
172+
given api_version and kind are matched.
170173
"""
171-
if (match_name is None) == (match_labels is None):
172-
msg = "Exactly one of match_name or match_labels must be provided"
174+
if match_name is not None and match_labels is not None:
175+
msg = "match_name and match_labels are mutually exclusive"
173176
raise ValueError(msg)
174177

175178
selector = fnv1.ResourceSelector(

tests/test_response.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ class TestCase:
233233
match_name="worker-1",
234234
),
235235
),
236+
TestCase(
237+
reason="Should match all resources of a kind with no match field.",
238+
rsp=fnv1.RunFunctionResponse(),
239+
name="all-pods",
240+
api_version="v1",
241+
kind="Pod",
242+
match_name=None,
243+
match_labels=None,
244+
namespace="default",
245+
want_selector=fnv1.ResourceSelector(
246+
api_version="v1",
247+
kind="Pod",
248+
namespace="default",
249+
),
250+
),
236251
]
237252

238253
for case in cases:
@@ -270,17 +285,6 @@ def test_require_resources_invalid_args(self) -> None:
270285
match_labels={"app": "test"},
271286
)
272287

273-
# Should raise ValueError if neither match_name nor match_labels are provided
274-
with self.assertRaises(ValueError):
275-
response.require_resources(
276-
rsp,
277-
"test",
278-
"v1",
279-
"Pod",
280-
match_name=None,
281-
match_labels=None,
282-
)
283-
284288
def test_require_schema(self) -> None:
285289
@dataclasses.dataclass
286290
class TestCase:

0 commit comments

Comments
 (0)