-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmock_client.py
More file actions
308 lines (281 loc) · 12.1 KB
/
mock_client.py
File metadata and controls
308 lines (281 loc) · 12.1 KB
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
from typing import List, Dict, Optional, cast
import yaml
from k8s_client.client import KubernetesClient
class MockKubernetesClient(KubernetesClient):
"""Implementation of KubernetesClient that uses mock data"""
def __init__(self) -> None:
"""Initialize the mock Kubernetes client"""
print("Using mock Kubernetes client")
# Mock data for resources
self.mock_resources: Dict[str, Dict[str, List[str]]] = {
"default": {
"deployments": ["nginx-deployment", "web-app"],
"services": ["kubernetes", "web-service"],
"configmaps": ["app-config", "system-config"],
"pods": ["nginx-pod-1", "web-app-pod-1", "web-app-pod-2"],
"secrets": ["default-token", "app-secrets"],
"replicasets": ["nginx-rs", "web-app-rs"],
"daemonsets": ["node-exporter"],
"statefulsets": ["database"],
"ingresses": ["web-ingress"],
},
"kube-system": {
"deployments": ["coredns", "metrics-server"],
"services": ["kube-dns", "metrics-server"],
"pods": ["coredns-123456", "metrics-server-789012"],
"configmaps": ["kube-proxy", "kube-dns"],
"secrets": ["kube-system-token"],
},
"kube-public": {
"configmaps": ["cluster-info"],
},
}
# Mock data for pod containers
self.mock_pod_containers: Dict[str, List[str]] = {
"nginx-pod-1": ["nginx", "sidecar"],
"web-app-pod-1": ["web-app", "logging-agent"],
"web-app-pod-2": ["web-app", "logging-agent"],
"coredns-123456": ["coredns"],
"metrics-server-789012": ["metrics-server"],
}
# Mock data for deployment pods
self.mock_deployment_pods: Dict[str, List[str]] = {
"nginx-deployment": ["nginx-pod-1"],
"web-app": ["web-app-pod-1", "web-app-pod-2"],
"coredns": ["coredns-123456"],
"metrics-server": ["metrics-server-789012"],
}
# Mock data for other controllers
self.mock_controller_pods: Dict[str, List[str]] = {
"database": ["database-0", "database-1"],
"node-exporter": ["node-exporter-node1", "node-exporter-node2"],
}
def get_namespaces(self) -> List[str]:
"""Get all namespaces"""
return list(self.mock_resources.keys())
def get_resource_types(self) -> List[str]:
"""Get all supported resource types"""
return [
"services",
"deployments",
"daemonsets",
"statefulsets",
"replicasets",
"configmaps",
"secrets",
"ingresses",
"pods",
]
def get_resources(self, namespace: str, resource_type: str) -> List[str]:
"""Get resources of a specific type in a namespace"""
if namespace in self.mock_resources and resource_type in self.mock_resources[namespace]:
return self.mock_resources[namespace][resource_type]
return []
def get_pods_for_resource(self, namespace: str, resource_type: str, resource_name: str) -> List[str]:
"""Get pods for a resource"""
if resource_type == "pods":
return [resource_name]
# For simplicity, we'll just return a list with a single pod
pod_name = f"{resource_name}-pod-1"
return [pod_name]
def get_containers_for_pod(self, namespace: str, pod_name: str) -> List[str]:
"""Get containers for a pod"""
# For simplicity, we'll just return a list with a couple of containers
if pod_name.endswith("-pod-1"):
return ["main", "sidecar"]
else:
return ["main"]
def get_pod_containers(self, namespace: str, pod_name: str) -> List[str]:
"""Get containers in a pod"""
if pod_name in self.mock_pod_containers:
return self.mock_pod_containers[pod_name]
return []
def is_resource_with_children(self, resource_type: str) -> bool:
"""Check if a resource type can have children (e.g., pods)"""
# Workload controllers and pods have children
return resource_type in ["deployments", "statefulsets", "daemonsets", "replicasets", "pods"]
def get_resource_yaml(self, namespace: str, resource_type: str, resource_name: str) -> Optional[str]:
"""Get YAML definition of a resource"""
# Handle namespace resource
if resource_type == "namespace" and resource_name in self.get_namespaces():
resource_dict = {
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": resource_name
}
}
return cast(str, yaml.dump(resource_dict, default_flow_style=False))
# Special handling for pods with complex names (like those generated by deployments)
if resource_type == "pods" and resource_name not in self.mock_resources.get(namespace, {}).get("pods", []):
# Check if it's a pod with a deployment-like name pattern
# For example: nginx-deployment-7f5569bb7f-vsmbx
for deployment in self.mock_resources.get(namespace, {}).get("deployments", []):
if resource_name.startswith(f"{deployment}-"):
# It's a pod from this deployment, create a mock pod YAML
resource_dict = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": resource_name,
"namespace": namespace,
"labels": {
"app": deployment
},
"ownerReferences": [{
"apiVersion": "apps/v1",
"kind": "ReplicaSet",
"name": f"{deployment}-7f5569bb7f",
"controller": True
}]
},
"spec": {
"containers": [{
"name": deployment.split("-")[0],
"image": f"{deployment.split('-')[0]}:latest"
}]
}
}
return cast(str, yaml.dump(resource_dict, default_flow_style=False))
# Check if resource exists
if namespace in self.mock_resources and resource_type in self.mock_resources[namespace] and resource_name in self.mock_resources[namespace][resource_type]:
# Create a mock YAML definition
resource_dict = {
"apiVersion": self._get_api_version(resource_type),
"kind": self._get_kind(resource_type),
"metadata": {
"name": resource_name,
"namespace": namespace,
"labels": {
"app": resource_name
}
}
}
# Add resource-specific fields
if resource_type == "deployments":
resource_dict["spec"] = {
"replicas": 1,
"selector": {
"matchLabels": {
"app": resource_name
}
},
"template": {
"metadata": {
"labels": {
"app": resource_name
}
},
"spec": {
"containers": [
{
"name": "main",
"image": "nginx:latest",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
elif resource_type == "services":
resource_dict["spec"] = {
"selector": {
"app": resource_name
},
"ports": [
{
"port": 80,
"targetPort": 80
}
]
}
# Special case for kubernetes service
if resource_name == "kubernetes":
resource_dict["spec"] = {
"clusterIP": "10.96.0.1",
"ports": [
{
"name": "https",
"port": 443,
"protocol": "TCP",
"targetPort": 443
}
]
}
elif resource_type == "configmaps":
resource_dict["data"] = {
"key1": "value1",
"key2": "value2"
}
elif resource_type == "secrets":
resource_dict["type"] = "Opaque"
resource_dict["data"] = {
"username": "YWRtaW4=", # base64 encoded "admin"
"password": "cGFzc3dvcmQ=" # base64 encoded "password"
}
elif resource_type == "pods":
resource_dict["spec"] = {
"containers": [
{
"name": "main",
"image": "nginx:latest",
"ports": [
{
"containerPort": 80
}
]
}
]
}
# Add a sidecar container for nginx-pod-1
if resource_name == "nginx-pod-1":
containers = resource_dict["spec"]["containers"] # type: ignore
if isinstance(containers, list):
containers.append({
"name": "sidecar",
"image": "busybox:latest",
"command": ["sh", "-c", "while true; do echo sidecar running; sleep 10; done"]
})
# Return YAML representation
return cast(str, yaml.dump(resource_dict, default_flow_style=False))
return None
def _get_api_version(self, resource_type: str) -> str:
"""Get API version for a resource type"""
api_versions = {
"pods": "v1",
"services": "v1",
"deployments": "apps/v1",
"daemonsets": "apps/v1",
"statefulsets": "apps/v1",
"replicasets": "apps/v1",
"configmaps": "v1",
"secrets": "v1",
"ingresses": "networking.k8s.io/v1",
}
return api_versions.get(resource_type, "v1")
def _get_kind(self, resource_type: str) -> str:
"""Get kind for a resource type"""
# Convert from plural to singular and capitalize
if resource_type == "services":
return "Service"
elif resource_type == "deployments":
return "Deployment"
elif resource_type == "daemonsets":
return "DaemonSet"
elif resource_type == "statefulsets":
return "StatefulSet"
elif resource_type == "replicasets":
return "ReplicaSet"
elif resource_type == "configmaps":
return "ConfigMap"
elif resource_type == "secrets":
return "Secret"
elif resource_type == "ingresses":
return "Ingress"
elif resource_type == "pods":
return "Pod"
return resource_type.capitalize()