Skip to content
39 changes: 19 additions & 20 deletions krkn/cerberus/setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python
#
# Copyright 2025 The Krkn Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -11,6 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import requests
import sys
Expand All @@ -31,15 +34,26 @@ def set_url(config):
global cerberus_url
cerberus_url = get_yaml_item_value(config["cerberus"],"cerberus_url", "")
global check_application_routes
check_application_routes = \
get_yaml_item_value(config["cerberus"],"check_applicaton_routes","")
# Read correct key first; fall back to legacy misspelled key for older configs
check_application_routes = get_yaml_item_value(
config["cerberus"], "check_application_routes", ""
)
if not check_application_routes:
legacy = get_yaml_item_value(
config["cerberus"], "check_applicaton_routes", ""
)
if legacy:
logging.warning(
"Config key 'check_applicaton_routes' is deprecated and will be "
"removed in a future release. Please rename it to 'check_application_routes'."
)
check_application_routes = legacy

def get_status(start_time, end_time):
"""
Get cerberus status
"""
cerberus_status = True
check_application_routes = False
application_routes_status = True
if cerberus_enabled:
if not cerberus_url:
Expand All @@ -55,7 +69,6 @@ def get_status(start_time, end_time):
# experience downtime during the chaos
if check_application_routes:
application_routes_status, unavailable_routes = application_status(
cerberus_url,
start_time,
end_time
)
Expand Down Expand Up @@ -96,26 +109,12 @@ def publish_kraken_status( start_time, end_time):
if not cerberus_status:
if exit_on_failure:
logging.info(
"Cerberus status is not healthy and post action scenarios "
"are still failing, exiting kraken run"
)
sys.exit(1)
else:
logging.info(
"Cerberus status is not healthy and post action scenarios "
"are still failing"
)
else:
if exit_on_failure:
logging.info(
"Cerberus status is healthy but post action scenarios "
"are still failing, exiting kraken run"
"Cerberus status is not healthy, exiting kraken run"
)
sys.exit(1)
else:
logging.info(
"Cerberus status is healthy but post action scenarios "
"are still failing"
"Cerberus status is not healthy"
)


Expand Down
9 changes: 7 additions & 2 deletions krkn/prometheus/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python
#
# Copyright 2025 The Krkn Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -11,6 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import datetime
Expand Down Expand Up @@ -178,8 +181,10 @@ def critical_alerts(

if not firing_alerts:
logging.info("No critical alerts are firing!!")



return firing_alerts


def metrics(
prom_cli: KrknPrometheus,
elastic: KrknElastic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def run(
targets = random.sample(
targets, network_chaos_config.instance_count
)

if network_chaos_config.execution == "parallel":
self.run_parallel(targets, network_chaos)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python
#
# Copyright 2025 The Krkn Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -11,10 +13,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import json
import logging
import time
import base64
import json

import yaml

Expand Down
2 changes: 2 additions & 0 deletions krkn/utils/HealthChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def run_health_check(self, health_check_config, health_check_telemetry_queue: qu
"status_code": response["status_code"],
"start_timestamp": start_timestamp
}
if response["status_code"] != 200:
if response["status_code"] != 200:
if response_tracker[config["url"]] is not False:
response_tracker[config["url"]] = False
Expand Down Expand Up @@ -106,3 +107,4 @@ def run_health_check(self, health_check_config, health_check_telemetry_queue: qu
else:
logging.info("health checks config is not defined, skipping them")
return self.ret_value
return self.ret_value
42 changes: 37 additions & 5 deletions tests/test_cerberus_setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#!/usr/bin/env python
#
# Copyright 2025 The Krkn Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Test suite for krkn/cerberus/setup.py

Expand Down Expand Up @@ -33,7 +49,7 @@ def test_set_url_with_cerberus_enabled(self):
"cerberus": {
"cerberus_enabled": True,
"cerberus_url": "http://cerberus.example.com",
"check_applicaton_routes": "route1,route2"
"check_application_routes": "route1,route2"
}
}

Expand Down Expand Up @@ -178,12 +194,28 @@ def test_publish_kraken_status_healthy_exit_on_failure_true(self, mock_get_statu
cerberus_setup.exit_on_failure = True
mock_get_status.return_value = True

with self.assertRaises(SystemExit) as cm:
cerberus_setup.publish_kraken_status(0, 100)

self.assertEqual(cm.exception.code, 1)
# Healthy cluster should NOT exit regardless of exit_on_failure
cerberus_setup.publish_kraken_status(0, 100)

mock_get_status.assert_called_once_with(0, 100)

def test_set_url_legacy_misspelled_key_fallback(self):
"""Test set_url falls back to legacy misspelled key check_applicaton_routes"""
config = {
"kraken": {"exit_on_failure": False},
"cerberus": {
"cerberus_enabled": True,
"cerberus_url": "http://cerberus.example.com",
"check_applicaton_routes": "legacy-route" # old misspelled key
}
}

with self.assertLogs(level="WARNING") as log:
cerberus_setup.set_url(config)

self.assertEqual(cerberus_setup.check_application_routes, "legacy-route")
self.assertTrue(any("deprecated" in msg for msg in log.output))

@patch('krkn.cerberus.setup.get_status')
def test_publish_kraken_status_unhealthy_exit_on_failure_false(self, mock_get_status):
"""Test publish_kraken_status when cluster is unhealthy and exit_on_failure is False"""
Expand Down
Loading