Fix flake8 errors
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
Lauri Võsandi 2022-08-03 07:50:30 +03:00
parent e276e71456
commit 43764d3365
3 changed files with 80 additions and 74 deletions

6
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,6 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.10.0,flake8-quotes==3.2.0]

6
README.md Normal file
View File

@ -0,0 +1,6 @@
To run locally with suitable `KUBECONFIG`:
```
MY_POD_NAMESPACE=camtiler ./camera-operator.py
```

View File

@ -6,13 +6,11 @@ import yaml
from base64 import b64decode
from kubernetes_asyncio.client.api_client import ApiClient
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"})
useful.logs.setup(json_fields={"msg": "message", "level": "levelname"})
logger = logging.getLogger()
NAMESPACE = os.environ["MY_POD_NAMESPACE"]
@ -23,6 +21,7 @@ with open("camera-service.yml") as stream:
with open("camera-deployment.yml") as stream:
DEPLOYMENT_BODY = stream.read()
async def apply_changes(item, v1, now, apps_api):
target = item["spec"]["target"]
replicas = item["spec"].get("replicas")
@ -51,12 +50,10 @@ async def apply_changes(item, v1, now, apps_api):
if replicas:
body["spec"]["replicas"] = replicas
try:
await apps_api.replace_namespaced_deployment(
name = name, body = body, namespace=NAMESPACE)
await apps_api.replace_namespaced_deployment(name, NAMESPACE, body)
logger.debug("Updated deployment %s/%s", NAMESPACE, name)
except client.exceptions.ApiException as e:
await apps_api.create_namespaced_deployment(
body = body, namespace=NAMESPACE)
except client.exceptions.ApiException:
await apps_api.create_namespaced_deployment(NAMESPACE, body)
logger.debug("Created deployment %s/%s", NAMESPACE, name)
# Generate Service
@ -64,12 +61,10 @@ async def apply_changes(item, v1, now, apps_api):
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)
await v1.replace_namespaced_service(name, NAMESPACE, body)
logger.debug("Updated service %s/%s", NAMESPACE, name)
except client.exceptions.ApiException as e:
await v1.create_namespaced_service(
body = body, namespace=NAMESPACE)
except client.exceptions.ApiException:
await v1.create_namespaced_service(NAMESPACE, body)
logger.debug("Created service %s/%s", NAMESPACE, name)
@ -123,20 +118,19 @@ async def main():
name = "camera-%s" % event["object"]["metadata"]["name"]
try:
await v1.delete_namespaced_service(name, NAMESPACE)
except client.exceptions.ApiException as e:
except client.exceptions.ApiException:
pass
else:
logger.debug("Removed service: %s/%s", NAMESPACE, name)
try:
await apps_api.delete_namespaced_deployment(name, NAMESPACE)
except client.exceptions.ApiException as e:
except client.exceptions.ApiException:
pass
else:
logger.debug("Removed deployment: %s/%s", NAMESPACE, name)
if __name__ == '__main__':
if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
loop.close()