new-pykube
Pykube (new-pykube) is a lightweight Python 3.10+ client library for Kubernetes.
This is a fork of the no longer maintained pykube-ng.
Features
- HTTP interface using requests using kubeconfig for authentication
- Python native querying of Kubernetes API objects
Installation
To install pykube, use pip:
pip install new-pykube
Interactive Console
The pykube library module can be run as an interactive console locally for quick exploration.
It will automatically load ~/.kube/config to provide the api object, and it loads pykube classes (Deployment, Pod, ..) into local context:
python3 -m pykube
>>> [d.name for d in Deployment.objects(api)]
Usage
Query for all ready pods in a custom namespace:
import operator
import pykube
api = pykube.HTTPClient(pykube.KubeConfig.from_file())
pods = pykube.Pod.objects(api).filter(namespace="gondor-system")
ready_pods = filter(operator.attrgetter("ready"), pods)
Access any attribute of the Kubernetes object:
pod = pykube.Pod.objects(api).filter(namespace="gondor-system").get(name="my-pod")
pod.obj["spec"]["containers"][0]["image"]
Selector query:
pods = pykube.Pod.objects(api).filter(
namespace="gondor-system",
selector={"gondor.io/name__in": {"api-web", "api-worker"}},
)
pending_pods = pykube.objects.Pod.objects(api).filter(
field_selector={"status.phase": "Pending"}
)
Watch query:
watch = pykube.Job.objects(api, namespace="gondor-system")
watch = watch.filter(field_selector={"metadata.name": "my-job"}).watch()
# watch is a generator:
for watch_event in watch:
print(watch_event.type) # 'ADDED', 'DELETED', 'MODIFIED'
print(watch_event.object) # pykube.Job object
Create a Deployment:
obj = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "my-deploy",
"namespace": "gondor-system"
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": {
"app": "nginx"
}
},
"template": {
"metadata": {
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx",
"ports": [
{"containerPort": 80}
]
}
]
}
}
}
}
pykube.Deployment(api, obj).create()
Delete a Deployment:
obj = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "my-deploy",
"namespace": "gondor-system"
}
}
pykube.Deployment(api, obj).delete()
Check server version:
api = pykube.HTTPClient(pykube.KubeConfig.from_file())
api.version
Requirements
- Python 3.10+
- requests (included in
install_requires) - PyYAML (included in
install_requires)
Local Development
You can run pykube against your current kubeconfig context, e.g. local Minikube:
poetry install
poetry run python3
>>> import pykube
>>> config = pykube.KubeConfig.from_file()
>>> api = pykube.HTTPClient(config)
>>> list(pykube.Deployment.objects(api))
To run PEP8 (flake8) checks and unit tests including coverage report:
make test
License
The code in this project is licensed under the Apache License, version 2.0
Contributing
Easiest way to contribute is to provide feedback! We would love to hear what you like and what you think is missing. Create an issue and we will take a look. PRs are welcome.
Code of Conduct
In order to foster a kind, inclusive, and harassment-free community, this project follows the Contributor Covenant Code of Conduct.
Acknowledgments
Thanks to pykube-ng project authored by Henning Jacobs.
Metadata
Release files for new-pykube 26.10.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| new_pykube-26.10.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / new_pykube-26.10.0-py3-none-any.whl
| Download URL | new_pykube-26.10.0-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
07ed4c48aa00b86b697b0d7a9dad7ac351ab21fc53c582a0d88de29120bf0a35
|
|
BLAKE2b-256 checksum How to use checksums |
fc436aa715ed18bb8f42c1b22fe8984c07370a80ee93fb188e87a01ef156158b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.22
|