Skip to main content

lightkube

Coverage Status pypi supported versions

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.

Installation

This module requires python >= 3.7

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 adding a label

patch = {'metadata': {'labels': {'app': 'xyz'}}}
client.patch(ConfigMap, name='my-config', namespace='default', obj=patch)

Remove the label app

# 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

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)

Create and modify resources using 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.

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

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.

Metadata

Release files for lightkube 0.15.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lightkube 0.15.5
File Size Uploaded Provenance Actions
lightkube-0.15.5.tar.gz 44.4 kB Without provenance Details for lightkube-0.15.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lightkube 0.15.5
File Interpreter ABI Platform Actions
lightkube-0.15.5-py3-none-any.whl Python 3 none any Details for lightkube-0.15.5-py3-none-any.whl

Total release size: 82.6 kB

Release files / lightkube-0.15.5.tar.gz

Download URL lightkube-0.15.5.tar.gz
Size 44.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5edbfd1aee83398374179f41f4897519e8f89dc9754c866d40bbdc68c49c033f
BLAKE2b-256 checksum
How to use checksums
5d061e38e6c4f99369786380b321c795c8f29ea78997e34bf8f965e82faf7ead
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.12

Release files / lightkube-0.15.5-py3-none-any.whl

Download URL lightkube-0.15.5-py3-none-any.whl
Size 38.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d93be743cbeae022d18a1d3fbb45d1df58f9a603ea0061237842658f68d93fd
BLAKE2b-256 checksum
How to use checksums
d3cc916cbf49ebad6dc190f1b51fbd42dc5b3258b9f4660586171d64952ace7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.12

Security for lightkube 0.15.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.15.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page