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