一个 Python 框架,只需几行代码即可编写 Kubernetes 运算器
Kopf — Kubernetes Operator Pythonic Framework — is a framework and a library to make Kubernetes operator development easier, in just 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).
Kopf is production-ready and stable (semantic v1). Major bugs are fixed ASAP (there were none for a long time). Minor bugs are fixed as time and energy permit, or a workaround is provided.
There is no active development of new major functionality for Kopf — the whole idea of a framework for operators is fully expressed and implemented, I have nothing more to add. This piece of art is finished. (This might change.)
Minor feature requests can be implemented from time to time. Maintenance for new versions of Python and Kubernetes is performed regularly. Some internal optimizations are planned, such as minimizing the memory footprint, high-load readiness, or agentic friendliness — but will be backwards-compatible (no semantic v2 with breaking changes on the horizon).
Dockerfile + a Python file (*).(*) 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.
See examples for examples of 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's easy! For more features, see the documentation.
Python 3.10+ is required: CPython and PyPy are officially supported and tested; other Python implementations can work too.
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.14
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 examples).
For quick experimentation, a pre-built image with all extras is available on GHCR — just mount your operator file and go:
# Minimize the credentials exposure.
kubectl config view --minify --flatten > dev.kubeconfig
# Run the operator locally, target a local cluster (host networking).
docker run --rm -it --network=host \
-v ./handlers.py:/app/main.py:ro \
-v ./dev.kubeconfig:/root/.kube/config:ro \
ghcr.io/nolar/kopf
See the Docker image documentation for more details.
See kopf run --help for other ways of attaching the handlers.
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.
We use SemVer for versioning. For the versions available, see the releases on this repository.
This project is licensed under the MIT License — see the LICENSE file for details.
暂无开放 Issues,或尚未同步最近议题。