aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/apis/kubernetes/deployments.py
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2021-07-18 03:58:54 +0100
committerGravatar Joe Banks <[email protected]>2021-07-18 03:58:54 +0100
commitdf3ed1affc5c9337b82a896ce724eacb7f2ddb25 (patch)
treec16642b974ed60dd218817e080ae242d50dbacba /arthur/apis/kubernetes/deployments.py
parentfix: linting fixes (diff)
refactor: split kubernetes API interaction into it's own module
Diffstat (limited to 'arthur/apis/kubernetes/deployments.py')
-rw-r--r--arthur/apis/kubernetes/deployments.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/arthur/apis/kubernetes/deployments.py b/arthur/apis/kubernetes/deployments.py
new file mode 100644
index 0000000..8d667b0
--- /dev/null
+++ b/arthur/apis/kubernetes/deployments.py
@@ -0,0 +1,37 @@
+"""APIs for working with Kubernetes deployments."""
+from datetime import datetime, timezone
+
+from kubernetes_asyncio import client
+from kubernetes_asyncio.client.api_client import ApiClient
+from kubernetes_asyncio.client.models import V1DeploymentList
+
+
+async def restart_deployment(deployment: str, namespace: str) -> None:
+ """Patch a deployment with a custom annotation to trigger redeployment."""
+ async with ApiClient() as api:
+ v1 = client.AppsV1Api(api)
+ await v1.patch_namespaced_deployment(
+ name=deployment,
+ namespace=namespace,
+ body={
+ "spec": {
+ "template": {
+ "metadata": {
+ "annotations": {
+ "king-arthur.pydis.com/restartedAt": datetime.now(
+ timezone.utc
+ ).isoformat()
+ }
+ }
+ }
+ }
+ },
+ field_manager="King Arthur",
+ )
+
+
+async def list_deployments(namespace: str) -> V1DeploymentList:
+ """Query the Kubernetes API for a list of deployments in the provided namespace."""
+ async with ApiClient() as api:
+ v1 = client.AppsV1Api(api)
+ return await v1.list_namespaced_deployment(namespace=namespace)