Skip to content

Commit 6c7b4ce

Browse files
author
Nitesh
committed
fix(scenarios): fix cerberus call signature, missing imports, prometheus return, HealthChecker bool flags, network_chaos_ng len check
1 parent 6da7c9d commit 6c7b4ce

File tree

5 files changed

+111
-46
lines changed

5 files changed

+111
-46
lines changed

krkn/cerberus/setup.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2025 The Krkn Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
117
import logging
218
import requests
319
import sys
@@ -19,14 +35,13 @@ def set_url(config):
1935
cerberus_url = get_yaml_item_value(config["cerberus"],"cerberus_url", "")
2036
global check_application_routes
2137
check_application_routes = \
22-
get_yaml_item_value(config["cerberus"],"check_applicaton_routes","")
38+
get_yaml_item_value(config["cerberus"],"check_application_routes","")
2339

2440
def get_status(start_time, end_time):
2541
"""
2642
Get cerberus status
2743
"""
2844
cerberus_status = True
29-
check_application_routes = False
3045
application_routes_status = True
3146
if cerberus_enabled:
3247
if not cerberus_url:
@@ -42,7 +57,6 @@ def get_status(start_time, end_time):
4257
# experience downtime during the chaos
4358
if check_application_routes:
4459
application_routes_status, unavailable_routes = application_status(
45-
cerberus_url,
4660
start_time,
4761
end_time
4862
)
@@ -83,26 +97,12 @@ def publish_kraken_status( start_time, end_time):
8397
if not cerberus_status:
8498
if exit_on_failure:
8599
logging.info(
86-
"Cerberus status is not healthy and post action scenarios "
87-
"are still failing, exiting kraken run"
88-
)
89-
sys.exit(1)
90-
else:
91-
logging.info(
92-
"Cerberus status is not healthy and post action scenarios "
93-
"are still failing"
94-
)
95-
else:
96-
if exit_on_failure:
97-
logging.info(
98-
"Cerberus status is healthy but post action scenarios "
99-
"are still failing, exiting kraken run"
100+
"Cerberus status is not healthy, exiting kraken run"
100101
)
101102
sys.exit(1)
102103
else:
103104
logging.info(
104-
"Cerberus status is healthy but post action scenarios "
105-
"are still failing"
105+
"Cerberus status is not healthy"
106106
)
107107

108108

krkn/prometheus/client.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2025 The Krkn Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
117
from __future__ import annotations
218

