GitHub

k8s_http_client

API reference for k8s_http_client.

The Kubernetes API from inside a cluster (Effect's K8sHttpClient).

layer builds a K8sHttpClient on the shared HttpClient: requests go to https://kubernetes.default.svc/api with the mounted service-account token (re-read at most once a minute, so a rotated token is picked up), non-2xx statuses fail and transient failures retry every five seconds. make_get_pods lists running pods by IP; make_create_pod creates a pod for the life of a scope and waits until it is ready.

Values

AttributeTypeDescription
API_URLLiteral['https://kubernetes.default.svc/api'] = 'https://kubernetes.default.svc/api'
TOKEN_PATHLiteral['/var/run/secrets/kubernetes.io/serviceaccount/toke…'] = '/var/run/secrets/kubernetes.io/serviceaccount/token'
POD_CONDITIONBottom[PodCondition, Mapping[str, object], Never, Never]
POD_STATUSBottom[PodStatus, Mapping[str, object], Never, Never]
PODBottom[Pod, Mapping[str, object], Never, Never]
layerlayer_.Layer[K8sHttpClient, Never, HttpClient | FileSystem]

A K8sHttpClient on the HttpClient, sending the pod's service account token (read through the FileSystem) to the cluster's API server.

make_create_podEffect[CreatePod, Never, K8sHttpClient]

A function creating a pod (in default unless its metadata names a namespace) that is deleted when the caller's scope closes; it returns the pod's status once ready.

Types

TypeDefinitionDescription
CreatePodtype CreatePod = Callable[[K8sPod], Effect[PodStatus, Never, Scope]]

Classes

Functions

make_get_pods

make_get_pods(*, namespace=None, label_selector=None) -> Effect[Effect[dict[str, Pod], HttpClientError | SchemaError], Never, K8sHttpClient]

A cached (ten seconds) read of the running pods, keyed by pod IP.

ParameterTypeDescription
namespacestr | None = None
label_selectorstr | None = None

Returns Effect[Effect[dict[str, Pod], HttpClientError | SchemaError], Never, K8sHttpClient]

def make_get_pods(
    *, namespace: str | None = None, label_selector: str | None = None
) -> Effect[Effect[dict[str, Pod], HttpClientError | SchemaError], Never, K8sHttpClient]:
    """A cached (ten seconds) read of the running pods, keyed by pod IP."""

    def body() -> Generator[Effect[Any, Any, Any], Any, Any]:
        client = yield from K8sHttpClient()
        request = request_.get(
            f"/v1/namespaces/{namespace}/pods" if namespace else "/v1/pods"
        ).set_url_param("fieldSelector", "status.phase=Running")
        if label_selector:
            request = request.set_url_param("labelSelector", label_selector)

        def by_ip(pods: _PodList) -> dict[str, Pod]:
            return {pod.status.pod_ip: pod for pod in pods.items}

        return (
            yield from client.execute(request)
            .flat_map(response_.schema_body_json(_PodList))
            .map(by_ip)
            .tap_cause(lambda cause: log_warning("Failed to fetch pods from Kubernetes API", cause))
            .cached_with_ttl("10 seconds")
        )

    return gen(body)