File tree Expand file tree Collapse file tree 3 files changed +24
-4
lines changed
tests/cmk/plugins/kube/agent_based Expand file tree Collapse file tree 3 files changed +24
-4
lines changed Original file line number Diff line number Diff line change 44# conditions defined in the file COPYING, which is part of this source code package.
55
66
7+ import json
78import time
8- from json import loads
99
1010from cmk .agent_based .v2 import AgentSection , StringTable
11- from cmk .plugins .kube .schemata .section import StartTime
1211from cmk .plugins .lib .uptime import Section
1312
1413
1514def _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
2127def parse_kube_start_time (string_table : StringTable ) -> Section | None :
Original file line number Diff line number Diff line change @@ -598,7 +598,12 @@ class CollectorDaemons(Section):
598598
599599
600600class 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
Original file line number Diff line number Diff line change 55
66
77from 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
810from cmk .plugins .lib .uptime import Section
911
1012
1113def 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+ )
You can’t perform that action at this time.
0 commit comments