Add ex_list_nodes method
import datetime
import json
+import hashlib
from libcloud.container.base import (
Container,
@@ -30,7 +31,16 @@ from libcloud.common.kubernetes import KubernetesDriverMixin
from libcloud.container.providers import Provider
from libcloud.container.types import ContainerState
-__all__ = ["KubernetesContainerDriver"]
+from libcloud.compute.types import NodeState
+from libcloud.compute.base import Node
+from libcloud.compute.base import NodeDriver
+from libcloud.compute.base import NodeSize
+from libcloud.compute.base import NodeImage
+from libcloud.compute.base import NodeLocation
+
+__all__ = [
+ 'KubernetesContainerDriver'
+]
ROOT_URL = "/api/"
@@ -214,6 +224,68 @@ class KubernetesContainerDriver(KubernetesDriverMixin, ContainerDriver):
"""
return self.ex_destroy_pod(container.extra["namespace"], container.extra["pod"])
+ def ex_list_nodes(self):
+ """
+ List available Nodes
+
+ :rtype: ``list`` of :class:`.Node`
+ """
+ result = self.connection.request(ROOT_URL + "v1/nodes").object
+ return [self._to_node(node) for node in result['items']]
+
+ def _to_node(self, node):
+ """
+ Convert an API node to a `Node` object
+ """
+ ID = node['metadata']['uid']
+ name = node['metadata']['name']
+ driver = self.connection.driver
+ namespace = 'undefined'
+ memory = node['status'].get('capacity', {}).get('memory', 0)
+ if not isinstance(memory, int):
+ if 'Ki' in memory:
+ memory = memory.rstrip('Ki')
+ memory = int(memory) * 1024
+ elif 'K' in memory:
+ memory = memory.rstrip('K')
+ memory = int(memory) * 1000
+ elif 'M' in memory or 'Mi' in memory:
+ memory = memory.rstrip('M')
+ memory = memory.rstrip('Mi')
+ memory = int(memory)
+ elif 'Gi' in memory:
+ memory = memory.rstrip('Gi')
+ memory = int(memory) // 1024
+ elif 'G' in memory:
+ memory = memory.rstrip('G')
+ memory = int(memory) // 1000
+ cpu = node['status'].get('capacity', {}).get('cpu', 1)
+ if not isinstance(cpu, int):
+ cpu = int(cpu.rstrip('m'))
+ extra_size = {'cpus': cpu}
+ size_name = f'{cpu} vCPUs, {memory}MB Ram'
+ size_id = hashlib.md5(size_name.encode("utf-8")).hexdigest()
+ size = NodeSize(id=size_id, name=size_name, ram=memory,
+ disk=0, bandwidth=0, price=0,
+ driver=driver, extra=extra_size)
+ extra = {'memory': memory, 'cpu': cpu}
+ # TODO: Find state
+ state = NodeState.UNKNOWN
+ public_ips, private_ips = [], []
+ for address in node['status']['addresses']:
+ if address['type'] == 'InternalIP':
+ private_ips.append(address['address'])
+ elif address['type'] == 'ExternalIP':
+ public_ips.append(address['address'])
+ created_at = datetime.datetime.strptime(
+ node['metadata']['creationTimestamp'],
+ '%Y-%m-%dT%H:%M:%SZ')
+ return Node(id=ID, name=name, state=state,
+ public_ips=public_ips,
+ private_ips=private_ips,
+ driver=driver, size=size,
+ extra=extra, created_at=created_at)
+
def ex_list_pods(self):
"""
List available Pods
