forked from microsoft/AIOpsLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject_otel.py
84 lines (70 loc) · 3.02 KB
/
inject_otel.py
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
import json
import subprocess
from aiopslab.generators.fault.base import FaultInjector
from aiopslab.service.kubectl import KubeCtl
class OtelFaultInjector(FaultInjector):
def __init__(self, namespace: str):
self.namespace = namespace
self.kubectl = KubeCtl()
self.configmap_name = f"{namespace}-flagd-config"
def inject_fault(self, feature_flag: str):
command = (
f"kubectl get configmap {self.configmap_name} -n {self.namespace} -o json"
)
try:
output = self.kubectl.exec_command(command)
configmap = json.loads(output)
except subprocess.CalledProcessError:
raise ValueError(
f"ConfigMap '{self.configmap_name}' not found in namespace '{self.namespace}'."
)
except json.JSONDecodeError:
raise ValueError(
f"Error decoding JSON for ConfigMap '{self.configmap_name}'."
)
flagd_data = json.loads(configmap["data"]["demo.flagd.json"])
if feature_flag in flagd_data["flags"]:
flagd_data["flags"][feature_flag]["defaultVariant"] = "on"
else:
raise ValueError(
f"Feature flag '{feature_flag}' not found in ConfigMap '{self.configmap_name}'."
)
updated_data = {"demo.flagd.json": json.dumps(flagd_data, indent=2)}
self.kubectl.create_or_update_configmap(
self.configmap_name, self.namespace, updated_data
)
print(f"Fault injected: Feature flag '{feature_flag}' set to 'on'.")
def recover_fault(self, feature_flag: str):
command = (
f"kubectl get configmap {self.configmap_name} -n {self.namespace} -o json"
)
try:
output = self.kubectl.exec_command(command)
configmap = json.loads(output)
except subprocess.CalledProcessError:
raise ValueError(
f"ConfigMap '{self.configmap_name}' not found in namespace '{self.namespace}'."
)
except json.JSONDecodeError:
raise ValueError(
f"Error decoding JSON for ConfigMap '{self.configmap_name}'."
)
flagd_data = json.loads(configmap["data"]["demo.flagd.json"])
if feature_flag in flagd_data["flags"]:
flagd_data["flags"][feature_flag]["defaultVariant"] = "off"
else:
raise ValueError(
f"Feature flag '{feature_flag}' not found in ConfigMap '{self.configmap_name}'."
)
updated_data = {"demo.flagd.json": json.dumps(flagd_data, indent=2)}
self.kubectl.create_or_update_configmap(
self.configmap_name, self.namespace, updated_data
)
print(f"Fault recovered: Feature flag '{feature_flag}' set to 'off'.")
# Example usage:
# if __name__ == "__main__":
# namespace = "astronomy-shop"
# feature_flag = "adServiceFailure"
# injector = OtelFaultInjector(namespace)
# injector.inject_fault(feature_flag)
# injector.recover_fault(feature_flag)