Skip to content

Commit 967b931

Browse files
Add kubectl-ng cordon/uncordon (#500)
1 parent f083525 commit 967b931

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024, Kr8s Developers (See LICENSE for list)
2+
# SPDX-License-Identifier: BSD 3-Clause License
3+
4+
import typer
5+
from rich.console import Console
6+
7+
from kr8s.asyncio.objects import Node
8+
9+
console = Console()
10+
11+
12+
# Missing Options
13+
# TODO --dry-run='none'
14+
# TODO -l, --selector=''
15+
16+
17+
async def cordon(
18+
node: str = typer.Argument(..., help="NODE"),
19+
):
20+
"""Mark node as unschedulable.
21+
22+
Examples:
23+
# Mark node "foo" as unschedulable
24+
kubectl-ng cordon foo
25+
"""
26+
nodes = [await Node.get(node)]
27+
for node_instance in nodes:
28+
await node_instance.cordon()
29+
console.print(f"node/{node_instance.name} cordoned")
30+
31+
32+
async def uncordon(
33+
node: str = typer.Argument(..., help="NODE"),
34+
):
35+
"""Mark node as schedulable.
36+
37+
Examples:
38+
# Mark node "foo" as schedulable
39+
kubectl-ng uncordon foo
40+
"""
41+
nodes = [await Node.get(node)]
42+
for node_instance in nodes:
43+
await node_instance.uncordon()
44+
console.print(f"node/{node_instance.name} uncordoned")

examples/kubectl-ng/kubectl_ng/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ._api_resources import api_resources
66
from ._api_versions import api_versions
77
from ._config import config
8+
from ._cordon_uncordon import cordon, uncordon
89
from ._create import create
910
from ._delete import delete
1011
from ._exec import kexec
@@ -16,6 +17,8 @@
1617
app = typer.Typer(no_args_is_help=True)
1718
register(app, api_resources)
1819
register(app, api_versions)
20+
register(app, cordon)
21+
register(app, uncordon)
1922
register(app, create)
2023
register(app, delete)
2124
register(app, get)

kr8s/_objects.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
BinaryIO,
1414
List,
1515
Literal,
16+
Sequence,
1617
cast,
1718
)
1819

@@ -21,6 +22,7 @@
2122
import jsonpath
2223
import yaml
2324
from box import Box
25+
from typing_extensions import Self
2426

2527
import kr8s
2628
import kr8s.asyncio
@@ -239,7 +241,7 @@ async def get(
239241
field_selector: str | dict[str, str] | None = None,
240242
timeout: int = 2,
241243
**kwargs,
242-
) -> APIObject:
244+
) -> Self:
243245
"""Get a Kubernetes resource by name or via selectors."""
244246
if api is None:
245247
if cls._asyncio:
@@ -283,6 +285,7 @@ async def get(
283285
raise ValueError(
284286
f"Expected exactly one {cls.kind} object. Use selectors to narrow down the search."
285287
)
288+
assert isinstance(resources[0], cls)
286289
return resources[0]
287290
raise NotFoundError(
288291
f"Could not find {cls.kind} {name} in namespace {namespace}."
@@ -685,7 +688,7 @@ def gen(cls, *args, **kwargs):
685688

686689
# Must be the last method defined due to https://github.com/python/mypy/issues/17517
687690
@classmethod
688-
async def list(cls, **kwargs) -> APIObject | list[APIObject]:
691+
async def list(cls, **kwargs) -> Sequence[Self]:
689692
"""List objects in Kubernetes.
690693
691694
Args:
@@ -695,7 +698,10 @@ async def list(cls, **kwargs) -> APIObject | list[APIObject]:
695698
A list of objects.
696699
"""
697700
api = await kr8s.asyncio.api()
698-
return await api.async_get(kind=cls, **kwargs)
701+
resources = await api.async_get(kind=cls, **kwargs)
702+
if not isinstance(resources, list):
703+
resources = [resources]
704+
return [resource for resource in resources if isinstance(resource, cls)]
699705

700706

701707
## v1 objects

0 commit comments

Comments
 (0)