forked from vitrixLab/AIOpsLab
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinject_operator.py
More file actions
242 lines (222 loc) · 8.41 KB
/
inject_operator.py
File metadata and controls
242 lines (222 loc) · 8.41 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
import yaml
import time
from aiopslab.service.kubectl import KubeCtl
from aiopslab.generators.fault.base import FaultInjector
class K8SOperatorFaultInjector(FaultInjector):
def __init__(self, namespace: str):
self.namespace = namespace
self.kubectl = KubeCtl()
self.kubectl.create_namespace_if_not_exist(namespace)
def _apply_yaml(self, cr_name: str, cr_yaml: dict):
yaml_path = f"/tmp/{cr_name}.yaml"
with open(yaml_path, "w") as file:
yaml.dump(cr_yaml, file)
command = f"kubectl apply -f {yaml_path} -n {self.namespace}"
result = self.kubectl.exec_command(command)
print(f"Injected {cr_name}: {result}")
def _delete_yaml(self, cr_name: str):
yaml_path = f"/tmp/{cr_name}.yaml"
command = f"kubectl delete -f {yaml_path} -n {self.namespace}"
result = self.kubectl.exec_command(command)
print(f"Recovered from misconfiguration {cr_name}: {result}")
def inject_overload_replicas(self):
"""
Injects a TiDB misoperation custom resource.
The misconfiguration sets an unreasonably high number of TiDB replicas.
"""
cr_name = "overload-tidbcluster"
cr_yaml = {
"apiVersion": "pingcap.com/v1alpha1",
"kind": "TidbCluster",
"metadata": {"name": "basic", "namespace": self.namespace},
"spec": {
"version": "v3.0.8",
"timezone": "UTC",
"pvReclaimPolicy": "Delete",
"pd": {
"baseImage": "pingcap/pd",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tikv": {
"baseImage": "pingcap/tikv",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tidb": {
"baseImage": "pingcap/tidb",
"replicas": 100000, # Intentional misconfiguration
"service": {"type": "ClusterIP"},
"config": {},
},
},
}
self._apply_yaml(cr_name, cr_yaml)
def recover_overload_replicas(self):
self.recover_fault("overload-tidbcluster")
def inject_invalid_affinity_toleration(self):
"""
This misoperation specifies an invalid toleration effect.
"""
cr_name = "affinity-toleration-fault"
cr_yaml = {
"apiVersion": "pingcap.com/v1alpha1",
"kind": "TidbCluster",
"metadata": {"name": "basic", "namespace": self.namespace},
"spec": {
"version": "v3.0.8",
"timezone": "UTC",
"pvReclaimPolicy": "Delete",
"pd": {
"baseImage": "pingcap/pd",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tikv": {
"baseImage": "pingcap/tikv",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tidb": {
"baseImage": "pingcap/tidb",
"replicas": 2,
"service": {"type": "ClusterIP"},
"config": {},
"tolerations": [
{
"key": "test-keys",
"operator": "Equal",
"value": "test-value",
"effect": "TAKE_SOME_EFFECT", # Buggy: invalid toleration effect
"tolerationSeconds": 0,
}
],
},
},
}
self._apply_yaml(cr_name, cr_yaml)
def recover_invalid_affinity_toleration(self):
self.recover_fault("affinity-toleration-fault")
def inject_security_context_fault(self):
"""
The fault sets an invalid runAsUser value.
"""
cr_name = "security-context-fault"
cr_yaml = {
"apiVersion": "pingcap.com/v1alpha1",
"kind": "TidbCluster",
"metadata": {"name": "basic", "namespace": self.namespace},
"spec": {
"version": "v3.0.8",
"timezone": "UTC",
"pvReclaimPolicy": "Delete",
"pd": {
"baseImage": "pingcap/pd",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tikv": {
"baseImage": "pingcap/tikv",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tidb": {
"baseImage": "pingcap/tidb",
"replicas": 2,
"service": {"type": "ClusterIP"},
"config": {},
"podSecurityContext": {"runAsUser": -1}, # invalid runAsUser value
},
},
}
self._apply_yaml(cr_name, cr_yaml)
def recover_security_context_fault(self):
self.recover_fault("security-context-fault")
def inject_wrong_update_strategy(self):
"""
This fault specifies an invalid update strategy.
"""
cr_name = "deployment-update-strategy-fault"
cr_yaml = {
"apiVersion": "pingcap.com/v1alpha1",
"kind": "TidbCluster",
"metadata": {"name": "basic", "namespace": self.namespace},
"spec": {
"version": "v3.0.8",
"timezone": "UTC",
"pvReclaimPolicy": "Delete",
"pd": {
"baseImage": "pingcap/pd",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tikv": {
"baseImage": "pingcap/tikv",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tidb": {
"baseImage": "pingcap/tidb",
"replicas": 2,
"service": {"type": "ClusterIP"},
"config": {},
"statefulSetUpdateStrategy": "SomeStrategyForUpdata", # invalid update strategy
},
},
}
self._apply_yaml(cr_name, cr_yaml)
def recover_wrong_update_strategy(self):
self.recover_fault("deployment-update-strategy-fault")
def inject_non_existent_storage(self):
"""
This fault specifies a non-existent storage class.
"""
cr_name = "non-existent-storage-fault"
cr_yaml = {
"apiVersion": "pingcap.com/v1alpha1",
"kind": "TidbCluster",
"metadata": {"name": "basic", "namespace": self.namespace},
"spec": {
"version": "v3.0.8",
"timezone": "UTC",
"pvReclaimPolicy": "Delete",
"pd": {
"baseImage": "pingcap/pd",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
"storageClassName": "ThisIsAStorageClass", # non-existent storage class
},
"tikv": {
"baseImage": "pingcap/tikv",
"replicas": 3,
"requests": {"storage": "1Gi"},
"config": {},
},
"tidb": {
"baseImage": "pingcap/tidb",
"replicas": 2,
"service": {"type": "ClusterIP"},
"config": {},
},
},
}
self._apply_yaml(cr_name, cr_yaml)
def recover_non_existent_storage(self):
self.recover_fault("non-existent-storage-fault")
def recover_fault(self, cr_name: str):
self._delete_yaml(cr_name)
if __name__ == "__main__":
namespace = "tidb-cluster"
tidb_fault_injector = K8SOperatorFaultInjector(namespace)
tidb_fault_injector.inject_overload_replicas()
time.sleep(10)
tidb_fault_injector.recover_overload_replicas()