This commit is contained in:
parent
040f84328e
commit
e276e71456
@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import yaml
|
||||
from base64 import b64decode
|
||||
@ -8,6 +9,11 @@ from kubernetes_asyncio import client, config, watch
|
||||
from os import path
|
||||
from time import time
|
||||
from urllib.parse import urlparse
|
||||
import useful.logs
|
||||
|
||||
useful.logs.setup(
|
||||
json_fields={"msg":"message", "level": "levelname"})
|
||||
logger = logging.getLogger()
|
||||
|
||||
NAMESPACE = os.environ["MY_POD_NAMESPACE"]
|
||||
|
||||
@ -34,7 +40,7 @@ async def apply_changes(item, v1, now, apps_api):
|
||||
target = o._replace(netloc="%s:%s@%s" % (username, password, netloc)).geturl()
|
||||
|
||||
name = "camera-%s" % item["metadata"]["name"]
|
||||
print("Applying changes for", name, "CRD")
|
||||
logger.info("Applying changes for %s", name)
|
||||
|
||||
# Generate Deployment
|
||||
body = yaml.safe_load(DEPLOYMENT_BODY.replace("foobar", name))
|
||||
@ -47,11 +53,11 @@ async def apply_changes(item, v1, now, apps_api):
|
||||
try:
|
||||
await apps_api.replace_namespaced_deployment(
|
||||
name = name, body = body, namespace=NAMESPACE)
|
||||
print(" * Updated deployment %s/%s" % (NAMESPACE, name))
|
||||
logger.debug("Updated deployment %s/%s", NAMESPACE, name)
|
||||
except client.exceptions.ApiException as e:
|
||||
await apps_api.create_namespaced_deployment(
|
||||
body = body, namespace=NAMESPACE)
|
||||
print(" * Created deployment %s/%s" % (NAMESPACE, name))
|
||||
logger.debug("Created deployment %s/%s", NAMESPACE, name)
|
||||
|
||||
# Generate Service
|
||||
body = yaml.safe_load(SERVICE_BODY.replace("foobar", name))
|
||||
@ -60,11 +66,11 @@ async def apply_changes(item, v1, now, apps_api):
|
||||
try:
|
||||
await v1.replace_namespaced_service(
|
||||
name = name, body = body, namespace=NAMESPACE)
|
||||
print(" * Updated service %s/%s" % (NAMESPACE, name))
|
||||
logger.debug("Updated service %s/%s", NAMESPACE, name)
|
||||
except client.exceptions.ApiException as e:
|
||||
await v1.create_namespaced_service(
|
||||
body = body, namespace=NAMESPACE)
|
||||
print(" * Created service %s/%s" % (NAMESPACE, name))
|
||||
logger.debug("Created service %s/%s", NAMESPACE, name)
|
||||
|
||||
|
||||
async def main():
|
||||
@ -85,7 +91,7 @@ async def main():
|
||||
for i in resp["items"]:
|
||||
await apply_changes(i, v1, now, apps_api)
|
||||
|
||||
print("Cleaning up dangling deployments and services")
|
||||
logger.info("Cleaning up dangling deployments and services")
|
||||
resp = await v1.list_namespaced_service(NAMESPACE)
|
||||
for i in resp.items:
|
||||
if not i.metadata.labels:
|
||||
@ -94,7 +100,7 @@ async def main():
|
||||
continue
|
||||
if i.metadata.labels.get("modified") == now:
|
||||
continue
|
||||
print(" * Removing service: %s/%s" % (NAMESPACE, i.metadata.name))
|
||||
logger.debug("Removing service: %s/%s", NAMESPACE, i.metadata.name)
|
||||
await v1.delete_namespaced_service(i.metadata.name, NAMESPACE)
|
||||
|
||||
resp = await apps_api.list_namespaced_deployment(NAMESPACE)
|
||||
@ -105,10 +111,10 @@ async def main():
|
||||
continue
|
||||
if i.metadata.labels.get("modified") == now:
|
||||
continue
|
||||
print(" * Removing deployment: %s/%s" % (NAMESPACE, i.metadata.name))
|
||||
logger.debug("Removing deployment: %s/%s", NAMESPACE, i.metadata.name)
|
||||
await apps_api.delete_namespaced_deployment(i.metadata.name, NAMESPACE)
|
||||
|
||||
print("Subscribing to updates")
|
||||
logger.info("Subscribing to updates")
|
||||
|
||||
async for event in w.stream(api_instance.list_namespaced_custom_object, *args):
|
||||
if event["type"] == "ADDED":
|
||||
@ -120,13 +126,13 @@ async def main():
|
||||
except client.exceptions.ApiException as e:
|
||||
pass
|
||||
else:
|
||||
print("Removed service: %s/%s" % (NAMESPACE, name))
|
||||
logger.debug("Removed service: %s/%s", NAMESPACE, name)
|
||||
try:
|
||||
await apps_api.delete_namespaced_deployment(name, NAMESPACE)
|
||||
except client.exceptions.ApiException as e:
|
||||
pass
|
||||
else:
|
||||
print("Removed deployment: %s/%s" % (NAMESPACE, name))
|
||||
logger.debug("Removed deployment: %s/%s", NAMESPACE, name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Reference in New Issue
Block a user