From 62b6ef762f7fafa134ca347df3a60d7055a99733 Mon Sep 17 00:00:00 2001 From: Frank Sauerburger <frank@sauerburger.com> Date: Mon, 10 Jun 2024 08:10:35 +0200 Subject: [PATCH] Add dupvc --- k8sutils/dupvc.py | 33 +++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 2 files changed, 34 insertions(+) create mode 100644 k8sutils/dupvc.py diff --git a/k8sutils/dupvc.py b/k8sutils/dupvc.py new file mode 100644 index 0000000..f9ee2fb --- /dev/null +++ b/k8sutils/dupvc.py @@ -0,0 +1,33 @@ +import kr8s +from typing import Annotated +import sys +from io import BytesIO +import typer + +app = typer.Typer() + +@app.command() +def main( + namespace: str = typer.Option(default=kr8s.ALL), + human_readable: Annotated[bool, typer.Option("--human-readable", "-h", help="Human-readable sizes")] = False, +): + """Run `du -s` in all PVCs.""" + pods = kr8s.get("pod", namespace=namespace) + + h_arg = ["-h"] if human_readable else [] + + for pod in pods: + pvc_volumes = {} + + for volume in pod.spec.volumes: + if "persistentVolumeClaim" in volume: + pvc_volumes[volume.name] = volume.persistentVolumeClaim.claimName + + for container in pod.spec.containers: + for mount in container.volumeMounts: + if mount.name in pvc_volumes: + stdout = BytesIO() + + pod.exec(["du", "-s"] + h_arg + [mount.mountPath], stdout=stdout, stderr=stdout, check=False) + stdout.seek(0) + print(pod.namespace, pvc_volumes[mount.name], stdout.read().decode(), end="") diff --git a/pyproject.toml b/pyproject.toml index 09be00b..7e79dd5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,3 +19,4 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] s3auth = 'k8sutils.s3auth:app' softdrain = 'k8sutils.softdrain:app' +dupvc = 'k8sutils.dupvc:app' -- GitLab