kubelet源码阅读(三)——kubelet调用device plugin的allocate接口

kubernetes-master/pkg/kubelet/cm/devicemanager/endpoint.go
入参
  devs是设备ID
响应
  AllocateResponse包含了需要注入到容器中的工作件(envs环境变量、mounts挂在信息、devices设备信息、annotations注解信息、cdidevice名称),以便容器能访问到AllocateRequest中deviceIDs。
  返回的信息中可以选择使用某一种方式,例如:只返回env,就是为容器添加环境变量。nvidia/k8s-device-plugin使用为容器添加环境变量NVIDIA_VISIBLE_DEVICES的方式,支持容器使用GPU卡。nvidia也支持了使用cdi的方式。这需要container的版本支持cdi。
失败处理
  如果kubelet发送了对dev1和dev2的分配请求。dev1 的分配成功,但 dev2 的分配失败。设备插件应发送 ListAndWatch 更新并使分配请求失败。
 1 // allocate issues Allocate gRPC call to the device plugin.
 2 func (e *endpointImpl) allocate(devs []string) (*pluginapi.AllocateResponse, error) {
 3     if e.isStopped() {
 4         return nil, fmt.Errorf(errEndpointStopped, e)
 5     }
 6     return e.api.Allocate(context.Background(), &pluginapi.AllocateRequest{
 7         ContainerRequests: []*pluginapi.ContainerAllocateRequest{
 8             {DevicesIDs: devs},
 9         },
10     })
11 }
 
 1 // AllocateResponse includes the artifacts that needs to be injected into
 2 // a container for accessing 'deviceIDs' that were mentioned as part of
 3 // 'AllocateRequest'.
 4 // Failure Handling:
 5 // if Kubelet sends an allocation request for dev1 and dev2.
 6 // Allocation on dev1 succeeds but allocation on dev2 fails.
 7 // The Device plugin should send a ListAndWatch update and fail the
 8 // Allocation request
 9 type AllocateResponse struct {
10     ContainerResponses   []*ContainerAllocateResponse `protobuf:"bytes,1,rep,name=container_responses,json=containerResponses,proto3" json:"container_responses,omitempty"`
11     XXX_NoUnkeyedLiteral struct{}                     `json:"-"`
12     XXX_sizecache        int32                        `json:"-"`
13 }
14 
15 
16 type ContainerAllocateResponse struct {
17     // List of environment variable to be set in the container to access one of more devices.
18     Envs map[string]string `protobuf:"bytes,1,rep,name=envs,proto3" json:"envs,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
19     // Mounts for the container.
20     Mounts []*Mount `protobuf:"bytes,2,rep,name=mounts,proto3" json:"mounts,omitempty"`
21     // Devices for the container.
22     Devices []*DeviceSpec `protobuf:"bytes,3,rep,name=devices,proto3" json:"devices,omitempty"`
23     // Container annotations to pass to the container runtime
24     Annotations map[string]string `protobuf:"bytes,4,rep,name=annotations,proto3" json:"annotations,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
25     // CDI devices for the container.
26     CDIDevices           []*CDIDevice `protobuf:"bytes,5,rep,name=cdi_devices,json=cdiDevices,proto3" json:"cdi_devices,omitempty"`
27     XXX_NoUnkeyedLiteral struct{}     `json:"-"`
28     XXX_sizecache        int32        `json:"-"`
29 }

 

posted @ 2025-09-30 09:15  xiaoxiongfei  阅读(11)  评论(0)    收藏  举报