lightkube-models
This Python module provides definitions of Kubernetes models and resources for use with the `lightkube` client library. The version of this package (major.minor.micro) directly corresponds to the Kubernetes API schema version it defines. Users should install the module version that matches their target Kubernetes installation or ensure compatibility. It is currently active, with new versions released to support the latest Kubernetes API schemas.
Common errors
-
ModuleNotFoundError: No module named 'lightkube.models.Pod'
cause Attempting to import a Kubernetes model directly from the top-level `lightkube.models` package instead of its versioned API group submodule.fixModels are organized by Kubernetes API group and version. You need to import from the specific module, e.g., `from lightkube.models.core_v1 import Pod` for a Pod model, or `from lightkube.models.apps_v1 import Deployment` for a Deployment model. -
lightkube.core.exceptions.LoadResourceError: No module named 'lightkube.resources.stable_example_com_v1'
cause This error occurs when trying to load a Custom Resource Definition (CRD) object from a YAML file or dictionary using `lightkube.codecs.load_all_yaml()` without `lightkube` being aware of that custom resource's schema. `lightkube-models` only includes standard Kubernetes resources.fixFor Custom Resources, you need to explicitly define a generic resource class using `lightkube.generic_resource.create_namespaced_resource` or `create_global_resource` before attempting to load or interact with instances of that CRD. Alternatively, `lightkube.generic_resource.load_in_cluster_generic_resources()` can load all CRDs from a cluster dynamically.
Warnings
- gotcha The version of `lightkube-models` (first three parts: major.minor.micro) directly corresponds to the Kubernetes API schema version it encapsulates. Mismatching this version with your Kubernetes cluster's version can lead to schema validation errors or missing fields.
- gotcha `lightkube.models` provides pure dataclass representations of Kubernetes schemas. The `lightkube` client (from the `lightkube` library) often expects `lightkube.resources` classes, which are subclasses of models with additional metadata for client operations.
- breaking Prior to `lightkube-models` version 1.33, resource models did not automatically set `apiVersion` and `kind` during instantiation. These fields had to be manually provided when creating objects.
Install
-
pip install lightkube-models
Imports
- ObjectMeta
from lightkube.models import ObjectMeta
from lightkube.models.meta_v1 import ObjectMeta
- PodSpec
from lightkube.models.core_v1 import PodSpec
Quickstart
from lightkube.models.meta_v1 import ObjectMeta
from lightkube.models.core_v1 import PodSpec, Container
# Create a simple ObjectMeta instance
metadata = ObjectMeta(name='my-pod', namespace='default', labels={'app': 'my-app'})
print(f"Created ObjectMeta: {metadata.name} in {metadata.namespace} with labels {metadata.labels}")
# Create a PodSpec
container = Container(name='nginx', image='nginx:latest')
pod_spec = PodSpec(containers=[container])
print(f"Created PodSpec with container: {pod_spec.containers[0].name}")