File size: 4,109 Bytes
3d898cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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