Skip to content

Commit

Permalink
Parse nodes to kubenode objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quving committed Apr 2, 2020
1 parent 1723d05 commit a5f28ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class KubenodeCannotBeParsedFromJson(Exception):
pass
27 changes: 27 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json


class KubeNode:
def __init__(self, node_json):
# Parse meta informations
self.name = node_json.metadata.name
self.uid = node_json.metadata.uid

# Parse node conditions
for condition_json in node_json.status.conditions:
type = condition_json.type
if type == 'MemoryPressure':
self.memory_pressure = condition_json.status == 'True'

if type == 'DiskPressure':
self.disk_pressure = condition_json.status == 'True'

if type == 'PIDPressure':
self.pid_pressure = condition_json.status == 'True'

if type == 'Ready':
self.ready = condition_json.status == 'True'

def __str__(self):
attrs = {str(i): getattr(self, i) for i in dir(self) if not i.startswith('__')}
return json.dumps(attrs)
8 changes: 7 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from kubernetes import client, config

from models import KubeNode

label_selector = 'grid=testing'

# Configs can be set in Configuration class directly or using helper utility
Expand All @@ -8,5 +10,9 @@
print("Listing pods with their IPs:")
ret = v1.list_node(label_selector=label_selector)

kubenodes = []
for node in ret.items:
print(node.metadata.name)
kubenodes.append(KubeNode(node_json=node))

for kn in kubenodes:
print(kn)

0 comments on commit a5f28ca

Please sign in to comment.