-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmounts_v2.py
More file actions
59 lines (49 loc) · 1.78 KB
/
mounts_v2.py
File metadata and controls
59 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import argparse
from kubernetes import client, config
def list_pod_mounts(namespace):
v1 = client.CoreV1Api()
print(f"\nNamespace: {namespace}")
print("=" * 40)
try:
pods = v1.list_namespaced_pod(namespace=namespace)
except client.exceptions.ApiException as e:
print(f"Error fetching pods from {namespace}: {e}")
return
if not pods.items:
print("No pods found.")
return
for pod in pods.items:
print(f"Pod: {pod.metadata.name}")
for container in pod.spec.containers:
print(f" Container: {container.name}")
if container.volume_mounts:
for mount in container.volume_mounts:
print(f" Mount Name: {mount.name}")
print(f" Mount Path: {mount.mount_path}")
print(f" Read Only: {mount.read_only}")
else:
print(" No volume mounts found.")
print("-" * 40)
def main(namespaces):
# Load Kubernetes configuration
try:
config.load_kube_config()
except Exception:
config.load_incluster_config()
v1 = client.CoreV1Api()
if "all" in namespaces:
try:
all_ns = v1.list_namespace()
namespaces = [ns.metadata.name for ns in all_ns.items]
except client.exceptions.ApiException as e:
print(f"Error fetching namespaces: {e}")
return
for ns in namespaces:
list_pod_mounts(ns)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="List pod volume mounts in one or more namespaces.")
parser.add_argument(
"namespaces", nargs="+", help='Space-separated list of namespaces, or "all" for all namespaces'
)
args = parser.parse_args()
main(args.namespaces)