Package: lightkube
lightkubeModern lightweight kubernetes module for pythonHighlightsSimpleinterface shared across all kubernetes APIs.Extensivetype hintsto 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/awaitSupport for installing a specific version of the kubernetes models (1.15 to 1.29)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 byhttpx.InstallationThis module requires python >= 3.7pip install lightkubeUsageRead a podfromlightkubeimportClientfromlightkube.resources.core_v1importPodclient=Client()pod=client.get(Pod,name="my-pod",namespace="default")print(pod.namespace.uid)List nodesfromlightkubeimportClientfromlightkube.resources.core_v1importNodeclient=Client()fornodeinclient.list(Node):print(node.metadata.name)Watch deploymentsfromlightkubeimportClientfromlightkube.resources.apps_v1importDeploymentclient=Client()forop,depinclient.watch(Deployment,namespace="default"):print(f"{dep.namespace.name}{dep.spec.replicas}")Create a config mapfromlightkube.resources.core_v1importConfigMapfromlightkube.models.meta_v1importObjectMetaconfig=ConfigMap(metadata=ObjectMeta(name='my-config',namespace='default'),data={'key1':'value1','key2':'value2'})client.create(config)Replace the previous config with a different contentconfig.data['key1']='new value'client.replace(config)Patch an existing config adding a labelpatch={'metadata':{'labels':{'app':'xyz'}}}client.patch(ConfigMap,name='my-config',namespace='default',obj=patch)Remove the labelapp# When using PatchType.STRATEGIC (default), setting a value of a key/value to None, will remove the current itempatch={'metadata':{'labels':{'app':None}}}client.patch(ConfigMap,name='my-config',namespace='default',obj=patch)Delete a namespaced resourceclient.delete(ConfigMap,name='my-config',namespace='default')Create resources defined in a filefromlightkubeimportClient,codecsclient=Client()withopen('deployment.yaml')asf:forobjincodecs.load_all_yaml(f):client.create(obj)Scale a deploymentfromlightkube.resources.apps_v1importDeploymentfromlightkube.models.meta_v1importObjectMetafromlightkube.models.autoscaling_v1importScaleSpecobj=Deployment.Scale(metadata=ObjectMeta(name='metrics-server',namespace='kube-system'),spec=ScaleSpec(replicas=1))client.replace(obj)Create and modify resources usingserver side applyNote:field_manageris required for server-side apply. You can specify it once in the client constructor
or when callingapply(). AlsoapiVersionandkindneed to be provided as part of
the object definition.fromlightkube.resources.core_v1importConfigMapfromlightkube.models.meta_v1importObjectMetaclient=Client(field_manager="my-manager")config=ConfigMap(# note apiVersion and kind need to be specified for server-side applyapiVersion='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'}delconfig.data['key1']config.data['key3']='value3'res=client.apply(config)print(res.data)# prints {'key2': 'value2', 'key3': 'value3'}Stream pod logsfromlightkubeimportClientclient=Client()forlineinclient.log('my-pod',follow=True):print(line)Unsupported featuresThe following features are not supported at the moment:Special subresourcesattach,exec,portforwardandproxy.auth-providerauthentication method is not supported. The supported
authentication methods aretoken,username+passwordandexec.