Kube CRD
The primary purpose of this project is to simplify working with Kubernetes Custom Resources. To achieve that it provides a base class, kubecrd.KubeResourceBase that can create Python dataclassses into Kubernetes Custom Resources and also generate and install Custom Resource Definitions for those resource into the K8s cluster directly.
>>> from dataclasses import dataclass, field >>> from uuid import UUID >>> from kubecrd import KubeResourceBase >>> from apischema import schema>>> @dataclass ... class Resource(KubeResourceBase): ... __group__ = 'example.com' ... __version__ = 'v1alpha1' ... ... name: str ... tags: list[str] = field( ... default_factory=list, ... metadata=schema( ... description='regroup multiple resources', ... unique=False, ... ), ... )>>> print(Resource.crd_schema()) apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: resources.example.com spec: group: example.com names: kind: Resource plural: resources singular: resource scope: Namespaced versions: - name: v1alpha1 schema: openAPIV3Schema: properties: spec: properties: name: type: string tags: default: [] description: regroup multiple resources items: type: string type: array uniqueItems: false required: - name type: object type: object served: true storage: true <BLANKLINE>
Create CRD in K8s Cluster
It is also possible to install the CRD in a cluster using a Kubernetes Client object:
from from kubernetes import client, config config.load_kube_config() k8s_client = client.ApiClient() Resource.install(k8s_client)
You can then find the resource in the cluster:
» kubectl get crds/resources.example.com NAME CREATED AT resources.example.com 2022-03-20T03:58:25Z $ kubectl api-resources | grep example.com resources example.com/v1alpha1 true Resource
Installation of resource is idempotent, so re-installing an already installed resource doesn’t raise any exceptions if exist_ok=True is passed in:
Resource.install(k8s_client, exist_ok=True)
Serialization
You can serialize a Resource such that it is suitable to POST to K8s:
>>> example = Resource(name='myResource', tags=['tag1', 'tag2'])
>>> import json
>>> print(json.dumps(example.serialize(), sort_keys=True, indent=4))
{
"apiVersion": "example.com/v1alpha1",
"kind": "Resource",
"metadata": {
"name": "..."
},
"spec": {
"name": "myResource",
"tags": [
"tag1",
"tag2"
]
}
}
Objects can also be serialized and saved directly in K8s:
example.save(k8s_client)
Where client in the above is a Kubernetes client object. You can also use asyncio with kubernetes_asyncio client and instead do:
await example.async_save(k8s_async_client)
Deserialization
You can deserialize the JSON from Kubernetes API into Python CR objects.
$ cat -p testdata/cr.json
{
"apiVersion": "example.com/v1alpha1",
"kind": "Resource",
"metadata": {
"generation": 1,
"name": "myresource1",
"namespace": "default",
"resourceVersion": "105572812",
"uid": "02102eb3-968b-418a-8023-75df383daa3c"
},
"spec": {
"name": "bestID",
"tags": [
"tag1",
"tag2"
]
}
}
by using from_json classmethod on the resource:
>>> import json
>>> with open('testdata/cr.json') as fd:
... json_schema = json.load(fd)
>>> res = Resource.from_json(json_schema)
>>> print(res.name)
bestID
>>> print(res.tags)
['tag1', 'tag2']
This also loads the Kubernetes’s V1ObjectMeta and sets it as the .metadata property of CR:
>>> print(res.metadata.namespace) default >>> print(res.metadata.name) myresource1 >>> print(res.metadata.resource_version) 105572812
Watch
It is possible to Watch for changes in Custom Resources using the standard Watch API in Kubernetes. For example, to watch for all changes in Resources:
async for happened, resource in Resource.async_watch(k8s_async_client):
print(f'Resource {resource.metadata.name} was {happened}')
Or you can use the block sync API for the watch:
for happened, resource in Resource.watch(k8s_client):
print(f'Resource {resource.metadata.name} was {happened}')
Installing
Kube CRD can be install from PyPI using pip or your favorite tool:
$ pip install kubecrd
Metadata
Release files for kubecrd 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| kubecrd-0.5.0.tar.gz | 10.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kubecrd-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.8 kB
Release files / kubecrd-0.5.0.tar.gz
| Download URL | kubecrd-0.5.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
09c903cbb47c66b6ded706a14919d61e87a6753fb425e5b213c3c3fcbb97c320
|
|
BLAKE2b-256 checksum How to use checksums |
44f3dec68c8cc004c0c139e4dca40b6a08f68f8544806a2bf2ead45b5fae15e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12
|
Release files / kubecrd-0.5.0-py3-none-any.whl
| Download URL | kubecrd-0.5.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f3cc3d3e60ab1e830bb3bba7206015320901d9f3a355d970dcb17886ab321f11
|
|
BLAKE2b-256 checksum How to use checksums |
0c627a9f04d82a6c54c0e280aa2da8519ecb3f913048005a6584cb71002acc95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12
|
Security for kubecrd 0.5.0
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.