diff --git a/k8sutils/dupvc.py b/k8sutils/dupvc.py new file mode 100644 index 0000000000000000000000000000000000000000..f9ee2fb4635e29ce0fa69649f0336cc27c62cbdb --- /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 09be00b2fb0492ca2d04edc30e07b6a9616678ae..7e79dd5ce54c0f6c6b9a0c14a584f3f738d81ff2 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'