-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and Delete objects from files (#166)
- Loading branch information
1 parent
2e5e5cb
commit 842f760
Showing
7 changed files
with
198 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023, Dask Developers, NVIDIA | ||
# SPDX-License-Identifier: BSD 3-Clause License | ||
|
||
import typer | ||
from rich.console import Console | ||
|
||
import kr8s.asyncio | ||
from kr8s.asyncio.objects import objects_from_files | ||
|
||
console = Console() | ||
|
||
|
||
# Missing Options | ||
# TODO --allow-missing-template-keys='true' | ||
# TODO --dry-run='none' | ||
# TODO --edit='false' | ||
# TODO --field-manager='' | ||
# TODO -k, --kustomize='' | ||
# TODO -o, --output='' | ||
# TODO --raw='false' | ||
# TODO -R, --recursive='false' | ||
# TODO --save-config='false' | ||
# TODO -l, --selector='' | ||
# TODO --show-managed-fields='false' | ||
# TODO --template='' | ||
# TODO --validate='true' | ||
# TODO --windows-line-endings='false' | ||
|
||
|
||
async def create( | ||
filename: str = typer.Option( | ||
"", | ||
"--filename", | ||
"-f", | ||
help="Filename, directory, or URL to files identifying the resources to create", | ||
), | ||
): | ||
api = await kr8s.asyncio.api() | ||
try: | ||
objs = await objects_from_files(filename, api) | ||
except Exception as e: | ||
console.print(f"[red]Error loading objects from {filename}[/red]: {e}") | ||
raise typer.Exit(1) | ||
for obj in objs: | ||
try: | ||
await obj.create() | ||
except Exception as e: | ||
console.print(f"[red]Error creating {obj}[/red]: {e}") | ||
raise typer.Exit(1) | ||
console.print(f'[green]{obj.singular} "{obj}" created [/green]') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023, Dask Developers, NVIDIA | ||
# SPDX-License-Identifier: BSD 3-Clause License | ||
|
||
import anyio | ||
import typer | ||
from rich.console import Console | ||
|
||
import kr8s.asyncio | ||
from kr8s.asyncio.objects import objects_from_files | ||
|
||
console = Console() | ||
|
||
|
||
# Missing Options | ||
# TODO --all=false | ||
# TODO -A, --all-namespaces=false | ||
# TODO --cascade='background' | ||
# TODO --dry-run='none' | ||
# TODO --field-selector='' | ||
# TODO --force=false | ||
# TODO --grace-period=-1 | ||
# TODO --ignore-not-found=false | ||
# TODO -k, --kustomize='' | ||
# TODO --now='false' | ||
# TODO -o, --output='' | ||
# TODO --raw='false' | ||
# TODO -R, --recursive='false' | ||
# TODO -l, --selector='' | ||
# TODO --timeout='0s' | ||
# TODO --wait='true' | ||
|
||
|
||
async def delete( | ||
filename: str = typer.Option( | ||
"", | ||
"--filename", | ||
"-f", | ||
help="Filename, directory, or URL to files identifying the resources to delete", | ||
), | ||
wait: bool = typer.Option( | ||
True, | ||
"--wait", | ||
help="If true, wait for resources to be gone before returning. This waits for finalizers.", | ||
), | ||
): | ||
api = await kr8s.asyncio.api() | ||
try: | ||
objs = await objects_from_files(filename, api) | ||
except Exception as e: | ||
console.print(f"[red]Error loading objects from {filename}[/red]: {e}") | ||
raise typer.Exit(1) | ||
for obj in objs: | ||
try: | ||
await obj.delete() | ||
except Exception as e: | ||
console.print(f"[red]Error deleting {obj}[/red]: {e}") | ||
raise typer.Exit(1) | ||
console.print(f'[green]{obj.singular} "{obj}" deleted [/green]') | ||
async with anyio.create_task_group() as tg: | ||
for obj in objs: | ||
if wait: | ||
tg.start_soon(obj.wait, "delete") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/kubectl-ng/kubectl_ng/tests/resources/simple/nginx_pod_service.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx | ||
labels: | ||
app.kubernetes.io/name: proxy | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:stable | ||
ports: | ||
- containerPort: 80 | ||
name: http-web-svc | ||
|
||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: nginx-service | ||
spec: | ||
selector: | ||
app.kubernetes.io/name: proxy | ||
ports: | ||
- name: name-of-service-port | ||
protocol: TCP | ||
port: 80 | ||
targetPort: http-web-svc |
34 changes: 34 additions & 0 deletions
34
examples/kubectl-ng/kubectl_ng/tests/test_create_delete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pathlib | ||
|
||
from kubectl_ng.cli import app | ||
from typer.testing import CliRunner | ||
|
||
from kr8s.objects import objects_from_files | ||
|
||
runner = CliRunner() | ||
|
||
HERE = pathlib.Path(__file__).parent.absolute() | ||
|
||
|
||
def test_create_and_delete(): | ||
spec = str(HERE / "resources" / "simple" / "nginx_pod_service.yaml") | ||
|
||
objs = objects_from_files(spec) | ||
for obj in objs: | ||
assert not obj.exists() | ||
|
||
result = runner.invoke(app, ["create", "-f", spec]) | ||
assert result.exit_code == 0 | ||
for obj in objs: | ||
assert obj.name in result.stdout | ||
|
||
for obj in objs: | ||
assert obj.exists() | ||
|
||
result = runner.invoke(app, ["delete", "-f", spec]) | ||
assert result.exit_code == 0 | ||
for obj in objs: | ||
assert obj.name in result.stdout | ||
|
||
for obj in objs: | ||
assert not obj.exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters