Skip to content

Instantly share code, notes, and snippets.

@bronger
Created March 20, 2021 16:48
Show Gist options
  • Save bronger/9bf1a19d336ca2a48eeb8969436da24e to your computer and use it in GitHub Desktop.
Save bronger/9bf1a19d336ca2a48eeb8969436da24e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess, json, argparse
parser = argparse.ArgumentParser(description="Show all Kubernetes objects without limits.")
parser.add_argument("--namespace", metavar="NAMESPACE", help="namespace to look at (default: all namespaces)")
parser.add_argument("--context", help="context to use (default: current context)")
args = parser.parse_args()
extra_options = []
if args.namespace:
extra_options.extend(["--namespace", args.namespace])
else:
extra_options.append("--all-namespaces")
if args.context:
extra_options.extend(["--context", args.context])
def collect_objects(resource_type):
json_data = subprocess.run(["kubectl", "get", resource_type, "-o", "json"] + extra_options,
text=True, capture_output=True, check=True).stdout
for object_ in json.loads(json_data)["items"]:
if resource_type in {"deployment", "job", "daemonset", "statefulset"}:
containers = object_["spec"]["template"]["spec"]["containers"]
elif resource_type == "cronjob":
containers = object_["spec"]["jobTemplate"]["spec"]["template"]["spec"]["containers"]
for container in containers:
if not container["resources"]:
print(object_["metadata"]["namespace"] + "." + resource_type + "." +
object_["metadata"]["name"] + "." + container["name"])
for resource_type in ("deployment", "job", "cronjob", "daemonset", "statefulset"):
collect_objects(resource_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment