diff --git a/main.py b/main.py index 1c78d4f..6b1b03b 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,6 @@ # 2. v3) Introduce a status endpoint of the scheduler - # maybe expose a prometheus compatible output -import json import argparse import logging @@ -13,9 +12,8 @@ from collections import Counter from functools import reduce from etcd3_wrapper import Etcd3Wrapper -from ucloud_common.enums import HostStatus -from ucloud_common.vm import VmPool, VMEntry -from ucloud_common.host import HostPool +from ucloud_common.vm import VmPool, VMEntry, VMStatus +from ucloud_common.host import HostPool, HostStatus logging.basicConfig( level=logging.DEBUG, @@ -45,14 +43,14 @@ def remaining_resources(host_specs, vms_specs): def get_suitable_host(vm_specs): - hosts = HOST_POOL.by_status("ALIVE") + hosts = HOST_POOL.by_status(HostStatus.alive) for host in hosts: # Filter them by host_name vms = VM_POOL.by_host(host.key) # Filter them by status - vms = VM_POOL.except_status("REQUESTED_NEW", vms=vms) + vms = VM_POOL.except_status(VMStatus.requested_new, vms) running_vms_specs = [vm.specs for vm in vms] # Accumulate all of their combined specs @@ -109,11 +107,11 @@ def dead_host_mitigation(dead_hosts_keys): def assign_host(vm): host_name = get_suitable_host(vm.specs) if host_name: - if vm.status == "REQUESTED_NEW": - vm.status = "SCHEDULED_DEPLOY" + if vm.status == VMStatus.requested_new: + vm.status = VMStatus.scheduled_deploy - if vm.status == "KILLED": - vm.status = "REQUESTED_START" + if vm.status == VMStatus.killed: + vm.status = VMStatus.requested_start vm.hostname = host_name VM_POOL.put(vm) @@ -134,7 +132,7 @@ def main(vm_prefix, host_prefix): PENDING_VMS = [] for events_iterator in [ client.get_prefix(vm_prefix, value_in_json=True), - client.watch_prefix(vm_prefix, timeout=10, value_in_json=True), + client.watch_prefix(vm_prefix, timeout=5, value_in_json=True), ]: for e in events_iterator: e = VMEntry(e) @@ -157,7 +155,7 @@ def main(vm_prefix, host_prefix): for vm in vm_scheduled: PENDING_VMS.remove(vm) logging.debug(f"Remaining Pending: {PENDING_VMS}") - elif e.status in ["REQUESTED_NEW", "KILLED"]: + elif e.status in [VMStatus.requested_new, VMStatus.killed]: if assign_host(e) is None: PENDING_VMS.append(e) logging.info("No Resource Left. Emailing admin....")