Lightweight kubernetes client library
Project description
lightkube
Modern lightweight kubernetes module for python
NOTICE: This project is still under development and not suitable for production usage.
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.15 to 1.22)
- 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.
Installation
This module requires python >= 3.6
pip install lightkube
Usage
Read a pod
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
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
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
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
config.data['key1'] = 'new value'
client.replace(config)
Patch an existing config
patch = {'metadata': {'labels': {'app': 'xyz'}}}
client.patch(ConfigMap, name='my-config', namespace='default', obj=patch)
Delete a namespaced resource
client.delete(ConfigMap, name='my-config', namespace='default')
Create resources defined in a file
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
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, 'metrics-server', namespace='kube-system')
Stream pod logs
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
andproxy
. auth-provider
authentication method is not supported. The supported authentication methods aretoken
,username
+password
andexec
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
lightkube-0.8.1.tar.gz
(20.5 kB
view hashes)
Built Distribution
lightkube-0.8.1-py3-none-any.whl
(23.9 kB
view hashes)
Close
Hashes for lightkube-0.8.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25d000ce909d7eb564bc5a27e056874b089055e3065bc6bf0269c8b27e46947e |
|
MD5 | 0c55913a06d662d1b505269e575f8bd5 |
|
BLAKE2b-256 | 357dab5f938bce1605fc418c7c149ef316857e059bccb3d96e88a83fb623bbcd |