Package: pykube-ng
Pykube (pykube-ng) is a lightweight Python 3.6+ client library for Kubernetes.This is a fork ofkelproject/pykubewhich 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, seekelproject.comor follow us on Twitter@projectkel.FeaturesHTTP interface using requests using kubeconfig for authenticationPython native querying of Kubernetes API objectsInstallationTo install pykube, use pip:pip install pykube-ngInteractive ConsoleThepykubelibrary module can be run as an interactive console locally for quick exploration.
It will automatically load~/.kube/configto provide theapiobject, and it loads pykube classes (Deployment,Pod, ..) into local context:python3-mpykube>>>[d.namefordinDeployment.objects(api)]UsageQuery for all ready pods in a custom namespace:importoperatorimportpykubeapi=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:forwatch_eventinwatch:print(watch_event.type)# 'ADDED', 'DELETED', 'MODIFIED'print(watch_event.object)# pykube.Job objectCreate 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.versionRequirementsPython 3.6+requests (included ininstall_requires)PyYAML (included ininstall_requires)Local DevelopmentYou can run pykube against your current kubeconfig context, e.g. localMinikube:poetryinstallpoetryrunpython3>>>importpykube>>>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:maketestLicenseThe code in this project is licensed under the Apache License, version 2.0
(included in this repository under LICENSE).ContributingEasiest way to contribute is to provide feedback! We would love to hear what you like and what you think is missing.
Create an issue orping try_except_ on Twitter.PRs are welcome. Please also have a look atissues labeled with “help wanted”.Code of ConductIn order to foster a kind, inclusive, and harassment-free community, this project follows theContributor Covenant Code of Conduct.