add --apply flag for create

This commit is contained in:
Waylon S. Walker 2025-04-18 13:38:05 -05:00
parent ae918bf5f2
commit cc425cf812
2 changed files with 62 additions and 7 deletions

View file

@ -1,3 +1,7 @@
## 0.4.0
- create now has --apply to apply the generated manifest to the cluster
## 0.3.0 ## 0.3.0
- created pypi release - created pypi release

View file

@ -144,6 +144,17 @@ def get_pods(
raise typer.Exit(1) raise typer.Exit(1)
def get_namespaces(
namespace=None,
label_selector: str = "app=krayt",
):
config.load_kube_config()
api = client.CoreV1Api()
all_namespaces = [n.metadata.name for n in api.list_namespace().items]
return all_namespaces
def get_pod_spec(pod_name, namespace): def get_pod_spec(pod_name, namespace):
config.load_kube_config() config.load_kube_config()
v1 = client.CoreV1Api() v1 = client.CoreV1Api()
@ -640,6 +651,11 @@ def create(
"--post-init-hooks", "--post-init-hooks",
help="additional hooks to execute at the start of container initialization", help="additional hooks to execute at the start of container initialization",
), ),
apply: bool = typer.Option(
False,
"--apply",
help="Automatically apply the changes instead of just echoing them.",
),
): ):
""" """
Krack open a Krayt dragon! Create an inspector pod to explore what's inside your volumes. Krack open a Krayt dragon! Create an inspector pod to explore what's inside your volumes.
@ -647,16 +663,15 @@ def create(
The inspector will be created in the same namespace as the selected pod. The inspector will be created in the same namespace as the selected pod.
""" """
# For create, we want to list all pods, not just Krayt pods # For create, we want to list all pods, not just Krayt pods
selected_namespace = None selected_namespace = namespace
selected_pod = None selected_pod = clone
if namespace is None and clone is not None and "/" in clone: if namespace is None and clone is not None and "/" in clone:
selected_namespace, selected_pod = clone.split("/", 1) selected_namespace, selected_pod = clone.split("/", 1)
elif namespace is not None and clone is not None:
selected_namespace = namespace
selected_pod = clone
pods = get_pods(namespace, label_selector=None) namepaces = get_namespaces(namespace)
pods = get_pods(namespace, label_selector="app!=krayt")
if not pods: if not pods:
typer.echo("No pods found.") typer.echo("No pods found.")
raise typer.Exit(1) raise typer.Exit(1)
@ -694,7 +709,43 @@ def create(
) )
# Output the job manifest # Output the job manifest
typer.echo(yaml.dump(clean_dict(inspector_job), sort_keys=False)) job_yaml = yaml.dump(clean_dict(inspector_job), sort_keys=False)
if apply:
# # Apply the job to the cluster
# import tempfile
# import subprocess
#
# with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml") as temp_file:
# temp_file.write(job_yaml)
# temp_file.flush()
#
# try:
# typer.echo(
# f"Applying job {job_name} to namespace {selected_namespace}..."
# )
# result = subprocess.run(
# ["kubectl", "apply", "-f", temp_file.name],
# capture_output=True,
# text=True,
# check=True,
# )
# typer.echo(result.stdout)
# typer.echo(f"Successfully created inspector job {job_name}")
# except subprocess.CalledProcessError as e:
# typer.echo(f"Error applying job: {e.stderr}", err=True)
# raise typer.Exit(1)
#
batch_api = client.BatchV1Api()
job = batch_api.create_namespaced_job(
namespace=selected_namespace,
body=inspector_job,
)
print(f"Job {job.metadata.name} created.")
return job
else:
# Just echo the YAML
typer.echo(job_yaml)
@app.command() @app.command()