all repos — janny @ f504aa9245876aa806532e909f2c410ad9c240e7

clean up Kubernetes resources after a set TTL

Add argument parsing
Anirudh Oppiliappan x@icyphox.sh
Tue, 09 Mar 2021 17:57:02 +0530
commit

f504aa9245876aa806532e909f2c410ad9c240e7

parent

5dc8a67abe83bd84041c9d223c645845363ead18

4 files changed, 58 insertions(+), 24 deletions(-)

jump to
M janny/__main__.pyjanny/__main__.py

@@ -1,5 +1,35 @@

+import argparse +import sys + from .main import main +from janny.config import logger + if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument( + "--included-resources", + help="Comma separated list of Kubernetes resources for janny to clean up.", + type=str, + ) + parser.add_argument( + "--namespaces", + help="Comma separated list of namespaces for janny to scan.", + type=str, + default="default", + ) + args = parser.parse_args() + + namespace_list = list() + if not args.included_resources: + parser.print_help() + sys.exit(1) + elif not args.namespaces: + logger.info("No namspaces provided, using 'default'") + elif args.namespaces: + namespace_list = args.namespaces.replace(" ", "").split(",") + + include_list = args.included_resources.replace(" ", "").split(",") + while True: - main() + main(include_list, namespace_list)
M janny/cleanup.pyjanny/cleanup.py

@@ -20,5 +20,6 @@ def send_delete_event(url: str, kube_resource: str, resource_name: str, namespace: str):

api_url = f"{API_HOST}{url}/namespaces/{namespace}/{kube_resource}/{resource_name}" response = s.delete(api_url, params={"propagationPolicy": "Background"}) logger.info(f"Sent delete event to {kube_resource}/{resource_name}") - if "Success" not in json.loads(response.content): - logger.error(f"Deletion did not succeed. Recieved: {response.content}") + response_json = json.loads(response.content) + if "Success" not in response_json.values(): + logger.error(f"Deletion did not succeed. Recieved: {response_json}")
M janny/main.pyjanny/main.py

@@ -1,4 +1,5 @@

import threading +import itertools from janny.utils import get from janny.cleanup import clean_up

@@ -49,25 +50,27 @@ """

url, resource = resource_tuple resource_list = get(f"{url}/namespaces/{namespace}/{resource.name}") for r in resource_list.items: - if ( - "janny.ttl" in vars(r.metadata.annotations) - and r.metadata.name not in RUNNING - ): - logger.info( - f"New resource to clean up: {resource.name}/{r.metadata.name}: {vars(r.metadata.annotations)}" - ) - kill_time = vars(r.metadata.annotations)["janny.ttl"] - t = threading.Thread( - target=clean_up, - args=[url, resource.name, r.metadata.name, kill_time, namespace], - ) - logger.info(f"Starting cleaner thread for {r.metadata.name}") - RUNNING.append(r.metadata.name) - t.start() + try: + if ( + "janny.ttl" in vars(r.metadata.annotations) + and r.metadata.name not in RUNNING + ): + logger.info( + f"New resource to clean up: {resource.name}/{r.metadata.name}: {vars(r.metadata.annotations)}" + ) + kill_time = vars(r.metadata.annotations)["janny.ttl"] + t = threading.Thread( + target=clean_up, + args=[url, resource.name, r.metadata.name, kill_time, namespace], + ) + logger.info(f"Starting cleaner thread for {r.metadata.name}") + RUNNING.append(r.metadata.name) + t.start() + except AttributeError: + pass -def main(): - while True: - filtered = filter_included_resources(["deployments"], get_resource_urls()) - for f in filtered: - spawn_clean_up_job(f, "default") +def main(include_list, namespace_list): + filtered = filter_included_resources(include_list, get_resource_urls()) + for f, n in itertools.product(filtered, namespace_list): + spawn_clean_up_job(f, n)
M pyproject.tomlpyproject.toml

@@ -1,7 +1,7 @@

[tool.poetry] name = "janny" version = "0.1.0" -description = "Delete Kubernetes resources after a set time" +description = "Delete Kubernetes resources after a set TTL" authors = ["Anirudh Oppiliappan <x@icyphox.sh>"] license = "MIT"