Skip to main content

easykube

A simple Python client for Kubernetes, providing both synchronous and asynchronous implementations.

Creating a client

Clients are created using Configuration objects. A single Configuration object can produce multiple clients, both synchronous or asynchronous.

import easykube


#####
# Initialise a configuration object
#####

# From kubeconfig data as str or bytes
# Currently, only certificate authentication is supported
config = easykube.Configuration.from_kubeconfig_data(data)
# From the kubeconfig file at the given path
config = easykube.Configuration.from_kubeconfig(path)
# From a service account
config = easykube.Configuration.from_serviceaccount()
# From the environment, using the following (in order of precedence)
#   * kubeconfig file specified using KUBECONFIG environment variable if present
#   * service account if environment variables are present
#   * kubeconfig file at $HOME/.kube/config
config = easykube.Configuration.from_environment()


#####
# Create a sync or async client from the configuration
#
# Clients should be used in a context to ensure connections are properly disposed
#####

# Optionally specify a default namespace for the client
# Defaults to the namespace from the kubeconfig if present, "default" otherwise
with config.sync_client(default_namespace="default") as sync_client:
    do_something(sync_client)

async with config.async_client(default_namespace="default") as async_client:
    await do_something(async_client)

Interacting with Kubernetes objects

All objects in Kubernetes are part of an API (e.g. v1, networking.k8s.io/v1) and a resource (e.g. pods, ingresses).

The client provides methods for consuming APIs and resources and performing operations on their objects, including subresources like pod logs, status and scale subresources.

#####
# Consume a particular resource, e.g. pods, ingresses
#####
sync_pods = sync_client.api("v1").resource("pods")
async_pods = await async_client.api("v1").resource("pods")

sync_ingresses = sync_client.api("networking.k8s.io/v1").resource("ingresses")
async_ingresses = await async_client.api("networking.k8s.io/v1").resource("ingresses")

#####
# Consume a subresource
#   e.g. pod logs, status subresource
#####
sync_pod_logs = sync_client.api("v1").resource("pods/log")
async_pod_logs = await async_client.api("v1").resource("pods/log")
#   e.g. the status of a custom resource when writing controllers
sync_custom_status = sync_client.api("example.com/v1").resource("myresource/status")
async_custom_status = await async_client.api("example.com/v1").resource(
    "myresource/status"
)

Listing objects

for obj in sync_resource.list(
    # The namespace to list resources in, defaults to the client's default namespace
    namespace="myns",
    # Whether to list resources in all namespaces (default false)
    all_namespaces=True | False,
    # Labels to match, default none
    labels={
        # Match a single value
        "label1": "value1",
        # Match one of a set of values
        "label2": ["value2", "value3", "value4"],
        # Require label to be present with any value
        "label3": easykube.PRESENT,
        # Require label to be absent
        "label4": easykube.ABSENT,
    },
    # Additional kwargs arguments are passed as URL parameters
    **kwargs,
):
    do_something(obj)


# Async client produces an async iterator when listing
# Supports the same parameters as the synchronous list
async for obj in async_resource.list(...):
    await do_something(obj)

Fetching individual objects

When fetching a single object by name using fetch, the type of the returned object is determined using the Content-Type of the response. For application/json responses, the object is decoded and the Python object is returned. For text/plain responses, the text is returned as a str. For all other responses, the received bytes are returned.

# Fetch the first object that matches a list query, or None if there are no matches
# Supports the same parameters as list
obj = sync_resource.first(...)
obj = await async_resource.first(...)

# Fetch a single object by name
obj = sync_resource.fetch(
    name,
    # The namespace that the object is in, defaults to the client's default namespace
    namespace="myns",
    # Additional kwargs arguments are passed as URL parameters
    **kwargs,
)
obj = await async_resource.fetch(...)

Example - reading pod logs

fetch allows additional parameters to be specified which are passed as URL parameters. This can be useful when reading pod logs as there are several parameters you can use to control the logs that are returned.

async_pod_logs = await async_client.api("v1").resource("pods/log")

# Fetch the logs for a specific container
logs = await async_pod_logs.fetch(name, namespace="myns", container="container1")

# Fetch the logs for the last 60 seconds only
logs = await async_pod_logs.fetch(name, namespace="myns", sinceSeconds=60)

# Fetch only the last 10 lines of logs
logs = await async_pod_logs.fetch(name, namespace="myns", tailLines=10)

Creating objects

obj = sync_resource.create(data, namespace="myns")
obj = await async_resource.create(...)

Updating objects

There are various ways to fully or partially update an object. See the Kubernetes docs for more details.

# Replace the whole object using a PUT request
# The metadata.resourceVersion must match the version that the server has
obj = sync_resource.replace(name, data, namespace="myns")
obj = await async_resource.replace(...)

# Update an object using a PATCH request and a JSON merge formatted patch
obj = sync_resource.patch(name, data, namespace="myns")
obj = await async_resource.patch(...)

# Update an object using a PATCH request and a JSON patch formatted patch
obj = sync_resource.json_patch(name, data, namespace="myns")
obj = await async_resource.json_patch(...)

