Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add taint function and taints property to Node #530

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ async def patch(self, patch, *, subresource=None, type=None) -> None:
"""Patch this object in Kubernetes."""
await self.async_patch(patch, subresource=subresource, type=type)

async def async_patch(self, patch: dict, *, subresource=None, type=None) -> None:
async def async_patch(
self, patch: dict | list, *, subresource=None, type=None
) -> None:
"""Patch this object in Kubernetes."""
url = f"{self.endpoint}/{self.name}"
if type == "json":
Expand Down Expand Up @@ -824,6 +826,33 @@ async def uncordon(self) -> None:
"""
await self.async_patch({"spec": {"unschedulable": False}})

async def taint(self, key: str, value: str, *, effect: str) -> None:
"""Taint a node."""
jacobtomlinson marked this conversation as resolved.
Show resolved Hide resolved
if effect.endswith("-"):
effect = effect[:-1]
await self.async_patch(
[
{
"op": "remove",
"path": "/spec/taints",
"value": [{"key": key, "value": value, "effect": effect}],
}
],
type="json",
)
else:
await self.async_patch(
{"spec": {"taints": [{"effect": effect, "key": key, "value": value}]}}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are already taints will this not replace them all with this new taint? We might need to do "op": "add" here instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

Looking at kubectl taint nodes ... my-key=value1:NoSchedule -v10, I see it makes two calls, one to get all the taints, and another one update it. I updated this PR to copy what kubectl does.


@property
def taints(self) -> Box:
"""Labels of the Kubernetes resource."""
try:
return self.raw["spec"]["taints"]
except KeyError:
return Box({})


class PersistentVolumeClaim(APIObject):
"""A Kubernetes PersistentVolumeClaim."""
Expand Down
15 changes: 15 additions & 0 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,21 @@ async def test_node():
await node.uncordon()


async def test_node_taint():
api = await kr8s.asyncio.api()
nodes = [node async for node in api.get("nodes")]
assert len(nodes) > 0
node = nodes[0]

await node.taint(key="key1", value="value1", effect="NoSchedule")
assert any(
taint["key"] == "key1" and taint["value"] == "value1" for taint in node.taints
)

await node.taint(key="key1", value="value1", effect="NoSchedule-")
assert not node.taints


async def test_service_proxy():
api = await kr8s.asyncio.api()
[service] = await api.get("services", "kubernetes")
Expand Down
Loading