def handle_worker_type(cfg): min_replicas = cfg["autoscale"]["args"]["min_replicas"] log = logger.bind( worker_type=cfg["worker_type"], provisioner=cfg["provisioner"], deployment_namespace=cfg["deployment_namespace"], deployment_name=cfg["deployment_name"], min_replicas=min_replicas, ) api = get_api(cfg.get("kube_connfig"), cfg.get("kube_connfig_context")) log.info("Handling worker type. Getting the number of running replicas...") running = get_running( api=api, deployment_namespace=cfg["deployment_namespace"], deployment_name=cfg["deployment_name"], ) log = log.bind(running=running) log.info("Calculating capacity") capacity = cfg["autoscale"]["args"]["max_replicas"] - running log = log.bind(capacity=capacity) log.info("Checking pending") queue = Queue({"rootUrl": cfg["root_url"]}) pending = queue.pendingTasks(cfg["provisioner"], cfg["worker_type"])["pendingTasks"] log = log.bind(pending=pending) log.info("Calculated desired replica count") desired = get_new_worker_count(pending, running, cfg["autoscale"]["args"]) log = log.bind(desired=desired) if desired == 0: log.info("Zero replicas needed") if running < min_replicas: log.info("Using min_replicas") adjust_scale(api, min_replicas, cfg["deployment_namespace"], cfg["deployment_name"]) return if desired < 0: log.info(f"Need to remove {abs(desired)} of {running}") target_replicas = running + desired log = log.bind(target_replicas=target_replicas) if target_replicas < 0: log.info("Target is negative, setting to zero") target_replicas = 0 log = log.bind(target_replicas=target_replicas) if target_replicas < min_replicas: log.info("Using min_replicas instead of target") target_replicas = min_replicas adjust_scale(api, target_replicas, cfg["deployment_namespace"], cfg["deployment_name"]) else: adjustment = min([capacity, desired]) log = log.bind(adjustment=adjustment) log.info(f"Need to increase capacity from {running} running by {adjustment}") if capacity <= 0: log.info("Maximum capacity reached") return adjust_scale(api, running + adjustment, cfg["deployment_namespace"], cfg["deployment_name"]) log.info("Done handling worker type")
def test_process_raises(pending, running, args, exception_type): with pytest.raises(exception_type): get_new_worker_count(pending, running, args)
def handle_worker_type(cfg): min_replicas = cfg["autoscale"]["args"]["min_replicas"] log_env = dict( worker_type=cfg["worker_type"], provisioner=cfg["provisioner"], deployment_namespace=cfg["deployment_namespace"], deployment_name=cfg["deployment_name"], min_replicas=min_replicas, ) api = get_api(cfg.get("kube_connfig"), cfg.get("kube_connfig_context")) logger.info( "Handling worker type. Getting the number of running replicas...", extra=log_env) running = get_running( api=api, deployment_namespace=cfg["deployment_namespace"], deployment_name=cfg["deployment_name"], ) log_env["running"] = running logger.info("Calculating capacity", extra=log_env) capacity = cfg["autoscale"]["args"]["max_replicas"] - running log_env["capacity"] = capacity logger.info("Checking pending", extra=log_env) queue = Queue({"rootUrl": cfg["root_url"]}) pending = get_pending(queue, cfg["provisioner"], cfg["worker_type"]) log_env["pending"] = pending logger.info("Calculated desired replica count", extra=log_env) desired = get_new_worker_count(pending, running, cfg["autoscale"]["args"]) log_env["desired"] = desired if desired == 0: logger.info("Zero replicas needed", extra=log_env) if running < min_replicas: logger.info("Using min_replicas", extra=log_env) adjust_scale(api, min_replicas, cfg["deployment_namespace"], cfg["deployment_name"]) return if desired < 0: logger.info("Need to remove %s of %s", abs(desired), running, extra=log_env) target_replicas = running + desired log_env["target_replicas"] = target_replicas if target_replicas < 0: logger.info("Target is negative, setting to zero", extra=log_env) target_replicas = 0 log_env["target_replicas"] = target_replicas if target_replicas < min_replicas: logger.info("Using min_replicas instead of target", extra=log_env) target_replicas = min_replicas log_env["target_replicas"] = target_replicas adjust_scale(api, target_replicas, cfg["deployment_namespace"], cfg["deployment_name"]) else: adjustment = min([capacity, desired]) log_env["adjustment"] = adjustment logger.info("Need to increase capacity from %s running by %s", running, adjustment, extra=log_env) if capacity <= 0: logger.info("Maximum capacity reached", extra=log_env) return adjust_scale(api, running + adjustment, cfg["deployment_namespace"], cfg["deployment_name"]) logger.info("Done handling worker type", extra=log_env)
def test_process(pending, running, args, expected): assert get_new_worker_count(pending, running, args) == expected