# Update an object using server-side apply
# https://kubernetes.io/docs/reference/using-api/server-side-apply/
obj = sync_resource.server_side_apply(name, data, namespace="myns", force=True)
obj = await async_resource.server_side_apply(...)

Deleting objects

Kubernetes allows for different ways of propagating deletion to dependent objects. For more information, see the Kubernetes docs.

# Delete a single object by name
obj = sync_resource.delete(
    name,
    namespace="myns",
    # The propagation policy to use
    # One of BACKGROUND, FOREGROUND or ORPHAN
    propagation_policy=easykube.DeletePropagationPolicy.BACKGROUND,
)
obj = await async_resource.delete(...)

# Delete a collection of objects matching the given arguments
# Supports the same arguments as list
obj = sync_resource.delete_all(...)
obj = await async_resource.delete_all(...)

Watching objects

easykube allows synchronous and asynchronous watching of objects.

Both watch_list and watch_one return a tuple of the initial state and an iterator of events.

For watch_list, the initial state is a list of objects. For watch_one, the initial state is a single object or None if the object does not exist.

The events that are produced have the format:

{
    "type": "ADDED" | "MODIFIED" | "DELETED",
    "object": {
        # ... the state of the object when the event was produced ...
    },
}
#####
# Watch a collection of objects
#####
# Supports the same arguments as list
initial_state, sync_events = sync_resource.watch_list(...)
initial_state, async_events = await async_resource.watch_list(...)

# Process the initial state
# The initial state is always a regular list for both sync and async resources
for obj in initial_state:
    do_something(obj)

# Process events (synchronous)
for event in sync_events:
    process_event(event["type"], event["object"])

# Process events (asynchronous)
async for event in async_events:
    await process_event(event["type"], event["object"])


#####
# Watch a single object
#####
initial_state, events = sync_resource.watch_one(name, namespace="myns")
initial_state, events = await async_resource.watch_one(name, namespace="myns")

Client-level methods

As well as interacting directly with individual resources, the client provides some top-level convenience methods that operate on "fully-formed" Kubernetes objects that already include information about their API, resource, name and namespace via the apiVersion, kind, metadata.name and metadata.namespace fields.

The apply_object function is particularly useful for keeping an object consistent with the expected representation, e.g. in a controller, using server-side apply.

#####
# Create an object
#####
obj = sync_client.create_object(obj)
obj = await async_client.create_object(obj)

#####
# Replace an entire object
#####
obj = sync_client.replace_object(obj)
obj = await async_client.replace_object(obj)

#####
# Patch an object
#####
patch = {"data": {"key2": "value2-1", "key3": "value3"}}
obj = sync_client.patch_object(obj, patch)
obj = await async_client.patch_object(obj, patch)

#####
# Delete an object
#####
obj = sync_client.delete_object(obj)
obj = await async_client.delete_object(obj)

#####
# Apply an object using server-side apply
# https://kubernetes.io/docs/reference/using-api/server-side-apply/
#####
obj = sync_client.apply_object(obj, force=True)
obj = await async_client.apply_object(obj, force=True)

#####
# Apply an object from the client-side using a "create-or-update" pattern
# This is similar to what "kubectl apply" does but does not use an annotation
#####
obj = sync_client.client_side_apply_object(obj)
obj = await async_client.client_side_apply_object(obj)

Running tests

The tests use pytest and are run with tox via uv:

uv run --locked tox                         # lint, tests, mypy and coverage
uv run --locked tox -e 3.14 -- -k <expr>    # run selected tests only
uv run --locked tox -e autofix              # apply ruff and codespell fixes

The coverage environment prints a coverage report and the coverage of lines changed since origin/main.

Tests live in easykube/tests/. Please add tests alongside your changes.

Metadata

Release files for easykube 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for easykube 0.7.0
File Size Uploaded Provenance Actions
easykube-0.7.0.tar.gz 123.0 kB With provenance Details for easykube-0.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for easykube 0.7.0
File Interpreter ABI Platform Actions
easykube-0.7.0-py3-none-any.whl Python 3 none any Details for easykube-0.7.0-py3-none-any.whl

Total release size: 157.7 kB

Release files / easykube-0.7.0.tar.gz

Download URL easykube-0.7.0.tar.gz
Size 123.0 kB
Tags Source
SHA-256 checksum
How to use checksums
20209739f1c09ef2c88a36f591cb30aca333d15f91480726cd8452b53f800331
BLAKE2b-256 checksum
How to use checksums
062ed2c32a49a103c65d77ab27f815ec85e5735f4fe64b501cb6ca7e2d9bd7ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / easykube-0.7.0-py3-none-any.whl

Download URL easykube-0.7.0-py3-none-any.whl
Size 34.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd123dbd008413d7bc6250109048fe2eda742d56d3cb11dde628e1b3b0cd657d
BLAKE2b-256 checksum
How to use checksums
fa3340b75d9d71811d61b8bf44579501c4fb887efd8e1cbe8f1c635476004c8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for easykube 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for easykube-0.7.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page