def _list_reservations(mine, requester): def _err_handler(err): msg = f"reservation listing failed: {str(err)}" _error(msg) try: if mine: try: requester = whoami() except Exception: log.info( "whoami returned an error - getting reservations for 'bonfire'" ) # minikube requester = "bonfire" oc("get", "reservation", "--selector", f"requester={requester}") else: if requester: oc("get", "reservation", "--selector", f"requester={requester}") else: oc("get", "reservation") except KeyboardInterrupt as err: log.error("aborted by keyboard interrupt!") _err_handler(err) except TimedOutError as err: log.error("hit timeout error: %s", err) _err_handler(err) except FatalError as err: log.error("hit fatal error: %s", err) _err_handler(err) except Exception as err: log.exception("hit unexpected error!") _err_handler(err)
def _reserve_ns_for_duration(namespace, duration): requester_id = uuid.uuid4() namespace.reserved = True namespace.ready = False namespace.requester = requester_id namespace.duration = duration namespace.expires = None # let the reconciler tell us when it expires namespace.requester_name = whoami() namespace.update() return requester_id
def reserve_namespace(duration, retries, specific_namespace=None, attempt=0): attempt = attempt + 1 ns_name = specific_namespace if specific_namespace else "" log.info("attempt [%d] to reserve namespace %s", attempt, ns_name) available_namespaces = get_namespaces(available_only=True) if specific_namespace: available_namespaces = [ ns for ns in available_namespaces if ns.name == specific_namespace ] if not available_namespaces: log.info("all namespaces currently unavailable") if retries and attempt > retries: log.error("maximum retries reached") return None log.info("waiting 60sec before retrying") time.sleep(60) return reserve_namespace(duration, retries, specific_namespace, attempt=attempt) namespace = random.choice(available_namespaces) requester_id = uuid.uuid4() namespace.reserved = True namespace.ready = False namespace.requester = requester_id namespace.duration = duration namespace.expires = None # let the reconciler tell us when it expires namespace.requester_name = whoami() namespace.update() # to avoid race conditions, wait and verify we still own this namespace time.sleep(RESERVATION_DELAY_SEC) namespace.refresh() if str(namespace.requester) != str(requester_id): log.warning("hit namespace reservation conflict") if retries and attempt > retries: log.error("maximum retries reached") return None return reserve_namespace(duration, retries, specific_namespace, attempt=attempt) return namespace
def process_reservation( name, requester, duration, template_path=None, ): log.info("processing namespace reservation") template_path = Path(template_path if template_path else conf.DEFAULT_RESERVATION_TEMPLATE) if not template_path.exists(): raise FatalError("Reservation template file does not exist: %s", template_path) with template_path.open() as fp: template_data = yaml.safe_load(fp) params = dict() params["NAME"] = name if name else f"bonfire-reservation-{str(uuid.uuid4()).split('-')[0]}" params["DURATION"] = duration if requester is None: try: requester = whoami() except Exception: log.info("whoami returned an error - setting requester to 'bonfire'") # minikube requester = "bonfire" params["REQUESTER"] = requester processed_template = process_template(template_data, params=params) if not processed_template.get("items"): raise FatalError("Processed Reservation template has no items") return processed_template
def owned_by_me(self): return self.requester_name == whoami()
def owned_by_me(self): if on_k8s(): return True return self.requester_name == whoami()