all repos — janny @ a5e17725e7d2ea9c0d165c5c9e96d5da10f782c0

clean up Kubernetes resources after a set TTL

Runs on a loop
Anirudh Oppiliappan x@icyphox.sh
Tue, 09 Mar 2021 16:07:53 +0530
commit

a5e17725e7d2ea9c0d165c5c9e96d5da10f782c0

parent

02b42c96dd97c647c86790c17b4b352630701130

6 files changed, 34 insertions(+), 22 deletions(-)

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

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

from .main import main if __name__ == "__main__": - main() + while True: + main()
M janny/auth.pyjanny/auth.py

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

import requests -import logging import os -logging.basicConfig(level=logging.INFO) +from janny.config import logger def kube_auth():

@@ -14,9 +13,9 @@ with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as f:

token = f.read() session.verify = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" session.headers.update({"Authorization": f"Bearer {token}"}) - logging.info("Authenticated with the API server") + logger.info("Authenticated with the API server") else: - logging.info("Not in-cluster, continuing as is") + logger.info("Not in-cluster, continuing as is") return session
M janny/cleanup.pyjanny/cleanup.py

@@ -1,12 +1,8 @@

import time -import logging -import datetime -from janny.utils import parse_delta, get +from janny.utils import parse_delta from janny.auth import SESSION as s -from janny.config import API_HOST - -logging.basicConfig(level=logging.INFO) +from janny.config import API_HOST, logger def clean_up(

@@ -15,9 +11,9 @@ ):

delta = parse_delta(kill_time) secs = delta.total_seconds() time.sleep(secs) - logging.info(f"Slept for {kill_time}. Cleaning resource {resource_name} now.") + logger.info(f"Slept for {kill_time}. Cleaning resource {resource_name} now.") send_delete_event(url, kube_resource, resource_name, namespace) - logging.info(f"Sent delete event to {kube_resource}/{resource_name}") + logger.info(f"Sent delete event to {kube_resource}/{resource_name}") def send_delete_event(url: str, kube_resource: str, resource_name: str, namespace: str):
M janny/config.pyjanny/config.py

@@ -1,3 +1,12 @@

import os +import logging + +# Logging setup +formatter = logging.Formatter(fmt="%(asctime)s :: %(message)s") +handler = logging.StreamHandler() +handler.setFormatter(formatter) +logger = logging.getLogger("root") +logger.setLevel(logging.INFO) +logger.addHandler(handler) API_HOST = os.getenv("JANNY_API_HOST") or "https://kubernetes.default"
M janny/main.pyjanny/main.py

@@ -1,10 +1,11 @@

import threading -import logging from janny.utils import get from janny.cleanup import clean_up +from janny.config import logger + -logging.basicConfig(level=logging.INFO) +RUNNING = [] def get_resource_urls() -> list:

@@ -48,18 +49,25 @@ """

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): - print(r.metadata.annotations) + 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], ) - logging.info(f"Starting cleaner thread for {r.metadata.name}") + logger.info(f"Starting cleaner thread for {r.metadata.name}") + RUNNING.append(r.metadata.name) t.start() def main(): - filtered = filter_included_resources(["deployments"], get_resource_urls()) - for f in filtered: - spawn_clean_up_job(f, "default") + while True: + filtered = filter_included_resources(["deployments"], get_resource_urls()) + for f in filtered: + spawn_clean_up_job(f, "default")
M janny/utils.pyjanny/utils.py

@@ -1,4 +1,3 @@

-import logging import json import re from datetime import timedelta