跳转到主要内容

用于与Kubernetes容器运行时接口API交互的Python库。

项目描述

container-runtime-interface-api

与Kubernetes 容器运行时接口API通信的Python库。

用法

使用pipenvpip安装container-runtime-interface-api

$ pipenv install container-runtime-interface-api
Adding container-runtime-interface-api to Pipfile's [packages]…
✔ Installation Succeeded
...

此项目目前支持Python 3.7+。

连接

通常通过UNIX套接字连接到CRI API,但任何由insecure_channel支持的gRPC地址都将工作。

from cri_api.channel import Channel
channel = Channel.from_env() # Loads from RUNTIME_SOCK
channel = Channel("unix:///var/run/dockershim.sock") # Explicit argument

镜像

Images类是对现有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类是对现有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")

原始

可以通过从cri_api导入来实现对底层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

更新Protobuf Python生成的代码

$ bin/update-proto.sh

提交并创建新的pull请求!

开发

可以在MacOS上利用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
...

测试

针对所有支持的Python版本(3.7、3.8和3.9)运行单元测试。

集成测试的基本集合包含在单元测试中,但只有在正确设置RUNTIME_SOCK环境变量时才会运行。它们在CI环境中与运行Docker和dockershim的minikube安装进行验证。

使用以下命令运行单元测试

$ pytest

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

container_runtime_interface_api-2.0.0.tar.gz (87.8 kB 查看哈希值)

上传时间

构建分发

container_runtime_interface_api-2.0.0-py3-none-any.whl (89.3 kB 查看哈希值)

上传时间 Python 3

由支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面