def get_namespaces(available=False, mine=False): """ Look up reservable namespaces in the cluster. available (bool) -- return only namespaces that are ready and not reserved mine (bool) -- return only namespaces owned by current user """ log.debug("get_namespaces(available=%s, mine=%s)", available, mine) all_namespaces = [ Namespace(namespace_data=ns) for ns in get_all_namespaces() ] log.debug("namespaces found:\n%s", "\n".join([str(n) for n in all_namespaces])) # get clowd envs to ensure that ClowdEnvironment is ready for the namespaces env_ready_for_ns = _get_env_ready_status() ephemeral_namespaces = [] for ns in all_namespaces: ns.ready = ns.ready and env_ready_for_ns.get(ns.name, False) if not ns.is_reservable: continue get_all = not mine and not available if get_all or (mine and ns.owned_by_me) or (available and ns.available): ephemeral_namespaces.append(ns) return ephemeral_namespaces
def _get_target_namespace(duration, retries, namespace=None): """Determine the namespace to deploy to. Use ns reservation system if on a cluster that has reservable namespaces. Otherwise the user must specify a namespace with '--namespace' and we assume they have ownership of it. Returns tuple of: (bool indicating whether ns reservation system was used, namespace name) """ # check if we're on a cluster that has reservable namespaces reservable_namespaces = get_namespaces() if reservable_namespaces: ns = _reserve_namespace(duration, retries, namespace) return (True, ns.name) else: # we're not, user has to namespace to deploy to if not namespace: _error(NO_RESERVATION_SYS + ". Use -n/--namespace to specify target namespace") # make sure ns exists on the cluster cluster_namespaces = get_all_namespaces() for cluster_ns in cluster_namespaces: if cluster_ns["metadata"]["name"] == namespace: ns = namespace break else: _error(f"namespace '{namespace}' not found on cluster") return (False, ns)
def get_namespaces(available_only=False, mine=False): all_namespaces = get_all_namespaces() ephemeral_namespaces = [] for ns in all_namespaces: ns = Namespace(namespace_data=ns) if not ns.is_reservable: continue if mine: if ns.owned_by_me: ephemeral_namespaces.append(ns) elif not available_only or ns.available: ephemeral_namespaces.append(ns) return ephemeral_namespaces
def get_namespaces(available=False, mine=False): """ Look up reservable namespaces in the cluster. available (bool) -- return only namespaces that are not reserved mine (bool) -- return only namespaces owned by current user """ log.debug("get_namespaces(available=%s, mine=%s)", available, mine) all_namespaces = [Namespace(namespace_data=ns) for ns in get_all_namespaces()] log.debug("namespaces found:\n%s", "\n".join([str(n) for n in all_namespaces])) ephemeral_namespaces = [] for ns in all_namespaces: if not ns.is_reservable: continue get_all = not mine and not available if get_all or (mine and ns.owned_by_me) or (available and ns.available): ephemeral_namespaces.append(ns) return ephemeral_namespaces
def _cmd_config_deploy( app_names, source, get_dependencies, set_image_tag, ref_env, target_env, set_template_ref, set_parameter, clowd_env, local_config_path, remove_resources, single_replicas, namespace, duration, retries, timeout, no_release_on_fail, ): """Process app templates and deploy them to a cluster""" requested_ns = namespace ns = None oc_login() successfully_reserved_ns = False reservable_namespaces = get_namespaces() if reservable_namespaces: # check if we're on a cluster that has reservable namespaces log.info( "reserving ephemeral namespace%s...", f" '{requested_ns}'" if requested_ns else "", ) ns = _reserve_namespace(duration, retries, requested_ns) successfully_reserved_ns = True else: # we're not, user will have to specify namespace to deploy to if not requested_ns: _error("no reservable namespaces found on this cluster. '--namespace' is required") # make sure namespace exists on the cluster cluster_namespaces = get_all_namespaces() for cluster_ns in cluster_namespaces: if cluster_ns["metadata"]["name"] == requested_ns: ns = requested_ns break else: _error(f"namespace '{requested_ns}' not found on cluster") if not clowd_env: # if no ClowdEnvironment name provided, see if a ClowdEnvironment is associated with this ns match = find_clowd_env_for_ns(ns) if not match: _error( f"could not find a ClowdEnvironment tied to ns '{ns}'. Specify one with " "'--clowd-env' or apply one with 'bonfire deploy-env'" ) clowd_env = match["metadata"]["name"] log.debug("inferred clowd_env: '%s'", clowd_env) try: log.info("processing app templates...") apps_config = _process( app_names, source, get_dependencies, set_image_tag, ref_env, target_env, set_template_ref, set_parameter, clowd_env, local_config_path, remove_resources, single_replicas, ) log.debug("app configs:\n%s", json.dumps(apps_config, indent=2)) if not apps_config["items"]: log.warning("no configurations found to apply!") else: log.info("applying app configs...") apply_config(ns, apps_config) log.info("waiting on resources...") _wait_on_namespace_resources(ns, timeout) except (Exception, KeyboardInterrupt): log.exception("hit unexpected error!") try: if not no_release_on_fail and not requested_ns and successfully_reserved_ns: # if we auto-reserved this ns, auto-release it on failure unless # --no-release-on-fail was requested log.info("releasing namespace '%s'", ns) release_namespace(ns) finally: _error("deploy failed") else: log.info("successfully deployed to %s", ns) print(ns)