Skip to main content

Kubernetes Operator Pythonic Framework (Kopf)

Reason this release was yanked:

broken sub-sub-handlers?

Project description

Kubernetes Operator Pythonic Framework (Kopf)

CI codecov Coverage Status Total alerts Language grade: Python pre-commit

Kopf —Kubernetes Operator Pythonic Framework— is a framework and a library to make Kubernetes operators development easier, just in a few lines of Python code.

The main goal is to bring the Domain-Driven Design to the infrastructure level, with Kubernetes being an orchestrator/database of the domain objects (custom resources), and the operators containing the domain logic (with no or minimal infrastructure logic).

The project was originally started as zalando-incubator/kopf in March 2019, and then forked as nolar/kopf in August 2020: but it is the same codebase, the same packages, the same developer(s).

Documentation

Features

  • Simple, but powerful:
    • A full-featured operator in just 2 files: a Dockerfile + a Python file (*).
    • Handling functions registered via decorators with a declarative approach.
    • No infrastructure boilerplate code with K8s API communication.
    • Both sync and async handlers, with sync ones being threaded under the hood.
    • Detailed documentation with examples.
  • Intuitive mapping of Python concepts to Kubernetes concepts and back:
    • Marshalling of resources' data to the handlers' kwargs.
    • Marshalling of handlers' results to the resources' statuses.
    • Publishing of logging messages as Kubernetes events linked to the resources.
  • Support anything that exists in K8s:
    • Custom K8s resources.
    • Builtin K8s resources (pods, namespaces, etc).
    • Multiple resource types in one operator.
    • Both cluster and namespaced operators.
  • All the ways of handling that a developer can wish for:
    • Low-level handlers for events received from K8s APIs "as is" (an equivalent of informers).
    • High-level handlers for detected causes of changes (creation, updates with diffs, deletion).
    • Handling of selected fields only instead of the whole objects (if needed).
    • Dynamically generated or conditional sub-handlers (an advanced feature).
    • Timers that tick as long as the resource exists, optionally with a delay since the last change.
    • Daemons that run as long as the resource exists (in threads or asyncio-tasks).
    • Validating and mutating admission webhook (with dev-mode tunneling).
    • Live in-memory indexing of resources or their excerpts.
    • Filtering with stealth mode (no logging): by arbitrary filtering functions, by labels/annotations with values, presence/absence, or dynamic callbacks.
    • In-memory all-purpose containers to store non-serializable objects for individual resources.
  • Eventual consistency of handling:
    • Retrying the handlers in case of arbitrary errors until they succeed.
    • Special exceptions to request a special retry or to never retry again.
    • Custom limits for the number of attempts or the time.
    • Implicit persistence of the progress that survives the operator restarts.
    • Tolerance to restarts and lengthy downtimes: handles the changes afterwards.
  • Awareness of other Kopf-based operators:
    • Configurable identities for different Kopf-based operators for the same resource kinds.
    • Avoiding double-processing due to cross-pod awareness of the same operator ("peering").
    • Pausing of a deployed operator when a dev-mode operator runs outside of the cluster.
  • Extra toolkits and integrations:
    • Some limited support for object hierarchies with name/labels propagation.
    • Friendly to any K8s client libraries (and is client agnostic).
    • Startup/cleanup operator-level handlers.
    • Liveness probing endpoints and rudimentary metrics exports.
    • Basic testing toolkit for in-memory per-test operator running.
    • Embeddable into other Python applications.
  • Highly configurable (to some reasonable extent).

(*) Small font: two files of the operator itself, plus some amount of deployment files like RBAC roles, bindings, service accounts, network policies — everything needed to deploy an application in your specific infrastructure.

Examples

See examples for the examples of the typical use-cases.

A minimalistic operator can look like this:

import kopf

@kopf.on.create('kopfexamples')
def create_fn(spec, name, meta, status, **kwargs):
    print(f"And here we are! Created {name} with spec: {spec}")

Numerous kwargs are available, such as body, meta, spec, status, name, namespace, retry, diff, old, new, logger, etc: see Arguments

To run a never-exiting function for every resource as long as it exists:

import time
import kopf

@kopf.daemon('kopfexamples')
def my_daemon(spec, stopped, **kwargs):
    while not stopped:
        print(f"Object's spec: {spec}")
        time.sleep(1)

Or the same with the timers:

import kopf

@kopf.timer('kopfexamples', interval=1)
def my_timer(spec, **kwargs):
    print(f"Object's spec: {spec}")

That easy! For more features, see the documentation.

Usage

We assume that when the operator is executed in the cluster, it must be packaged into a docker image with a CI/CD tool of your preference.

FROM python:3.7
ADD . /src
RUN pip install kopf
CMD kopf run /src/handlers.py --verbose

Where handlers.py is your Python script with the handlers (see examples/*/example.py for the examples).

See kopf run --help for other ways of attaching the handlers.

Contributing

Please read CONTRIBUTING.md for details on our process for submitting pull requests to us, and please ensure you follow the CODE_OF_CONDUCT.md.

To install the environment for the local development, read DEVELOPMENT.md.

Versioning

We use SemVer for versioning. For the versions available, see the releases on this repository.

License

This project is licensed under the MIT License — see the LICENSE file for details.

Acknowledgments

  • Thanks to Zalando for starting this project in Zalando's Open-Source Incubator in the first place.
  • Thanks to @side8 and their k8s-operator for inspiration.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kopf-1.32.0.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

kopf-1.32.0-py3-none-any.whl (196.9 kB view details)

Uploaded Python 3

File details

Details for the file kopf-1.32.0.tar.gz.

File metadata

  • Download URL: kopf-1.32.0.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for kopf-1.32.0.tar.gz
Algorithm Hash digest
SHA256 3bb2d85a7d9579469f78e455f13c417018f1d483f7936ab853edbe8fdd2805a7
MD5 171277ea05a58eea9bf9c1f93b16bb21
BLAKE2b-256 1a45e29f1bdc6d27296208ebe7742fbc5f01296d32d75e90f9de8bc1e66a3942

See more details on using hashes here.

File details

Details for the file kopf-1.32.0-py3-none-any.whl.

File metadata

  • Download URL: kopf-1.32.0-py3-none-any.whl
  • Upload date:
  • Size: 196.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for kopf-1.32.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82323f84ddab504a6dbc145f705ddf4ee3e9ff1c72c1013020127e653b302e0a
MD5 8208e32a50a483be6e17c4f0c8e0e10f
BLAKE2b-256 f52fb90d3c52ecdd5a7c5271867baaed0fc5225514cb106a2c7b81915064c826

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page