def prepare(self): self._client = KubeClient(self._path, self._version)
class Kubevirt(virt.Virt): CONFIG_TYPE = "kubevirt" def __init__(self, logger, config, dest, terminate_event=None, interval=None, oneshot=False): super(Kubevirt, self).__init__(logger, config, dest, terminate_event=terminate_event, interval=interval, oneshot=oneshot) self._path = self.config['kubeconfig'] self._version = self.config['kubeversion'] def prepare(self): self._client = KubeClient(self._path, self._version) def parse_cpu(self, cpu): if cpu.endswith('m'): cpu = int(math.floor(int(cpu[:-1]) / 1000)) return str(cpu) def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ hosts = {} nodes = self._client.get_nodes() vms = self._client.get_vms() for node in nodes['items']: status = node['status'] version = status['nodeInfo']['kubeletVersion'] name = node['metadata']['name'] uuid = status['nodeInfo']['machineID'] if self.config['hypervisor_id'] == 'uuid': host_id = uuid elif self.config['hypervisor_id'] == 'hostname': # set to uuid if hostname not available host_id = uuid for addr in status['addresses']: if addr['type'] == 'Hostname': host_id = addr['address'] facts = { virt.Hypervisor.CPU_SOCKET_FACT: self.parse_cpu(status['allocatable']["cpu"]), virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', # this should be hardware uniqe identifier but k8s api gives us only machineID virt.Hypervisor.SYSTEM_UUID_FACT: status['nodeInfo']['machineID'], virt.Hypervisor.HYPERVISOR_VERSION_FACT: version } hosts[name] = virt.Hypervisor(hypervisorId=host_id, name=name, facts=facts) for vm in vms['items']: spec = vm['spec'] host_name = vm['status']['nodeName'] # a vm is not scheduled on any hosts if host_name is None: continue guest_id = spec['domain']['firmware']['uuid'] # a vm is always in running state status = virt.Guest.STATE_RUNNING hosts[host_name].guestIds.append( virt.Guest(guest_id, self.CONFIG_TYPE, status)) return {'hypervisors': list(hosts.values())}
def prepare(self): self._client = KubeClient(self._path)
class Kubevirt(virt.Virt): CONFIG_TYPE = "kubevirt" def __init__(self, logger, config, dest, terminate_event=None, interval=None, oneshot=False): super(Kubevirt, self).__init__(logger, config, dest, terminate_event=terminate_event, interval=interval, oneshot=oneshot) self._path = self.config['kubeconfig'] def prepare(self): self._client = KubeClient(self._path) def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ hosts = {} nodes = self._client.get_nodes() vms = self._client.get_vms() for node in nodes['items']: status = node['status'] version = status['nodeInfo']['kubeletVersion'] name = node['metadata']['name'] uuid = status['nodeInfo']['machineID'] if self.config['hypervisor_id'] == 'uuid': host_id = uuid elif self.config['hypervisor_id'] == 'hostname': # set to uuid if hostname not available host_id = uuid for addr in status['addresses']: if addr['type'] == 'Hostname': host_id = addr['address'] facts = { virt.Hypervisor.CPU_SOCKET_FACT: status['allocatable']["cpu"], virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', virt.Hypervisor.HYPERVISOR_VERSION_FACT: version } hosts[name] = virt.Hypervisor(hypervisorId=host_id, name=name, facts=facts) for vm in vms['items']: spec = vm['spec'] host_name = vm['status']['nodeName'] # a vm is not scheduled on any hosts if host_name is None: continue guest_id = spec['domain']['firmware']['uuid'] # a vm is always in running state status = virt.Guest.STATE_RUNNING hosts[host_name].guestIds.append(virt.Guest(guest_id, self.CONFIG_TYPE, status)) return {'hypervisors': list(hosts.values())}
class Kubevirt(virt.Virt): CONFIG_TYPE = "kubevirt" def __init__(self, logger, config, dest, terminate_event=None, interval=None, oneshot=False): super(Kubevirt, self).__init__(logger, config, dest, terminate_event=terminate_event, interval=interval, oneshot=oneshot) self._path = self.config['kubeconfig'] def prepare(self): self._client = KubeClient(self._path) def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ hosts = {} nodes = self._client.get_nodes() vms = self._client.get_vms() for node in nodes['items']: status = node['status'] version = status['nodeInfo']['kubeletVersion'] name = node['metadata']['name'] host_id = status['nodeInfo']['machineID'] address = status['addresses'][0]['address'] facts = { virt.Hypervisor.CPU_SOCKET_FACT: status['allocatable']["cpu"], virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', virt.Hypervisor.HYPERVISOR_VERSION_FACT: version } hosts[name] = virt.Hypervisor(hypervisorId=host_id, name=address, facts=facts) for vm in vms['items']: metadata = vm['metadata'] host_name = vm['status']['nodeName'] # a vm is not scheduled on any hosts if host_name is None: continue guest_id = metadata['namespace'] + '/' + metadata['name'] # a vm is always in running state status = virt.Guest.STATE_RUNNING hosts[host_name].guestIds.append( virt.Guest(guest_id, self.CONFIG_TYPE, status)) return {'hypervisors': list(hosts.values())}