import json from datetime import datetime MOCK_PODS = [ { "name": "web-app-f6c49bc8-cxjpf", "phase": "Running", "ready": False, "restarts": 99, "namespace": "default" }, { "name": "worker-777bbb8cb9-b844r", "phase": "Running", "ready": True, "restarts": 0, "namespace": "default" }, { "name": "cache-69689c8c-hq55x", "phase": "Running", "ready": True, "restarts": 0, "namespace": "default" }, ] MOCK_LOGS = "web-app starting...\n[container exits with code 1 — no error message logged]" MOCK_STATUS = { "name": "web-app-f6c49bc8-cxjpf", "phase": "Running", "containers": [ { "name": "web-app", "state": "waiting", "reason": "CrashLoopBackOff", "restart_count": 99, "ready": False, } ], "action_required": True, "diagnosis": [ "CRITICAL: web-app is in CrashLoopBackOff with 99 restarts" ], } MOCK_DEPLOYMENT = { "deployment": "web-app", "namespace": "default", "replicas": {"desired": 1, "ready": 0, "available": 0}, "strategy": "RollingUpdate", "containers": [ { "name": "web-app", "image": "busybox", "command": ["sh", "-c", "echo 'web-app starting...'; sleep 10; exit 1"], "env_vars": [], "liveness_probe": None, "readiness_probe": None, "resources": { "requests": {"cpu": "50m", "memory": "32Mi"}, "limits": {"cpu": "100m", "memory": "64Mi"}, }, } ], "warnings": [ "CRITICAL: container 'web-app' has a command with 'exit 1' — intentional crash detected", "INFO: container 'web-app' uses sleep + exit — demo crash pattern detected", ], "diagnosis": "INTENTIONAL_CRASH", } MOCK_RESTART = { "success": True, "pod": "web-app-f6c49bc8-cxjpf", "namespace": "default", "message": "Pod deleted. Kubernetes will recreate it automatically via ReplicaSet.", "restarts_before": 99, "action": "DELETE_AND_RECREATE", "next_step": "Wait 15-30 seconds then call get_pod_status to verify recovery.", } AUDIT_LOG = [] def run_tool(tool_name: str, params: dict) -> dict: ts = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") entry = { "timestamp": ts, "tool": tool_name, "namespace": params.get("namespace", "default"), "pod": params.get("pod_name", ""), "success": True, } AUDIT_LOG.append(entry) if tool_name == "list_pods": crashed = [p for p in MOCK_PODS if p["restarts"] > 0] return { "namespace": params.get("namespace", "default"), "total": len(MOCK_PODS), "healthy": len(MOCK_PODS) - len(crashed), "crashed": len(crashed), "pods": MOCK_PODS, "attention": [ p["name"] + " (" + str(p["restarts"]) + " restarts)" for p in crashed ], } elif tool_name == "get_pod_status": return MOCK_STATUS elif tool_name == "read_logs": return { "pod": params.get("pod_name", ""), "namespace": params.get("namespace", "default"), "tail_lines": params.get("tail_lines", 50), "logs": MOCK_LOGS, "has_errors": False, "error_patterns_detected": [], } elif tool_name == "describe_deployment": return MOCK_DEPLOYMENT elif tool_name == "restart_service": if not params.get("confirm", False): return { "error": "CONFIRMATION_REQUIRED", "detail": "restart_service is destructive. Set confirm=True to proceed.", "pod": params.get("pod_name", ""), "namespace": params.get("namespace", "default"), } return MOCK_RESTART return {"error": "TOOL_NOT_FOUND", "tool": tool_name} def get_audit_log() -> list: return AUDIT_LOG