Skip to content

Instantly share code, notes, and snippets.

@kowalski7cc
Last active February 26, 2024 09:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowalski7cc/fa681736e94c95437a100628c49b4575 to your computer and use it in GitHub Desktop.
Save kowalski7cc/fa681736e94c95437a100628c49b4575 to your computer and use it in GitHub Desktop.
Check one or more cluster operator condition status with Python
#!/usr/bin/env python3
import json
import os
from jsonpath_ng import JSONPath
from jsonpath_ng.ext import parse
CO_CMD = "oc get co -o json"
QUERY = "items[?(@.status.conditions[?(@.type=='Degraded' & @.status=='True')])]"
with os.popen(CO_CMD) as co_result:
co_json = json.load(co_result)
assert co_json is not None
result = [match.value for match in parse(QUERY).find(co_json)]
print(
"There are {} degraded operators:\n- {}".format(
len(result), "\n- ".join([item["metadata"]["uid"] for item in result])
)
)
#!/usr/bin/env python3
import json
import os
CO_CMD = "oc get co -o json"
with os.popen(CO_CMD) as co_result:
co_json = json.load(co_result)
assert co_json is not None
items = co_json["items"]
degraded_items = []
for item in items:
conditions = item["status"]["conditions"]
for condition in conditions:
if condition["type"] == "Degraded" and condition["status"] == "True":
degraded_items.append(item)
print(
"There are {} degraded operators:\n- {}".format(
len(degraded_items),
"\n- ".join([item["metadata"]["uid"] for item in degraded_items]),
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment