-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathownership_tree.py
executable file
·392 lines (334 loc) · 14 KB
/
ownership_tree.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3.11
import json
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
import subprocess
import sys
import argparse
from collections import defaultdict
parser = argparse.ArgumentParser(description="Print a tree of ownership for all resources in a namespace, optionally gather cluster extension state.")
parser.add_argument("namespace", help="The namespace to inspect")
parser.add_argument("--no-events", action="store_true", help="Do not show Events kind grouping")
parser.add_argument("--with-event-info", action="store_true", help="Show additional info (message) for Events")
parser.add_argument("--gather-cluster-extension-state", action="store_true",
help="Gather and save a compressed fingerprint of the cluster extension state to a file.")
parser.add_argument("--no-tree", action="store_true", help="Do not print the tree output (only used if gather-cluster-extension-state is set).")
parser.add_argument("--prompt", action="store_true", help="Create the fingerprint file (if needed) and send it to OpenAI for diagnosis.")
args = parser.parse_args()
NAMESPACE = args.namespace
# If --prompt is used, we also want full info and to gather fingerprint, regardless of other flags
if args.prompt:
args.gather_cluster_extension_state = True
if args.gather_cluster_extension_state:
SHOW_EVENTS = True
WITH_EVENT_INFO = True
else:
SHOW_EVENTS = not args.no_events
WITH_EVENT_INFO = args.with_event_info
def parse_api_resources_line(line):
parts = [p for p in line.split(' ') if p]
if len(parts) < 3:
return None
kind = parts[-1]
namespaced_str = parts[-2].lower()
namespaced = (namespaced_str == "true")
name = parts[0]
return name, namespaced, kind
kind_to_plural = {}
resource_info = []
try:
all_resources_output = subprocess.check_output(["kubectl", "api-resources"], text=True).strip()
lines = all_resources_output.split('\n')
for line in lines[1:]:
if not line.strip():
continue
parsed = parse_api_resources_line(line)
if not parsed:
continue
name, is_namespaced, kind = parsed
if kind not in kind_to_plural:
kind_to_plural[kind] = name
resource_info.append((kind, name, is_namespaced))
except subprocess.CalledProcessError:
pass
uid_to_resource = {}
all_uids = set()
def get_resources_for_type(resource_name, namespaced):
if namespaced:
cmd = ["kubectl", "get", resource_name, "-n", NAMESPACE, "-o", "json", "--ignore-not-found"]
else:
cmd = ["kubectl", "get", resource_name, "-o", "json", "--ignore-not-found"]
try:
items_json = subprocess.check_output(cmd, text=True, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError:
return []
if not items_json.strip():
return []
data = json.loads(items_json)
if "items" not in data:
return []
items = data["items"]
if namespaced:
return items
# cluster-scoped: filter by namespace reference
filtered = []
for item in items:
meta_ns = item.get("metadata", {}).get("namespace")
spec_ns = item.get("spec", {}).get("namespace")
if meta_ns == NAMESPACE or spec_ns == NAMESPACE:
filtered.append(item)
if filtered:
return filtered
# fallback by name
try:
single_json = subprocess.check_output(
["kubectl", "get", resource_name, NAMESPACE, "-o", "json", "--ignore-not-found"],
text=True, stderr=subprocess.DEVNULL
)
if single_json.strip():
single_data = json.loads(single_json)
if "kind" in single_data and "metadata" in single_data:
meta_ns = single_data.get("metadata", {}).get("namespace")
spec_ns = single_data.get("spec", {}).get("namespace")
if meta_ns == NAMESPACE or spec_ns == NAMESPACE:
filtered.append(single_data)
except subprocess.CalledProcessError:
pass
return filtered
# Collect resources
for (kind, plural_name, is_namespaced) in resource_info:
if kind == "Event" and not SHOW_EVENTS:
continue
items = get_resources_for_type(plural_name, is_namespaced)
for item in items:
uid = item["metadata"]["uid"]
k = item["kind"]
nm = item["metadata"]["name"]
owners = [(o["kind"], o["name"], o["uid"]) for o in item["metadata"].get("ownerReferences", [])]
if k == "Event" and not SHOW_EVENTS:
continue
res_entry = {
"kind": k,
"name": nm,
"namespace": NAMESPACE,
"uid": uid,
"owners": owners
}
if k == "Event" and WITH_EVENT_INFO:
res_entry["message"] = item.get("message", "")
uid_to_resource[uid] = res_entry
all_uids.add(uid)
from collections import defaultdict
owner_to_children = defaultdict(list)
for uid, res in uid_to_resource.items():
for (_, _, o_uid) in res["owners"]:
owner_to_children[o_uid].append(uid)
# Identify top-level
top_level = []
for uid, res in uid_to_resource.items():
if len(res["owners"]) == 0:
top_level.append(uid)
else:
all_known = True
for (_, _, o_uid) in res["owners"]:
if o_uid not in uid_to_resource:
all_known = False
break
if not all_known:
top_level.append(uid)
kind_groups = defaultdict(list)
for uid in top_level:
r = uid_to_resource[uid]
if r["kind"] == "Event" and not SHOW_EVENTS:
continue
kind_groups[r["kind"]].append(uid)
pseudo_nodes = {}
for kind, uids_ in kind_groups.items():
if kind == "Event" and not SHOW_EVENTS:
continue
plural = kind_to_plural.get(kind, kind.lower() + "s")
pseudo_uid = f"PSEUDO_{kind.upper()}_NODE"
pseudo_nodes[kind] = pseudo_uid
uid_to_resource[pseudo_uid] = {
"kind": plural.capitalize(),
"name": "",
"namespace": NAMESPACE,
"uid": pseudo_uid,
"owners": []
}
for child_uid in uids_:
owner_to_children[pseudo_uid].append(child_uid)
top_level_kinds = list(pseudo_nodes.values())
def pseudo_sort_key(uid):
r = uid_to_resource[uid]
return (r["kind"].lower(), r["name"].lower())
top_level_kinds.sort(key=pseudo_sort_key)
def resource_sort_key(uid):
r = uid_to_resource[uid]
return (r["kind"].lower(), r["name"].lower())
def print_tree(uid, prefix="", is_last=True):
r = uid_to_resource[uid]
branch = "└── " if is_last else "├── "
if r['name']:
print(prefix + branch + f"{r['kind']}/{r['name']}")
else:
print(prefix + branch + f"{r['kind']}")
if WITH_EVENT_INFO and r['kind'] == "Event" and "message" in r:
child_prefix = prefix + (" " if is_last else "│ ")
print(child_prefix + "└── message: " + r["message"])
children = owner_to_children.get(uid, [])
children.sort(key=resource_sort_key)
child_prefix = prefix + (" " if is_last else "│ ")
for i, c_uid in enumerate(children):
print_tree(c_uid, prefix=child_prefix, is_last=(i == len(children)-1))
def extract_resource_summary(kind, name, namespace):
is_namespaced = (namespace is not None and namespace != "")
cmd = ["kubectl", "get", kind.lower()+"/"+name]
if is_namespaced:
cmd.extend(["-n", namespace])
cmd.extend(["-o", "json", "--ignore-not-found"])
try:
out = subprocess.check_output(cmd, text=True, stderr=subprocess.DEVNULL)
if not out.strip():
return {}
data = json.loads(out)
except subprocess.CalledProcessError:
return {}
summary = {
"kind": data.get("kind", kind),
"name": data.get("metadata", {}).get("name", name),
"namespace": data.get("metadata", {}).get("namespace", namespace)
}
conditions = data.get("status", {}).get("conditions", [])
if conditions:
summary["conditions"] = [
{
"type": c.get("type"),
"status": c.get("status"),
"reason": c.get("reason"),
"message": c.get("message")
} for c in conditions
]
# For pods/deployments, extract container images
if data.get("kind") in ["Pod", "Deployment"]:
images = []
if data["kind"] == "Pod":
containers = data.get("spec", {}).get("containers", [])
for cont in containers:
images.append({"name": cont.get("name"), "image": cont.get("image")})
elif data["kind"] == "Deployment":
containers = data.get("spec", {}).get("template", {}).get("spec", {}).get("containers", [])
for cont in containers:
images.append({"name": cont.get("name"), "image": cont.get("image")})
if images:
summary["containers"] = images
# For Events, show reason and message
if data.get("kind") == "Event":
summary["reason"] = data.get("reason")
summary["message"] = data.get("message")
metadata = data.get("metadata", {})
if metadata.get("labels"):
summary["labels"] = metadata["labels"]
if metadata.get("annotations"):
summary["annotations"] = metadata["annotations"]
return summary
def load_fingerprint(file_path):
with open(file_path, 'r') as f:
return json.load(f)
def generate_prompt(fingerprint):
prompt = """
You are an expert in Kubernetes operations and diagnostics. I will provide you with a JSON file that represents a snapshot ("fingerprint") of the entire state of a Kubernetes namespace focusing on a particular ClusterExtension and all related resources. This fingerprint includes:
- The ClusterExtension itself.
- All resources in the namespace that are either owned by or possibly needed by the ClusterExtension.
- Key details such as resource conditions, event messages, container images (with references), and minimal metadata.
Your task is:
1. Analyze the provided fingerprint to determine if there are any issues with the ClusterExtension, its related resources, or its configuration.
2. If issues are found, provide a diagnosis of what might be wrong and suggest steps to fix them.
3. If no issues appear, acknowledge that the ClusterExtension and its resources seem healthy.
4. Keep your answer concise and action-focused, as the output will be used by a human operator to troubleshoot or confirm the health of their cluster.
**Important Details:**
- The fingerprint might contain events that show what happened in the cluster recently.
- Check conditions of deployments, pods, and other resources to see if they indicate errors or warnings.
- Look at event messages for hints about failures, restarts, or other anomalies.
- Consider if all necessary resources (like ServiceAccounts, ConfigMaps, or other dependencies) are present and seemingly functional.
**BEGIN FINGERPRINT**
{fingerprint}
**END FINGERPRINT**
Please provide a summarized diagnosis and suggested fixes below:
""".format(fingerprint=json.dumps(fingerprint, indent=2))
return prompt
def send_to_openai(prompt, model="gpt-4o"):
try:
if os.getenv("OPENAI_API_KEY") is None:
raise ValueError("OPENAI_API_KEY environment variable is not set.")
response = client.chat.completions.create(model=model,
messages=[{"role": "user", "content": prompt}])
message_content = response.choices[0].message.content
return message_content
except Exception as e:
return f"Error communicating with OpenAI API: {e}"
def gather_fingerprint(namespace):
ce_uids = [uid for uid, res in uid_to_resource.items() if res["kind"] == "ClusterExtension" and res["namespace"] == namespace]
if not ce_uids:
return []
all_images = {}
image_ref_count = 0
def process_resource(uid):
nonlocal image_ref_count
r = uid_to_resource[uid]
k = r["kind"]
nm = r["name"]
ns = r["namespace"]
summary = extract_resource_summary(k, nm, ns)
if "containers" in summary:
new_containers = []
for c in summary["containers"]:
img = c["image"]
if img not in all_images:
image_ref_count += 1
ref_name = f"image_ref_{image_ref_count}"
all_images[img] = ref_name
c["imageRef"] = all_images[img]
del c["image"]
new_containers.append(c)
summary["containers"] = new_containers
return summary
results = []
for ce_uid in ce_uids:
fingerprint = {}
for uid in uid_to_resource:
r = uid_to_resource[uid]
key = f"{r['kind']}/{r['name']}"
fp = process_resource(uid)
fingerprint[key] = fp
if all_images:
fingerprint["_image_map"] = {v: k for k, v in all_images.items()}
ce_name = uid_to_resource[ce_uid]["name"]
fname = f"{ce_name}-state.json"
with open(fname, "w") as f:
json.dump(fingerprint, f, indent=2)
results.append(fname)
return results
state_files = []
if args.gather_cluster_extension_state:
state_files = gather_fingerprint(NAMESPACE)
if not (args.gather_cluster_extension_state and args.no_tree):
for i, uid in enumerate(top_level_kinds):
print_tree(uid, prefix="", is_last=(i == len(top_level_kinds)-1))
if args.gather_cluster_extension_state:
if not state_files:
print("No ClusterExtension found in the namespace, no state file created.", file=sys.stderr)
else:
print("Created state file(s):", ", ".join(state_files))
# If --prompt is used, we already created the fingerprint file. Now load and send to OpenAI.
if args.prompt:
if not state_files:
print("No ClusterExtension found, cannot prompt OpenAI.", file=sys.stderr)
sys.exit(1)
# Assume one ClusterExtension, take the first file
fingerprint_data = load_fingerprint(state_files[0])
prompt = generate_prompt(fingerprint_data)
response = send_to_openai(prompt)
print("\n--- OpenAI Diagnosis ---\n")
print(response)