Commit 18eee1ea authored by Vishnu kannan's avatar Vishnu kannan

Make AllocateResponse artifacts global across all devices per container in device plugin API

There is no use case known for passing artifacts per device as it currently exists. The current API is also complex to use for simple clients. Hence this PR creates a flat namespace where artifacts like environment variables and mount points apply globally to all devices returned as part of AllocateResponse proto. Signed-off-by: 's avatarVishnu kannan <vishnuk@google.com>
parent 900c0761
...@@ -81,25 +81,21 @@ message AllocateRequest { ...@@ -81,25 +81,21 @@ message AllocateRequest {
repeated string devicesIDs = 1; repeated string devicesIDs = 1;
} }
// AllocateResponse includes the artifacts that needs to be injected into
// a container for accessing 'deviceIDs' that were mentioned as part of
// 'AllocateRequest'.
// Failure Handling: // Failure Handling:
// if Kubelet sends an allocation request for dev1 and dev2. // if Kubelet sends an allocation request for dev1 and dev2.
// Allocation on dev1 succeeds but allocation on dev2 fails. // Allocation on dev1 succeeds but allocation on dev2 fails.
// The Device plugin should send a ListAndWatch update and fail the // The Device plugin should send a ListAndWatch update and fail the
// Allocation request // Allocation request
message AllocateResponse { message AllocateResponse {
repeated DeviceRuntimeSpec spec = 1; // List of environment variable to be set in the container to access one of more devices.
} map<string, string> envs = 1;
// The list to be added to the CRI spec
message DeviceRuntimeSpec {
// ID of the Device
string ID = 1;
// List of environment variable to set in the container.
map<string, string> envs = 2;
// Mounts for the container. // Mounts for the container.
repeated Mount mounts = 3; repeated Mount mounts = 2;
// Devices for the container // Devices for the container.
repeated DeviceSpec devices = 4; repeated DeviceSpec devices = 3;
} }
// Mount specifies a host volume to mount into a container. // Mount specifies a host volume to mount into a container.
......
...@@ -628,56 +628,60 @@ func (cm *containerManagerImpl) GetResources(pod *v1.Pod, container *v1.Containe ...@@ -628,56 +628,60 @@ func (cm *containerManagerImpl) GetResources(pod *v1.Pod, container *v1.Containe
} }
// Loops through AllocationResponses of all required extended resources. // Loops through AllocationResponses of all required extended resources.
for _, resp := range allocResps { for _, resp := range allocResps {
// Loops through runtime spec of all devices of the given resource. // Each Allocate response has the following artifacts.
for _, devRuntime := range resp.Spec { // Environment variables
// Updates RunContainerOptions.Devices. // Mount points
for _, dev := range devRuntime.Devices { // Device files
if d, ok := devsMap[dev.ContainerPath]; ok { // These artifacts are per resource per container.
glog.V(3).Infof("skip existing device %s %s", dev.ContainerPath, dev.HostPath) // Updates RunContainerOptions.Envs.
if d != dev.HostPath { for k, v := range resp.Envs {
glog.Errorf("Container device %s has conflicting mapping host devices: %s and %s", if e, ok := envsMap[k]; ok {
dev.ContainerPath, d, dev.HostPath) glog.V(3).Infof("skip existing envs %s %s", k, v)
} if e != v {
continue glog.Errorf("Environment variable %s has conflicting setting: %s and %s", k, e, v)
} }
devsMap[dev.ContainerPath] = dev.HostPath continue
opts.Devices = append(opts.Devices, kubecontainer.DeviceInfo{
PathOnHost: dev.HostPath,
PathInContainer: dev.ContainerPath,
Permissions: dev.Permissions,
})
} }
// Updates RunContainerOptions.Mounts. envsMap[k] = v
for _, mount := range devRuntime.Mounts { opts.Envs = append(opts.Envs, kubecontainer.EnvVar{Name: k, Value: v})
if m, ok := mountsMap[mount.ContainerPath]; ok { }
glog.V(3).Infof("skip existing mount %s %s", mount.ContainerPath, mount.HostPath)
if m != mount.HostPath { // Updates RunContainerOptions.Devices.
glog.Errorf("Container mount %s has conflicting mapping host mounts: %s and %s", for _, dev := range resp.Devices {
mount.ContainerPath, m, mount.HostPath) if d, ok := devsMap[dev.ContainerPath]; ok {
} glog.V(3).Infof("skip existing device %s %s", dev.ContainerPath, dev.HostPath)
continue if d != dev.HostPath {
glog.Errorf("Container device %s has conflicting mapping host devices: %s and %s",
dev.ContainerPath, d, dev.HostPath)
} }
mountsMap[mount.ContainerPath] = mount.HostPath continue
opts.Mounts = append(opts.Mounts, kubecontainer.Mount{
Name: mount.ContainerPath,
ContainerPath: mount.ContainerPath,
HostPath: mount.HostPath,
ReadOnly: mount.ReadOnly,
SELinuxRelabel: false,
})
} }
// Updates RunContainerOptions.Envs. devsMap[dev.ContainerPath] = dev.HostPath
for k, v := range devRuntime.Envs { opts.Devices = append(opts.Devices, kubecontainer.DeviceInfo{
if e, ok := envsMap[k]; ok { PathOnHost: dev.HostPath,
glog.V(3).Infof("skip existing envs %s %s", k, v) PathInContainer: dev.ContainerPath,
if e != v { Permissions: dev.Permissions,
glog.Errorf("Environment variable %s has conflicting setting: %s and %s", k, e, v) })
} }
continue // Updates RunContainerOptions.Mounts.
for _, mount := range resp.Mounts {
if m, ok := mountsMap[mount.ContainerPath]; ok {
glog.V(3).Infof("skip existing mount %s %s", mount.ContainerPath, mount.HostPath)
if m != mount.HostPath {
glog.Errorf("Container mount %s has conflicting mapping host mounts: %s and %s",
mount.ContainerPath, m, mount.HostPath)
} }
envsMap[k] = v continue
opts.Envs = append(opts.Envs, kubecontainer.EnvVar{Name: k, Value: v})
} }
mountsMap[mount.ContainerPath] = mount.HostPath
opts.Mounts = append(opts.Mounts, kubecontainer.Mount{
Name: mount.ContainerPath,
ContainerPath: mount.ContainerPath,
HostPath: mount.HostPath,
ReadOnly: mount.ReadOnly,
// TODO: This may need to be part of Device plugin API.
SELinuxRelabel: false,
})
} }
} }
return opts, nil return opts, nil
......
...@@ -99,9 +99,8 @@ func (m *DevicePluginManagerTestStub) Allocate(resourceName string, devIds []str ...@@ -99,9 +99,8 @@ func (m *DevicePluginManagerTestStub) Allocate(resourceName string, devIds []str
for _, id := range devIds { for _, id := range devIds {
key := resourceName + id key := resourceName + id
fmt.Printf("Alloc device %q for resource %q\n", id, resourceName) fmt.Printf("Alloc device %q for resource %q\n", id, resourceName)
devRuntime := new(pluginapi.DeviceRuntimeSpec)
for _, dev := range m.devRuntimeDevices[key] { for _, dev := range m.devRuntimeDevices[key] {
devRuntime.Devices = append(devRuntime.Devices, &pluginapi.DeviceSpec{ resp.Devices = append(resp.Devices, &pluginapi.DeviceSpec{
ContainerPath: dev.value1, ContainerPath: dev.value1,
HostPath: dev.value2, HostPath: dev.value2,
Permissions: "mrw", Permissions: "mrw",
...@@ -109,17 +108,16 @@ func (m *DevicePluginManagerTestStub) Allocate(resourceName string, devIds []str ...@@ -109,17 +108,16 @@ func (m *DevicePluginManagerTestStub) Allocate(resourceName string, devIds []str
} }
for _, mount := range m.devRuntimeMounts[key] { for _, mount := range m.devRuntimeMounts[key] {
fmt.Printf("Add mount %q %q\n", mount.value1, mount.value2) fmt.Printf("Add mount %q %q\n", mount.value1, mount.value2)
devRuntime.Mounts = append(devRuntime.Mounts, &pluginapi.Mount{ resp.Mounts = append(resp.Mounts, &pluginapi.Mount{
ContainerPath: mount.value1, ContainerPath: mount.value1,
HostPath: mount.value2, HostPath: mount.value2,
ReadOnly: true, ReadOnly: true,
}) })
} }
devRuntime.Envs = make(map[string]string) resp.Envs = make(map[string]string)
for _, env := range m.devRuntimeEnvs[key] { for _, env := range m.devRuntimeEnvs[key] {
devRuntime.Envs[env.value1] = env.value2 resp.Envs[env.value1] = env.value2
} }
resp.Spec = append(resp.Spec, devRuntime)
} }
return resp, nil return resp, nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment