Skip to main content

Python client library for Kubernetes

Project description

Build status Code Coverage Documentation PyPI version Python versions Apache License

Pykube (pykube-ng) is a lightweight Python 3+ 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

api = pykube.HTTPClient(pykube.KubeConfig.from_file("~/.kube/config"))
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.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:

pipenv install --dev
pipenv 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.

Project details


Download files

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

Source Distribution

pykube-ng-0.28.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

pykube_ng-0.28-py2.py3-none-any.whl (22.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pykube-ng-0.28.tar.gz.

File metadata

  • Download URL: pykube-ng-0.28.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pykube-ng-0.28.tar.gz
Algorithm Hash digest
SHA256 620d7105c648cd32d4bc9225d410a875209fa357ffb2d099c5d35330fb4e1721
MD5 674315a980c7a4dfb7d1395f3abef408
BLAKE2b-256 932eda38cd51cc34f66599da5fcc2ba7fec96591adbb14a0a3427100dde7a235

See more details on using hashes here.

File details

Details for the file pykube_ng-0.28-py2.py3-none-any.whl.

File metadata

  • Download URL: pykube_ng-0.28-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pykube_ng-0.28-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5e2cb153c1f01a77e2ff66fa1e53e29098c715efec4ac415802ce4b244fcd142
MD5 8cb62b64d329db009eac044938421555
BLAKE2b-256 053575b290fbb76258bb090805d8ddd5b415e84af261b619ba54d68ca5b3a7de

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