Skip to content

Commit f408dfa

Browse files
committed
kube: Don't crash w/o section_kube_{cpu,memory}
These sections can be missing during an upgrade (for example if generated section goes from v1 to v2). Don't crash in this case, just return an empty result. Change-Id: I742f8f4ee0759f5a55a4326a0aaa83e1ee024c73
1 parent e232cf1 commit f408dfa

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

packages/cmk-plugins/cmk/plugins/kube/agent_based/kube_cpu.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def _check_kube_cpu(
5959
current_timestamp: float,
6060
host_value_store: MutableMapping[str, Any],
6161
) -> CheckResult:
62-
assert section_kube_cpu_resources is not None
62+
if section_kube_cpu_resources is None:
63+
return
64+
6365
yield from check_resource(
6466
params,
6567
performance_cpu(

packages/cmk-plugins/cmk/plugins/kube/agent_based/kube_memory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def check_kube_memory(
5757
section_kube_memory_resources: Resources | None,
5858
section_kube_allocatable_memory_resource: AllocatableResource | None,
5959
) -> CheckResult:
60-
assert section_kube_memory_resources is not None
60+
if section_kube_memory_resources is None:
61+
return
62+
6163
yield from check_resource(
6264
params,
6365
section_kube_performance_memory,

packages/cmk-plugins/tests/cmk/plugins/kube/agent_based/test_kube_cpu.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import itertools
1111

12-
import pytest
1312
from polyfactory.factories.pydantic_factory import ModelFactory
1413

1514
import cmk.plugins.kube.kube
@@ -67,13 +66,14 @@ def test_discovery() -> None:
6766

6867

6968
def test_check_if_no_resources() -> None:
70-
"""Crashing is expected, because section_kube_cpu is only missing, if data from the api
71-
server missing."""
69+
"""
70+
No results expected when section_kube_cpu is missing,
71+
e.g. if the API server is unreachable or during upgrades.
72+
"""
7273
check_result = kube_cpu._check_kube_cpu(
7374
PARAMS, USAGE_SECTION, None, ALLOCATABLE_RESOURCE_SECTION, 1.0, {}
7475
)
75-
with pytest.raises(AssertionError):
76-
list(check_result)
76+
assert list(check_result) == []
7777

7878

7979
def test_performance_cpu() -> None:

packages/cmk-plugins/tests/cmk/plugins/kube/agent_based/test_kube_memory.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,22 @@ def test_check_kube_memory(
303303
),
304304
],
305305
)
306-
def test_crashes_if_no_resources(
306+
def test_no_results_if_no_resources(
307307
section_kube_performance_memory: PerformanceUsage | None,
308308
) -> None:
309-
with pytest.raises(AssertionError):
310-
list(
311-
check_kube_memory(
312-
DEFAULT_PARAMS,
313-
section_kube_performance_memory,
314-
None,
315-
AllocatableResource(context="node", value=35917989.0),
316-
)
309+
"""
310+
No results expected when section_kube_memory is missing,
311+
e.g. if the API server is unreachable or during upgrades.
312+
"""
313+
result = list(
314+
check_kube_memory(
315+
DEFAULT_PARAMS,
316+
section_kube_performance_memory,
317+
None,
318+
AllocatableResource(context="node", value=35917989.0),
317319
)
320+
)
321+
assert result == []
318322

319323

320324
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)