browsertrix/backend/btrixcloud/k8s/utils.py
Ilya Kreymer 2717a60763
improvements / bug fixes for stop/cancel handling: (#279)
- only send signal if stopping, no need for canceling as pods/containers will be removed
- refactor stop/cancel handling to be unified in manager, separate in job
- when stopping / graceful shutdown, return false if sending signal fails
- return success=true in json response if and only if stop/cancel actually succeeds, return 'error' message in error, should fix #270
- allow canceling after stopping / if stopping fails
- ensure finished time is set in case of cancelation before crawl starts, should fix #273
2022-06-29 17:47:25 -07:00

50 lines
1.3 KiB
Python

""" k8s utils """
import os
import yaml
from kubernetes_asyncio.utils import create_from_dict
def get_templates_dir():
""" return directory containing templates for loading """
return os.path.join(os.path.dirname(__file__), "templates")
async def create_from_yaml(k8s_client, doc, namespace):
""" init k8s objects from yaml """
yml_document_all = yaml.safe_load_all(doc)
k8s_objects = []
for yml_document in yml_document_all:
created = await create_from_dict(
k8s_client, yml_document, verbose=False, namespace=namespace
)
k8s_objects.append(created)
return k8s_objects
async def send_signal_to_pods(core_api_ws, namespace, pods, signame, func=None):
""" send signal to all pods """
command = ["kill", "-s", signame, "1"]
signaled = False
try:
for pod in pods:
if func and not func(pod.metadata):
continue
await core_api_ws.connect_get_namespaced_pod_exec(
pod.metadata.name,
namespace=namespace,
command=command,
stdout=True,
)
signaled = True
# pylint: disable=broad-except
except Exception as exc:
print(f"Send Signal Error: {exc}", flush=True)
return signaled