aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/apis/kubernetes/deployments.py
blob: b3aee83bf507bfc5d6230b38a904209d7a7094eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""APIs for working with Kubernetes deployments."""

from datetime import UTC, datetime

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_client:
        api = client.AppsV1Api(api_client)
        await api.patch_namespaced_deployment(
            name=deployment,
            namespace=namespace,
            body={
                "spec": {
                    "template": {
                        "metadata": {
                            "annotations": {
                                "king-arthur.pydis.com/restartedAt": datetime.now(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_client:
        api = client.AppsV1Api(api_client)
        return await api.list_namespaced_deployment(namespace=namespace)