camera-operator/camera-operator.py

143 lines
5.2 KiB
Python
Raw Normal View History

2022-08-02 13:08:19 +00:00
#!/usr/bin/env python3
2022-08-01 16:25:56 +00:00
import asyncio
2022-08-02 17:41:16 +00:00
import logging
2022-08-02 13:01:21 +00:00
import os
2022-08-01 16:25:56 +00:00
import yaml
2022-08-01 17:29:53 +00:00
from base64 import b64decode
2022-08-01 16:25:56 +00:00
from kubernetes_asyncio.client.api_client import ApiClient
2022-08-02 13:01:21 +00:00
from kubernetes_asyncio import client, config, watch
2022-08-01 16:25:56 +00:00
from os import path
from time import time
2022-08-01 17:29:53 +00:00
from urllib.parse import urlparse
2022-08-02 17:41:16 +00:00
import useful.logs
useful.logs.setup(
json_fields={"msg":"message", "level": "levelname"})
logger = logging.getLogger()
2022-08-01 16:25:56 +00:00
2022-08-02 13:01:21 +00:00
NAMESPACE = os.environ["MY_POD_NAMESPACE"]
2022-08-01 16:25:56 +00:00
LABEL_MANAGED_BY = "camera-operator"
with open("camera-service.yml") as stream:
SERVICE_BODY = stream.read()
with open("camera-deployment.yml") as stream:
DEPLOYMENT_BODY = stream.read()
2022-08-02 13:01:21 +00:00
async def apply_changes(item, v1, now, apps_api):
target = item["spec"]["target"]
replicas = item["spec"].get("replicas")
2022-08-01 16:25:56 +00:00
2022-08-02 13:01:21 +00:00
# Pull in secrets for the target URL
secret_ref = item["spec"].get("secretRef")
if secret_ref:
secret = await v1.read_namespaced_secret(secret_ref, NAMESPACE)
username = b64decode(secret.data.get("username", b"")).decode("ascii")
password = b64decode(secret.data.get("password", b"")).decode("ascii")
o = urlparse(target)
netloc = o.netloc
if "@" in netloc:
_, netloc = o.netloc.split("@", 1)
target = o._replace(netloc="%s:%s@%s" % (username, password, netloc)).geturl()
name = "camera-%s" % item["metadata"]["name"]
2022-08-02 17:41:16 +00:00
logger.info("Applying changes for %s", name)
2022-08-02 13:01:21 +00:00
# Generate Deployment
body = yaml.safe_load(DEPLOYMENT_BODY.replace("foobar", name))
body["metadata"]["labels"] ["app.kubernetes.io/managed-by"] = LABEL_MANAGED_BY
body["metadata"]["labels"] ["modified"] = now
body["spec"]["template"]["spec"]["containers"][0]["args"] = [target]
if replicas:
body["spec"]["replicas"] = replicas
try:
await apps_api.replace_namespaced_deployment(
name = name, body = body, namespace=NAMESPACE)
2022-08-02 17:41:16 +00:00
logger.debug("Updated deployment %s/%s", NAMESPACE, name)
2022-08-02 13:01:21 +00:00
except client.exceptions.ApiException as e:
await apps_api.create_namespaced_deployment(
body = body, namespace=NAMESPACE)
2022-08-02 17:41:16 +00:00
logger.debug("Created deployment %s/%s", NAMESPACE, name)
2022-08-01 16:25:56 +00:00
2022-08-02 13:01:21 +00:00
# Generate Service
body = yaml.safe_load(SERVICE_BODY.replace("foobar", name))
body["metadata"]["labels"] ["app.kubernetes.io/managed-by"] = LABEL_MANAGED_BY
body["metadata"]["labels"] ["modified"] = now
try:
await v1.replace_namespaced_service(
name = name, body = body, namespace=NAMESPACE)
2022-08-02 17:41:16 +00:00
logger.debug("Updated service %s/%s", NAMESPACE, name)
2022-08-02 13:01:21 +00:00
except client.exceptions.ApiException as e:
await v1.create_namespaced_service(
body = body, namespace=NAMESPACE)
2022-08-02 17:41:16 +00:00
logger.debug("Created service %s/%s", NAMESPACE, name)
2022-08-02 13:01:21 +00:00
async def main():
if os.getenv("KUBECONFIG"):
await config.load_kube_config()
else:
2022-08-02 13:08:19 +00:00
config.load_incluster_config()
2022-08-02 13:01:21 +00:00
async with ApiClient() as api:
2022-08-01 16:25:56 +00:00
v1 = client.CoreV1Api(api)
apps_api = client.AppsV1Api()
api_instance = client.CustomObjectsApi(api)
now = str(time())
2022-08-02 13:01:21 +00:00
w = watch.Watch()
args = "k-space.ee", "v1alpha1", NAMESPACE, "cams"
resp = await api_instance.list_namespaced_custom_object(*args)
for i in resp["items"]:
await apply_changes(i, v1, now, apps_api)
2022-08-02 17:41:16 +00:00
logger.info("Cleaning up dangling deployments and services")
2022-08-02 13:01:21 +00:00
resp = await v1.list_namespaced_service(NAMESPACE)
for i in resp.items:
if not i.metadata.labels:
continue
if i.metadata.labels.get("app.kubernetes.io/managed-by") != LABEL_MANAGED_BY:
continue
if i.metadata.labels.get("modified") == now:
continue
2022-08-02 17:41:16 +00:00
logger.debug("Removing service: %s/%s", NAMESPACE, i.metadata.name)
2022-08-02 13:01:21 +00:00
await v1.delete_namespaced_service(i.metadata.name, NAMESPACE)
resp = await apps_api.list_namespaced_deployment(NAMESPACE)
for i in resp.items:
if not i.metadata.labels:
continue
if i.metadata.labels.get("app.kubernetes.io/managed-by") != LABEL_MANAGED_BY:
2022-08-01 16:25:56 +00:00
continue
2022-08-02 13:01:21 +00:00
if i.metadata.labels.get("modified") == now:
continue
2022-08-02 17:41:16 +00:00
logger.debug("Removing deployment: %s/%s", NAMESPACE, i.metadata.name)
2022-08-02 13:01:21 +00:00
await apps_api.delete_namespaced_deployment(i.metadata.name, NAMESPACE)
2022-08-02 17:41:16 +00:00
logger.info("Subscribing to updates")
2022-08-02 13:01:21 +00:00
async for event in w.stream(api_instance.list_namespaced_custom_object, *args):
if event["type"] == "ADDED":
await apply_changes(event["object"], v1, now, apps_api)
elif event["type"] == "DELETED":
name = "camera-%s" % event["object"]["metadata"]["name"]
2022-08-01 16:25:56 +00:00
try:
2022-08-02 13:01:21 +00:00
await v1.delete_namespaced_service(name, NAMESPACE)
2022-08-01 16:25:56 +00:00
except client.exceptions.ApiException as e:
2022-08-02 13:01:21 +00:00
pass
else:
2022-08-02 17:41:16 +00:00
logger.debug("Removed service: %s/%s", NAMESPACE, name)
2022-08-01 16:25:56 +00:00
try:
2022-08-02 13:01:21 +00:00
await apps_api.delete_namespaced_deployment(name, NAMESPACE)
2022-08-01 16:25:56 +00:00
except client.exceptions.ApiException as e:
2022-08-02 13:01:21 +00:00
pass
else:
2022-08-02 17:41:16 +00:00
logger.debug("Removed deployment: %s/%s", NAMESPACE, name)
2022-08-01 16:25:56 +00:00
if __name__ == '__main__':
2022-08-02 13:01:21 +00:00
loop = asyncio.new_event_loop()
2022-08-01 16:25:56 +00:00
loop.run_until_complete(main())
loop.close()