# lightkube
![](https://img.shields.io/github/actions/workflow/status/gtsystem/lightkube/python-package.yml?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/gtsystem/lightkube/badge.svg?branch=master)](https://coveralls.io/github/gtsystem/lightkube?branch=master)
[![pypi supported versions](https://img.shields.io/pypi/pyversions/lightkube.svg)](https://pypi.python.org/pypi/lightkube)
Modern lightweight kubernetes module for python
## Highlights
* *Simple* interface shared across all kubernetes APIs.
* Extensive *type hints* to avoid common mistakes and to support autocompletion.
* Models and resources generated from the swagger specifications using standard dataclasses.
* Load/Dump resource objects from YAML.
* Support for async/await
* Support for installing a specific version of the kubernetes models (1.16 to 1.31)
* Lazy instantiation of inner models.
* Fast startup and small memory footprint as only needed models and resources can be imported.
* Automatic handling of pagination when listing resources.
This module is powered by [httpx](https://github.com/encode/httpx/tree/master/httpx).
## Installation
This module requires python >= 3.7
pip install lightkube
## Usage
Read a pod
```python
from lightkube import Client
from lightkube.resources.core_v1 import Pod
client = Client()
pod = client.get(Pod, name="my-pod", namespace="default")
print(pod.namespace.uid)
```
List nodes
```python
from lightkube import Client
from lightkube.resources.core_v1 import Node
client = Client()
for node in client.list(Node):
print(node.metadata.name)
```
Watch deployments
```python
from lightkube import Client
from lightkube.resources.apps_v1 import Deployment
client = Client()
for op, dep in client.watch(Deployment, namespace="default"):
print(f"{dep.namespace.name} {dep.spec.replicas}")
```
Create a config map
```python
from lightkube.resources.core_v1 import ConfigMap
from lightkube.models.meta_v1 import ObjectMeta
config = ConfigMap(
metadata=ObjectMeta(name='my-config', namespace='default'),
data={'key1': 'value1', 'key2': 'value2'}
)
client.create(config)
```
Replace the previous config with a different content
```python
config.data['key1'] = 'new value'
client.replace(config)
```
Patch an existing config adding a label
```python
patch = {'metadata': {'labels': {'app': 'xyz'}}}
client.patch(ConfigMap, name='my-config', namespace='default', obj=patch)
```
Remove the label `app`
```python
# When using PatchType.STRATEGIC (default), setting a value of a key/value to None, will remove the current item
patch = {'metadata': {'labels': {'app': None}}}
client.patch(ConfigMap, name='my-config', namespace='default', obj=patch)
```
Delete a namespaced resource
```python
client.delete(ConfigMap, name='my-config', namespace='default')
```
Create resources defined in a file
```python
from lightkube import Client, codecs
client = Client()
with open('deployment.yaml') as f:
for obj in codecs.load_all_yaml(f):
client.create(obj)
```
Scale a deployment
```python
from lightkube.resources.apps_v1 import Deployment
from lightkube.models.meta_v1 import ObjectMeta
from lightkube.models.autoscaling_v1 import ScaleSpec
obj = Deployment.Scale(
metadata=ObjectMeta(name='metrics-server', namespace='kube-system'),
spec=ScaleSpec(replicas=1)
)
client.replace(obj)
```
Create and modify resources using [server side apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/)
*Note:* `field_manager` is required for server-side apply. You can specify it once in the client constructor
or when calling `apply()`. Also `apiVersion` and `kind` need to be provided as part of
the object definition.
```python
from lightkube.resources.core_v1 import ConfigMap
from lightkube.models.meta_v1 import ObjectMeta
client = Client(field_manager="my-manager")
config = ConfigMap(
# note apiVersion and kind need to be specified for server-side apply
apiVersion='v1', kind='ConfigMap',
metadata=ObjectMeta(name='my-config', namespace='default'),
data={'key1': 'value1', 'key2': 'value2'}
)
res = client.apply(config)
print(res.data)
# prints {'key1': 'value1', 'key2': 'value2'}
del config.data['key1']
config.data['key3'] = 'value3'
res = client.apply(config)
print(res.data)
# prints {'key2': 'value2', 'key3': 'value3'}
```
Stream pod logs
```python
from lightkube import Client
client = Client()
for line in client.log('my-pod', follow=True):
print(line)
```
## Unsupported features
The following features are not supported at the moment:
* Special subresources `attach`, `exec`, `portforward` and `proxy`.
* `auth-provider` authentication method is not supported. The supported
authentication methods are `token`, `username` + `password` and `exec`.
Raw data
{
"_id": null,
"home_page": "https://github.com/gtsystem/lightkube",
"name": "lightkube",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Giuseppe Tribulato",
"author_email": "gtsystem@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/5d/06/1e38e6c4f99369786380b321c795c8f29ea78997e34bf8f965e82faf7ead/lightkube-0.15.5.tar.gz",
"platform": null,
"description": "# lightkube\n\n![](https://img.shields.io/github/actions/workflow/status/gtsystem/lightkube/python-package.yml?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/gtsystem/lightkube/badge.svg?branch=master)](https://coveralls.io/github/gtsystem/lightkube?branch=master)\n[![pypi supported versions](https://img.shields.io/pypi/pyversions/lightkube.svg)](https://pypi.python.org/pypi/lightkube)\n\nModern lightweight kubernetes module for python\n\n\n## Highlights\n\n* *Simple* interface shared across all kubernetes APIs.\n* Extensive *type hints* to avoid common mistakes and to support autocompletion.\n* Models and resources generated from the swagger specifications using standard dataclasses.\n* Load/Dump resource objects from YAML.\n* Support for async/await\n* Support for installing a specific version of the kubernetes models (1.16 to 1.31)\n* Lazy instantiation of inner models.\n* Fast startup and small memory footprint as only needed models and resources can be imported.\n* Automatic handling of pagination when listing resources.\n\nThis module is powered by [httpx](https://github.com/encode/httpx/tree/master/httpx). \n\n## Installation\n\nThis module requires python >= 3.7 \n\n pip install lightkube\n\n## Usage\n\nRead a pod\n\n```python\nfrom lightkube import Client\nfrom lightkube.resources.core_v1 import Pod\n\nclient = Client()\npod = client.get(Pod, name=\"my-pod\", namespace=\"default\")\nprint(pod.namespace.uid)\n```\n\nList nodes\n```python\nfrom lightkube import Client\nfrom lightkube.resources.core_v1 import Node\n\nclient = Client()\nfor node in client.list(Node):\n print(node.metadata.name)\n```\n\nWatch deployments\n```python\nfrom lightkube import Client\nfrom lightkube.resources.apps_v1 import Deployment\n\nclient = Client()\nfor op, dep in client.watch(Deployment, namespace=\"default\"):\n print(f\"{dep.namespace.name} {dep.spec.replicas}\")\n```\n\nCreate a config map\n```python\nfrom lightkube.resources.core_v1 import ConfigMap\nfrom lightkube.models.meta_v1 import ObjectMeta\n\nconfig = ConfigMap(\n metadata=ObjectMeta(name='my-config', namespace='default'),\n data={'key1': 'value1', 'key2': 'value2'}\n)\n\nclient.create(config)\n```\n\nReplace the previous config with a different content\n```python\nconfig.data['key1'] = 'new value'\nclient.replace(config)\n```\n\nPatch an existing config adding a label\n```python\npatch = {'metadata': {'labels': {'app': 'xyz'}}}\nclient.patch(ConfigMap, name='my-config', namespace='default', obj=patch)\n```\n\nRemove the label `app`\n```python\n# When using PatchType.STRATEGIC (default), setting a value of a key/value to None, will remove the current item \npatch = {'metadata': {'labels': {'app': None}}}\nclient.patch(ConfigMap, name='my-config', namespace='default', obj=patch)\n```\n\nDelete a namespaced resource\n```python\nclient.delete(ConfigMap, name='my-config', namespace='default')\n```\n\nCreate resources defined in a file\n```python\nfrom lightkube import Client, codecs\n\nclient = Client()\nwith open('deployment.yaml') as f:\n for obj in codecs.load_all_yaml(f):\n client.create(obj)\n```\n\nScale a deployment\n```python\nfrom lightkube.resources.apps_v1 import Deployment\nfrom lightkube.models.meta_v1 import ObjectMeta\nfrom lightkube.models.autoscaling_v1 import ScaleSpec\n\nobj = Deployment.Scale(\n metadata=ObjectMeta(name='metrics-server', namespace='kube-system'),\n spec=ScaleSpec(replicas=1)\n)\nclient.replace(obj)\n```\n\nCreate and modify resources using [server side apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/)\n\n*Note:* `field_manager` is required for server-side apply. You can specify it once in the client constructor\nor when calling `apply()`. Also `apiVersion` and `kind` need to be provided as part of\nthe object definition.\n\n```python\nfrom lightkube.resources.core_v1 import ConfigMap\nfrom lightkube.models.meta_v1 import ObjectMeta\n\nclient = Client(field_manager=\"my-manager\")\nconfig = ConfigMap(\n # note apiVersion and kind need to be specified for server-side apply\n apiVersion='v1', kind='ConfigMap',\n metadata=ObjectMeta(name='my-config', namespace='default'),\n data={'key1': 'value1', 'key2': 'value2'}\n)\n\nres = client.apply(config)\nprint(res.data)\n# prints {'key1': 'value1', 'key2': 'value2'}\n\ndel config.data['key1']\nconfig.data['key3'] = 'value3'\n\nres = client.apply(config)\nprint(res.data)\n# prints {'key2': 'value2', 'key3': 'value3'}\n```\n\nStream pod logs\n```python\nfrom lightkube import Client\n\nclient = Client()\nfor line in client.log('my-pod', follow=True):\n print(line)\n```\n\n## Unsupported features\n\nThe following features are not supported at the moment:\n\n* Special subresources `attach`, `exec`, `portforward` and `proxy`.\n* `auth-provider` authentication method is not supported. The supported\n authentication methods are `token`, `username` + `password` and `exec`.",
"bugtrack_url": null,
"license": "MIT",
"summary": "Lightweight kubernetes client library",
"version": "0.15.5",
"project_urls": {
"Homepage": "https://github.com/gtsystem/lightkube"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d3cc916cbf49ebad6dc190f1b51fbd42dc5b3258b9f4660586171d64952ace7f",
"md5": "831fd34412abf812da42c39ce1624558",
"sha256": "0d93be743cbeae022d18a1d3fbb45d1df58f9a603ea0061237842658f68d93fd"
},
"downloads": -1,
"filename": "lightkube-0.15.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "831fd34412abf812da42c39ce1624558",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 38188,
"upload_time": "2024-11-06T08:32:08",
"upload_time_iso_8601": "2024-11-06T08:32:08.325404Z",
"url": "https://files.pythonhosted.org/packages/d3/cc/916cbf49ebad6dc190f1b51fbd42dc5b3258b9f4660586171d64952ace7f/lightkube-0.15.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5d061e38e6c4f99369786380b321c795c8f29ea78997e34bf8f965e82faf7ead",
"md5": "a21766c516e0ee581d9714b4dbedbe27",
"sha256": "5edbfd1aee83398374179f41f4897519e8f89dc9754c866d40bbdc68c49c033f"
},
"downloads": -1,
"filename": "lightkube-0.15.5.tar.gz",
"has_sig": false,
"md5_digest": "a21766c516e0ee581d9714b4dbedbe27",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 44410,
"upload_time": "2024-11-06T08:32:06",
"upload_time_iso_8601": "2024-11-06T08:32:06.141487Z",
"url": "https://files.pythonhosted.org/packages/5d/06/1e38e6c4f99369786380b321c795c8f29ea78997e34bf8f965e82faf7ead/lightkube-0.15.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-06 08:32:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gtsystem",
"github_project": "lightkube",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [],
"lcname": "lightkube"
}