File size: 6,833 Bytes
05a686e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Optional, List, Dict, Any, Literal
from pydantic import BaseModel, Field


class PodStatus(BaseModel):
    name: str
    namespace: str = "default"
    status: Literal["Running", "Pending", "CrashLoopBackOff", "OOMKilled", "Terminating", "Unknown"]
    node: Optional[str] = None
    restarts: int = 0
    cpu_usage: float = 0.0
    mem_usage: float = 0.0
    container_image: str = "nginx:1.21"
    env_vars: Dict[str, str] = Field(default_factory=dict)
    resources: Dict[str, Dict[str, str]] = Field(default_factory=lambda: {"limits": {}, "requests": {}})


class NodeStatus(BaseModel):
    name: str
    status: Literal["Ready", "NotReady", "SchedulingDisabled"] = "Ready"
    cpu_capacity: float = 4.0
    mem_capacity: float = 8192.0
    cpu_usage: float = 0.0
    mem_usage: float = 0.0
    pods: List[str] = Field(default_factory=list)


class DeploymentStatus(BaseModel):
    name: str
    namespace: str = "default"
    desired_replicas: int = 1
    available_replicas: int = 1
    image: str = "nginx:1.21"
    env_vars: List[Dict[str, str]] = Field(default_factory=list)
    resources: Dict[str, Dict[str, str]] = Field(default_factory=lambda: {"limits": {}, "requests": {}})
    hpa: Optional[Dict[str, Any]] = None


class ServiceStatus(BaseModel):
    name: str
    namespace: str = "default"
    service_type: str = "ClusterIP"
    selector: Dict[str, str] = Field(default_factory=dict)
    ports: List[Dict[str, Any]] = Field(default_factory=lambda: [{"port": 80, "targetPort": 80}])
    external_ip: Optional[str] = None
    error_rate: float = 0.0
    latency_p95: float = 0.0


class ConfigMapStatus(BaseModel):
    name: str
    namespace: str = "default"
    data: Dict[str, str] = Field(default_factory=dict)


class HPAStatus(BaseModel):
    name: str
    namespace: str = "default"
    target_deployment: str
    min_replicas: int = 1
    max_replicas: int = 10
    cpu_target_percent: int = 80
    current_replicas: int = 1


class ClusterEvent(BaseModel):
    message: str
    reason: str
    type: Literal["Normal", "Warning"] = "Normal"
    involved_object: str = ""
    first_timestamp: Optional[str] = None
    count: int = 1


class ClusterObservation(BaseModel):
    nodes: List[NodeStatus] = Field(default_factory=list)
    pods: List[PodStatus] = Field(default_factory=list)
    deployments: List[DeploymentStatus] = Field(default_factory=list)
    services: List[ServiceStatus] = Field(default_factory=list)
    configmaps: List[ConfigMapStatus] = Field(default_factory=list)
    hpa: List[HPAStatus] = Field(default_factory=list)
    events: List[ClusterEvent] = Field(default_factory=list)
    step: int = 0
    objective: str = ""
"""
KubeSimEnv Models - Pydantic models for OpenEnv compliance
All typed models are mandatory for OpenEnv spec compliance.
Every endpoint uses these.
"""

from pydantic import BaseModel, Field, AliasChoices
from typing import List, Dict, Any, Optional, Literal
from datetime import datetime


class NodeStatus(BaseModel):
    """Status of a Kubernetes node"""
    name: str
    status: Literal["Ready", "NotReady", "Unknown", "SchedulingDisabled"]
    cpu_capacity: int  # in cores
    mem_capacity: int  # in MB
    cpu_usage: float = Field(ge=0, le=100)  # percentage
    mem_usage: float = Field(ge=0, le=100)  # percentage
    last_updated: str  # ISO timestamp


class PodStatus(BaseModel):
    """Status of a Kubernetes pod"""
    name: str
    status: Literal["Pending", "Running", "Succeeded", "Failed", "Unknown", "CrashLoopBackOff"]
    node: Optional[str] = None
    restarts: int = 0
    cpu_request: int = Field(default=0)  # in millicores
    mem_request: int = Field(default=0)  # in MB
    cpu_limit: Optional[int] = Field(default=None)  # in millicores
    mem_limit: Optional[int] = Field(default=None)  # in MB
    deployment: Optional[str] = None
    last_updated: str  # ISO timestamp


class DeploymentStatus(BaseModel):
    """Status of a Kubernetes deployment"""
    name: str
    desired_replicas: int
    available_replicas: int
    image: str
    last_updated: str  # ISO timestamp


class ServiceStatus(BaseModel):
    """Status of a Kubernetes service"""
    name: str
    type: Literal["ClusterIP", "NodePort", "LoadBalancer", "ExternalName"]
    ports: List[Dict[str, Any]]
    selector: Optional[Dict[str, str]] = None
    cluster_ip: Optional[str] = None
    last_updated: str  # ISO timestamp


class ConfigMapStatus(BaseModel):
    """Status of a Kubernetes ConfigMap"""
    name: str
    data: Dict[str, str]
    last_updated: str  # ISO timestamp


class HPAStatus(BaseModel):
    """Status of a HorizontalPodAutoscaler"""
    name: str
    min_replicas: int
    max_replicas: int
    current_replicas: int
    cpu_target_percent: int
    last_updated: str  # ISO timestamp


class ClusterEvent(BaseModel):
    """Kubernetes-style event"""
    event_id: str
    timestamp: str  # ISO timestamp
    type: Literal["Normal", "Warning"]
    reason: str
    message: str
    involved_object: str


class ClusterObservation(BaseModel):
    """Main observation model - typed cluster snapshot"""
    nodes: List[NodeStatus]
    pods: List[PodStatus]
    deployments: List[DeploymentStatus]
    services: List[ServiceStatus]
    configmaps: List[ConfigMapStatus]
    hpas: List[HPAStatus] = Field(
        default_factory=list,
        validation_alias=AliasChoices("hpa", "hpas")
    )
    events: List[ClusterEvent]
    step: int
    objective: str


class RewardSignal(BaseModel):
    """Reward signal returned by step()"""
    reward: float
    done: bool
    info: Dict[str, Any] = Field(default_factory=dict)


# Action Models - These represent the structured action space
class KubeAction(BaseModel):
    """Base action model"""
    action_type: Literal[
        "scale", "delete_pod", "patch", "rollout_restart", 
        "set_hpa", "drain_node", "describe"
    ]


class ScaleAction(KubeAction):
    """Scale a deployment to a specific replica count"""
    deployment: str
    replicas: int


class DeletePodAction(KubeAction):
    """Delete a specific pod"""
    pod_name: str


class PatchAction(KubeAction):
    """Patch a resource with specific changes"""
    resource_type: Literal["deployment", "pod", "node", "service"]
    name: str
    patch: Dict[str, Any]


class RolloutRestartAction(KubeAction):
    """Restart a deployment rollout"""
    deployment: str


class SetHPAAction(KubeAction):
    """Set HorizontalPodAutoscaler for a deployment"""
    deployment: str
    min_replicas: int
    max_replicas: int
    cpu_target_percent: int


class DrainNodeAction(KubeAction):
    """Drain a node (evict all pods)"""
    node_name: str


class DescribeAction(KubeAction):
    """Describe/get details of a resource"""
    resource_type: Literal["deployment", "pod", "node", "service"]
    name: str