From e87919dc8bf68dcf9df8598b956cab3c19ca6349 Mon Sep 17 00:00:00 2001 From: Brandon Squizzato <35474886+bsquizz@users.noreply.github.com> Date: Thu, 7 Oct 2021 15:20:59 -0400 Subject: [PATCH] Unset requester when releasing namespace (#127) * Remove reset command, unset the requester when releasing * Formatting * Fix missing 'force' arg --- bonfire/bonfire.py | 60 ++++++++++++++++++++----------------------- bonfire/namespaces.py | 12 ++++----- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/bonfire/bonfire.py b/bonfire/bonfire.py index 18b41072..90cc8816 100755 --- a/bonfire/bonfire.py +++ b/bonfire/bonfire.py @@ -38,7 +38,6 @@ get_namespaces, reserve_namespace, release_namespace, - reset_namespace, add_base_resources, reconcile, ) @@ -539,7 +538,7 @@ def _validate_reservation_duration(ctx, param, value): "-r", type=str, default=None, - help="Name of the user requesting a reservation" + help="Name of the user requesting a reservation", ), click.option( "--namespace", @@ -582,7 +581,7 @@ def inner(func): "-o", default="cli", help="which output format to return the data in", - type=click.Choice(['cli', 'json'], case_sensitive=False) + type=click.Choice(["cli", "json"], case_sensitive=False), ) def _list_namespaces(available, mine, output): """Get list of ephemeral namespaces""" @@ -590,12 +589,12 @@ def _list_namespaces(available, mine, output): if not available and not mine and not namespaces: _error(NO_RESERVATION_SYS) elif not namespaces: - if output == 'json': + if output == "json": click.echo("{}") else: click.echo("no namespaces found") else: - if output == 'json': + if output == "json": data = {} for ns in namespaces: data[ns.name] = { @@ -630,11 +629,19 @@ def _cmd_namespace_reserve(duration, retries, namespace): @namespace.command("release") @click.argument("namespace", required=True, type=str) -def _cmd_namespace_release(namespace): +@click.option( + "-f", + "--force", + is_flag=True, + default=False, + help="Do not check if you own this namespace", +) +def _cmd_namespace_release(namespace, force): """Remove reservation from an ephemeral namespace""" if not get_namespaces(): _error(NO_RESERVATION_SYS) - _warn_if_unsafe(namespace) + if not force: + _warn_if_unsafe(namespace) release_namespace(namespace) @@ -669,13 +676,6 @@ def _cmd_namespace_reconcile(): reconcile() -@namespace.command("reset", hidden=True) -@click.argument("namespace", required=True, type=str) -def _cmd_namespace_reset(namespace): - """Set namespace to not released/not ready (for admin use only)""" - reset_namespace(namespace) - - def _get_apps_config(source, target_env, ref_env, local_config_path): config = conf.load_config(local_config_path) @@ -1126,8 +1126,8 @@ def _cmd_apps_what_depends_on( @reservation.command("create") @click.option( - '--bot', - '-b', + "--bot", + "-b", is_flag=True, help="Use this flag to skip the duplicate reservation check (for automation)", ) @@ -1142,9 +1142,7 @@ def _err_handler(err): res = get_reservation(name) # Name should be unique on reservation creation. if res: - raise FatalError( - f"Reservation with name {name} already exists" - ) + raise FatalError(f"Reservation with name {name} already exists") res_config = process_reservation(name, requester, duration) @@ -1189,11 +1187,11 @@ def _err_handler(err): @reservation.command("extend") @click.option( - '--duration', - '-d', + "--duration", + "-d", type=str, - default='1h', - help='Amount of time to extend the reservation', + default="1h", + help="Amount of time to extend the reservation", callback=_validate_reservation_duration, ) @options(_reservation_lookup_options) @@ -1229,9 +1227,7 @@ def _err_handler(err): log.exception("hit unexpected error!") _err_handler(err) else: - log.info( - "reservation '%s' extended by '%s'", res["metadata"]["name"], duration - ) + log.info("reservation '%s' extended by '%s'", res["metadata"]["name"], duration) @reservation.command("delete") @@ -1267,17 +1263,17 @@ def _err_handler(err): @reservation.command("list") @click.option( - '--mine', - '-m', + "--mine", + "-m", is_flag=True, - help='Return reservations belonging to the result of oc whoami', + help="Return reservations belonging to the result of oc whoami", ) @click.option( - '--requester', - '-r', + "--requester", + "-r", type=str, default=None, - help='Return reservations belonging to the provided requester', + help="Return reservations belonging to the provided requester", ) def _list_reservations(mine, requester): def _err_handler(err): diff --git a/bonfire/namespaces.py b/bonfire/namespaces.py index da8a0d0f..f6c1979c 100644 --- a/bonfire/namespaces.py +++ b/bonfire/namespaces.py @@ -301,12 +301,12 @@ def reserve_namespace(duration, retries, specific_namespace=None, attempt=0): def release_namespace(namespace): - oc("label", "--overwrite", "namespace", namespace, f"{NS_RESERVED}=false") - - -def reset_namespace(namespace): - release_namespace(namespace) - oc("label", "--overwrite", "namespace", namespace, f"{NS_READY}=false") + ns = Namespace(name=namespace) + ns.reserved = False + ns.requester = None + ns.requester_name = None + ns.ready = False + ns.update() def _delete_resources(namespace):