Skip to content

Commit

Permalink
Allow passing custom plural suffix to new_class() (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Jun 3, 2024
1 parent 20fb01f commit 21c5c40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,7 @@ def new_class(
namespaced=True,
scalable: Optional[bool] = None,
scalable_spec: Optional[str] = None,
plural_suffix: str = "s",
) -> Type[APIObject]:
"""Create a new APIObject subclass.
Expand All @@ -1617,6 +1618,9 @@ def new_class(
version: The Kubernetes API version.
asyncio: Whether to use asyncio or not.
namespaced: Whether the resource is namespaced or not.
scalable: Whether the resource is scalable or not.
scalable_spec: The name of the field to use for scaling.
plural_suffix: The suffix to use for the plural form of the resource.
Returns:
A new APIObject subclass.
Expand All @@ -1632,8 +1636,8 @@ def new_class(
"kind": kind,
"version": version,
"_asyncio": asyncio,
"endpoint": kind.lower() + "s",
"plural": kind.lower() + "s",
"endpoint": kind.lower() + plural_suffix,
"plural": kind.lower() + plural_suffix,
"singular": kind.lower(),
"namespaced": namespaced,
"scalable": scalable or False,
Expand Down
12 changes: 12 additions & 0 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,15 @@ def test_sync_new_class_is_sync():
instance = MyResource({})
assert not instance._asyncio
assert not inspect.iscoroutinefunction(instance.create)


def test_new_class_plural_suffix():
MyPlural = new_class(
kind="MyPlural",
version="newclass.example.com/v1",
namespaced=True,
plural_suffix="es",
)
instance = MyPlural({})
assert instance.plural.endswith("es")
assert instance.endpoint.endswith("es")

0 comments on commit 21c5c40

Please sign in to comment.