container-runtime-interface-api
Python library for communication with the Kubernetes Container Runtime Interface API.
Usage
Install container-runtime-interface-api with pipenv or pip:
$ pipenv install container-runtime-interface-api
Adding container-runtime-interface-api to Pipfile's [packages]…
✔ Installation Succeeded
...
This project currently supports Python 3.10+.
Connection
Connection to the CRI API is generally done through a UNIX socket, but any gRPC address supported by insecure_channel will work.
from cri_api.channel import Channel
channel = Channel.from_env() # Loads from RUNTIME_SOCK
channel = Channel("unix:///var/run/dockershim.sock") # Explicit argument
Images
The Images class is a thin wrapper around the existing ImageService API:
from cri_api.images import Images
channel = Channel.from_env()
images = Images(channel)
images.list_images()
images.pull_image("busybox")
busybox_images = [i["id"] for i in images.list_images() if any("busybox" in r for r in i["repoTags"])]
[images.remove_image(i) for i in busybox_images]
Containers
The Containers class is a thin wrapper around the existing RuntimeService API:
from cri_api.images import Images
from cri_api import ContainerFilter, ContainerState, ContainerStateValue
channel = Channel.from_env()
images = Containers(channel)
containers.list_containers()
containers.list_containers(ContainerFilter(state=ContainerStateValue(state=ContainerState.CONTAINER_EXITED)))
containers.get_container("9d81052cc027a1fb2ec61b898ea0fd6fc88216ce730ad75f4c52b29849cb440f")
Raw
Raw access to the underlying CRI API objects can be done by importing from cri_api:
from os import getenv
from grpc import insecure_channel
from cri_api import RuntimeServiceStub, ListContainersRequest
stub = RuntimeServiceStub(insecure_channel(getenv("RUNTIME_SOCK")))
response = stub.ListContainers(ListContainersRequest())
containers = response.containers
Updating Protobuf Python Generated Code
$ bin/update-proto.sh
Commit & create a new pull request!
Development
Interactive development on MacOS can be done by leveraging minikube:
$ minikube start --container-runtime=cri-o
$ minikube ssh
$ socat -d -d TCP4-LISTEN:15432,fork UNIX-CONNECT:/var/run/crio/crio.sock
# In another window, you can now connect on $(minikube ip):15432
$ export RUNTIME_SOCK=$(minikube ip):15432
...
Testing
Unit tests are run against all supported Python versions: 3.10, 3.11, and 3.12.
A basic set of integration tests is included alongside the unit tests, but are only run if the RUNTIME_SOCK environment variable is set correctly.
They are verified in CI against a minikube installation running Docker and dockershim.
Run unit tests with:
$ pytest
Metadata
Release files for container-runtime-interface-api 2.0.1
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 | Actions |
|---|---|---|---|---|
| container_runtime_interface_api-2.0.1.tar.gz | 100.7 kB | Without provenance | Details for container_runtime_interface_api-2.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| container_runtime_interface_api-2.0.1-py3-none-any.whl | Python 3 | none | any | Details for container_runtime_interface_api-2.0.1-py3-none-any.whl |
Total release size: 203.4 kB
Release files / container_runtime_interface_api-2.0.1.tar.gz
| Download URL | container_runtime_interface_api-2.0.1.tar.gz |
|---|---|
| Size | 100.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
04684bc160c2c5ad1937057f0dbe3b30b58da85cb7ab96bea0b2a037221297aa
|
|
BLAKE2b-256 checksum How to use checksums |
8b66beb7eee12926644243c4d1d51e9c9fe88430dc1b3e8b115eb047526ddb6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.18
|
Release files / container_runtime_interface_api-2.0.1-py3-none-any.whl
| Download URL | container_runtime_interface_api-2.0.1-py3-none-any.whl |
|---|---|
| Size | 102.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c48c7492585b4387bd704ea30907ccd0200dcd845eb89eb2ddee1582c2845df
|
|
BLAKE2b-256 checksum How to use checksums |
da81591b7dd9329ad8d26f75a167745985a5b2f67d5258e00a6f7ecd43a9f1da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.18
|
Security for container-runtime-interface-api 2.0.1
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.