|
||
---|---|---|
docs | ||
pykube | ||
tests | ||
.flake8 | ||
.gitignore | ||
.pre-commit-config.yaml | ||
.tools-version | ||
.travis.yml | ||
CHANGELOG.md | ||
LICENSE | ||
Makefile | ||
README.rst | ||
poetry.lock | ||
pyproject.toml |
README.rst
pykube-ng
Pykube (pykube-ng) is a lightweight Python 3.6+ client library for Kubernetes.
This is a fork of kelproject/pykube which is no longer maintained (archived). Here the original text of the pykube README:
Kel is an open source Platform as a Service (PaaS) from Eldarion, Inc. that makes it easy to manage web application deployment and hosting through the entire lifecycle from development through testing to production. It adds components and tools on top of Kubernetes that help developers manage their application infrastructure. Kel builds on Eldarion's 7+ years experience running one of the leading Python and Django PaaSes. For more information about Kel, see kelproject.com or follow us on Twitter @projectkel.
Features
- HTTP interface using requests using kubeconfig for authentication
- Python native querying of Kubernetes API objects
Installation
To install pykube, use pip:
pip install pykube-ng
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
= pykube.HTTPClient(pykube.KubeConfig.from_file())
api = pykube.Pod.objects(api).filter(namespace="gondor-system")
pods = filter(operator.attrgetter("ready"), pods) ready_pods
Access any attribute of the Kubernetes object:
= pykube.Pod.objects(api).filter(namespace="gondor-system").get(name="my-pod")
pod "spec"]["containers"][0]["image"] pod.obj[
Selector query:
= pykube.Pod.objects(api).filter(
pods ="gondor-system",
namespace={"gondor.io/name__in": {"api-web", "api-worker"}},
selector
)= pykube.objects.Pod.objects(api).filter(
pending_pods ={"status.phase": "Pending"}
field_selector )
Watch query:
= pykube.Job.objects(api, namespace="gondor-system")
watch = watch.filter(field_selector={"metadata.name": "my-job"}).watch()
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:
= pykube.HTTPClient(pykube.KubeConfig.from_file())
api api.version
Requirements
- Python 3.6+
- 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 (included in this repository under LICENSE).
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 or ping try_except_ on Twitter.
PRs are welcome. Please also have a look at issues labeled with "help wanted".
Code of Conduct
In order to foster a kind, inclusive, and harassment-free community, this project follows the Contributor Covenant Code of Conduct.