Skip to main content

Python library for interaction with the Kubernetes container runtime interface API.

Project description

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.8+.

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-protos.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
...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

container-runtime-interface-api-0.0.5.tar.gz (33.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file container-runtime-interface-api-0.0.5.tar.gz.

File metadata

  • Download URL: container-runtime-interface-api-0.0.5.tar.gz
  • Upload date:
  • Size: 33.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for container-runtime-interface-api-0.0.5.tar.gz
Algorithm Hash digest
SHA256 1248bc48aac0403510092b70b37ba047d5a3303680a9b3f43ec09d5d3d1318c6
MD5 85625e0cacaa94f47438a52ee66414b0
BLAKE2b-256 f32e9b4500c291d4dd8189808cb311d8237fe956ff26ce331f423a18db5426a6

See more details on using hashes here.

File details

Details for the file container_runtime_interface_api-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: container_runtime_interface_api-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 39.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for container_runtime_interface_api-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2c965be93248c12aef26a45f5acc2154a8fc0ac95e807e30487e6362bc8ec791
MD5 b4751ccc4d7c75d48bd973783298da4d
BLAKE2b-256 1540763f165b0f36d2b1d2b8f3ac580723d699bcd1f757c77db783785919b94e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page