319
import datetime
@@ -165,8 +181,10 @@ def critical_alerts(
165181

166182
if not firing_alerts:
167183
logging.info("No critical alerts are firing!!")
168-
169-
184+
185+
return firing_alerts
186+
187+
170188
def metrics(
171189
prom_cli: KrknPrometheus,
172190
elastic: KrknElastic,
@@ -243,37 +261,37 @@ def metrics(
243261
pass
244262
telemetry_json = json.loads(telemetry_json)
245263
for scenario in telemetry_json['scenarios']:
246-
for k,v in scenario["affected_pods"].items():
264+
for pod_type, pod_list in scenario["affected_pods"].items():
247265
metric_name = "affected_pods_recovery"
248-
metric = {"metricName": metric_name, "type": k}
249-
if type(v) is list:
250-
for pod in v:
251-
for k,v in pod.items():
252-
metric[k] = v
266+
metric = {"metricName": metric_name, "type": pod_type}
267+
if type(pod_list) is list:
268+
for pod in pod_list:
269+
for pod_k, pod_v in pod.items():
270+
metric[pod_k] = pod_v
253271
metric['timestamp'] = str(datetime.datetime.now())
254272
logging.debug("adding pod %s", metric)
255273
metrics_list.append(metric.copy())
256274
for affected_node in scenario["affected_nodes"]:
257275
metric_name = "affected_nodes_recovery"
258276
metric = {"metricName": metric_name}
259-
for k,v in affected_node.items():
260-
metric[k] = v
277+
for node_k, node_v in affected_node.items():
278+
metric[node_k] = node_v
261279
metric['timestamp'] = str(datetime.datetime.now())
262280
metrics_list.append(metric.copy())
263281
if telemetry_json['health_checks']:
264282
for health_check in telemetry_json["health_checks"]:
265283
metric_name = "health_check_recovery"
266284
metric = {"metricName": metric_name}
267-
for k,v in health_check.items():
268-
metric[k] = v
285+
for hc_k, hc_v in health_check.items():
286+
metric[hc_k] = hc_v
269287
metric['timestamp'] = str(datetime.datetime.now())
270288
metrics_list.append(metric.copy())
271289
if telemetry_json['virt_checks']:
272290
for virt_check in telemetry_json["virt_checks"]:
273291
metric_name = "virt_check_recovery"
274292
metric = {"metricName": metric_name}
275-
for k,v in virt_check.items():
276-
metric[k] = v
293+
for vc_k, vc_v in virt_check.items():
294+
metric[vc_k] = vc_v
277295
metric['timestamp'] = str(datetime.datetime.now())
278296
metrics_list.append(metric.copy())
279297

krkn/scenario_plugins/network_chaos_ng/network_chaos_ng_scenario_plugin.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2025 The Krkn Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
117
import logging
218
import queue
319
import random
@@ -51,19 +67,15 @@ def run(
5167
f"with target type {network_chaos_type}"
5268
)
5369

54-
if (
55-
network_chaos_config.instance_count != 0
56-
and network_chaos_config.instance_count > len(targets)
57-
):
70+
if network_chaos_config.instance_count != 0 and len(targets) > network_chaos_config.instance_count:
5871
targets = random.sample(
5972
targets, network_chaos_config.instance_count
6073
)
61-
6274
if network_chaos_config.execution == "parallel":
6375
self.run_parallel(targets, network_chaos)
6476
else:
6577
self.run_serial(targets, network_chaos)
66-
if len(config) > 1:
78+
if len(scenario_config) > 1:
6779
logging.info(
6880
f"waiting {network_chaos_config.wait_duration} seconds before running the next "
6981
f"Network Chaos NG Module"

krkn/scenario_plugins/zone_outage/zone_outage_scenario_plugin.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
import base64
2-
import json
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2025 The Krkn Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
317
import logging
418
import time
19+
import base64
20+
import json
521

622
import yaml
723

krkn/utils/HealthChecker.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2025 The Krkn Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
117
import requests
218
import time
319
import logging
@@ -6,10 +22,10 @@
622
from krkn_lib.models.telemetry.models import HealthCheck
723

824
class HealthChecker:
9-
current_iterations: int = 0
10-
ret_value = 0
1125
def __init__(self, iterations):
1226
self.iterations = iterations
27+
self.current_iterations: int = 0
28+
self.ret_value = 0
1329

1430
def make_request(self, url, auth=None, headers=None, verify=True):
1531
response_data = {}
@@ -50,8 +66,10 @@ def run_health_check(self, health_check_config, health_check_telemetry_queue: qu
5066
"start_timestamp": start_timestamp
5167
}
5268
if response["status_code"] != 200:
53-
if response_tracker[config["url"]] != False: response_tracker[config["url"]] = False
54-
if config["exit_on_failure"] and config["exit_on_failure"] == True and self.ret_value==0: self.ret_value = 2
69+
if response_tracker[config["url"]] is not False:
70+
response_tracker[config["url"]] = False
71+
if config["exit_on_failure"] is True and self.ret_value == 0:
72+
self.ret_value = 2
5573
else:
5674
if response["status_code"] != health_check_tracker[config["url"]]["status_code"]:
5775
end_timestamp = datetime.now()
@@ -86,4 +104,5 @@ def run_health_check(self, health_check_config, health_check_telemetry_queue: qu
86104

87105
health_check_telemetry_queue.put(health_check_telemetry)
88106
else:
89-
logging.info("health checks config is not defined, skipping them")
107+
logging.info("health checks config is not defined, skipping them")
108+
return self.ret_value

0 commit comments

Comments
 (0)