Skip to content

Commit 77711c2

Browse files
committed
kube_uptime: No Pydantic for deserialization
Due to import times, avoid using Pydantic in places where we have a `parsed_section_name` referencing a non-Kube section, as we do in `kube_uptime`. CMK-29703 Change-Id: Ia32bb67fafe57905ae8baaeb2a5819589935ba5f
1 parent b9c8f40 commit 77711c2

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
# conditions defined in the file COPYING, which is part of this source code package.
55

66

7+
import json
78
import time
8-
from json import loads
99

1010
from cmk.agent_based.v2 import AgentSection, StringTable
11-
from cmk.plugins.kube.schemata.section import StartTime
1211
from cmk.plugins.lib.uptime import Section
1312

1413

1514
def _parse_kube_start_time(now: float, string_table: StringTable) -> Section | None:
1615
if not string_table:
1716
return None
18-
return Section(uptime_sec=now - StartTime(**loads(string_table[0][0])).start_time, message=None)
17+
18+
# We parse this manually (without using the Pydantic model) intentionally.
19+
# Since we have parsed_section_name="uptime" in the AgentSection, this file
20+
# gets imported/evaluated - particularly with the nagios core - for every
21+
# host that has an uptime service. And importing the Pydantic models is
22+
# slow. Don't use this as a good pattern to follow in the Kube plugins.
23+
start_time = json.loads(string_table[0][0])["start_time"]
24+
return Section(uptime_sec=now - start_time, message=None)
1925

2026

2127
def parse_kube_start_time(string_table: StringTable) -> Section | None:

packages/cmk-plugins/cmk/plugins/kube/schemata/section.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,12 @@ class CollectorDaemons(Section):
598598

599599

600600
class StartTime(Section):
601-
"""section: kube_start_time_v1"""
601+
"""section: kube_start_time_v1
602+
603+
Note that for performance reasons (to avoid dragging in pydantic on *every*
604+
host that has an uptime service), we only use this as a serializer, and we
605+
deserialize the JSON manually in kube_uptime, without using Pydantic.
606+
"""
602607

603608
start_time: api.Timestamp
604609

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55

66

77
from cmk.plugins.kube.agent_based.kube_uptime import _parse_kube_start_time
8+
from cmk.plugins.kube.schemata.api import Timestamp
9+
from cmk.plugins.kube.schemata.section import StartTime
810
from cmk.plugins.lib.uptime import Section
911

1012

1113
def test_parse_kube_start_time() -> None:
1214
assert _parse_kube_start_time(1.0, [['{"start_time": 0}']]) == Section(
1315
uptime_sec=1.0, message=None
1416
)
17+
18+
19+
def test_parse_kube_start_time_from_section_model() -> None:
20+
start_time_json = StartTime(start_time=Timestamp(20)).model_dump_json()
21+
assert _parse_kube_start_time(100.0, [[start_time_json]]) == Section(
22+
uptime_sec=80.0, message=None
23+
)

0 commit comments

Comments
 (0)