Module: check_mk
Branch: master
Commit: 3861de209b788c9e14fd485650ac666a2371dbe5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3861de209b788c…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Mon Dec 3 15:51:36 2018 +0100
agent_kubernetes: simplify the parsing of pods, cpu, and memory
CMK-511
Change-Id: I8d368691cf5a5fefa362b73fc9a2f62b248fe657
---
agents/special/agent_kubernetes | 37 ++++++++++++++-----------------------
1 file changed, 14 insertions(+), 23 deletions(-)
diff --git a/agents/special/agent_kubernetes b/agents/special/agent_kubernetes
index f0a52c6..8b1b88b 100755
--- a/agents/special/agent_kubernetes
+++ b/agents/special/agent_kubernetes
@@ -91,27 +91,15 @@ def setup_logging(verbosity):
logging.basicConfig(level=lvl, format=fmt)
-def parse_int(value):
- # type: (Optional[str]) -> Optional[int]
- if value is None:
- return None
- return int(value)
-
-
def parse_cpu(value):
- # type: (Optional[str]) -> Optional[float]
- if value is None:
- return None
+ # type: (str) -> float
if value.endswith('m'):
return 0.001 * float(value[:-1])
return float(value)
def parse_memory(value):
- # type: (Optional[str]) -> Optional[float]
- if value is None:
- return None
-
+ # type: (str) -> float
if value.endswith('Ki'):
return 1024**1 * float(value[:-2])
if value.endswith('Mi'):
@@ -173,21 +161,21 @@ class Node(Metadata):
@property
def resources(self):
- # type: () -> Dict[str, Dict[str, Optional[float]]]
+ # type: () -> Dict[str, Dict[str, float]]
if not self._status:
return {}
view, capacity, allocatable = {}, self._status.capacity,
self._status.allocatable
if capacity:
view['capacity'] = {
- 'cpu': parse_cpu(capacity.get('cpu')),
- 'memory': parse_memory(capacity.get('memory')),
- 'pods': parse_int(capacity.get('pods')),
+ 'cpu': parse_cpu(capacity.get('cpu', '0.0')),
+ 'memory': parse_memory(capacity.get('memory',
'0.0')),
+ 'pods': int(capacity.get('pods', '0')),
}
if allocatable:
view['allocatable'] = {
- 'cpu': parse_cpu(allocatable.get('cpu')),
- 'memory': parse_memory(allocatable.get('memory')),
- 'pods': parse_int(allocatable.get('pods')),
+ 'cpu': parse_cpu(allocatable.get('cpu', '0.0')),
+ 'memory': parse_memory(allocatable.get('memory',
'0.0')),
+ 'pods': int(allocatable.get('pods', '0')),
}
return view
@@ -277,8 +265,11 @@ class PersistentVolume(Metadata):
@property
def capacity(self):
# type: () -> Optional[float]
- if self._spec and self._spec.capacity:
- return parse_memory(self._spec.capacity.get('storage'))
+ if not self._spec or not self._spec.capacity:
+ return None
+ storage = self._spec.capacity.get('storage')
+ if storage:
+ return parse_memory(storage)
return None
@property