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 kubectl-ng config current-context #375

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions examples/kubectl-ng/kubectl_ng/_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: Copyright (c) 2024, Kr8s Developers (See LICENSE for list)
# SPDX-License-Identifier: BSD 3-Clause License
import typer

import kr8s

config = typer.Typer(
no_args_is_help=True,
name="config",
help="Modify kubeconfig files.",
)


@config.command(name="current-context", help="Display the current-context")
def config_current_context():
"""Display the current context."""
try:
typer.echo(kr8s.api().auth.kubeconfig.current_context)
except KeyError:
typer.echo("error: current-context is not set")
12 changes: 9 additions & 3 deletions examples/kubectl-ng/kubectl_ng/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ._api_resources import api_resources
from ._api_versions import api_versions
from ._config import config
from ._create import create
from ._delete import delete
from ._exec import kexec
Expand All @@ -26,10 +27,14 @@ def wrapper(*args, **kwargs):
def register(app, func, alias=None):
if asyncio.iscoroutinefunction(func):
func = _typer_async(func)
if alias is not None:
app.command(alias)(func)
if isinstance(func, typer.Typer):
assert alias, "Typer subcommand must have an alias."
app.add_typer(func, name=alias)
else:
app.command()(func)
if alias is not None:
app.command(alias)(func)
else:
app.command()(func)


app = typer.Typer(no_args_is_help=True)
Expand All @@ -41,6 +46,7 @@ def register(app, func, alias=None):
register(app, version)
register(app, wait)
register(app, kexec, "exec")
register(app, config, "config")


def go():
Expand Down
15 changes: 15 additions & 0 deletions examples/kubectl-ng/kubectl_ng/tests/test_kng_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-FileCopyrightText: Copyright (c) 2024, Kr8s Developers (See LICENSE for list)
# SPDX-License-Identifier: BSD 3-Clause License
from kubectl_ng.cli import app
from typer.testing import CliRunner

import kr8s

runner = CliRunner()


def test_create_and_delete():
current_context = kr8s.api().auth.kubeconfig.current_context
result = runner.invoke(app, ["config", "current-context"])
assert result.exit_code == 0
assert current_context in result.stdout
Loading