isolated_device.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package device
  15. import (
  16. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/apis"
  19. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  20. "yunion.io/x/onecloud/pkg/hostman/isolated_device"
  21. )
  22. func init() {
  23. RegisterDriver(newIsolatedDevice())
  24. }
  25. type isolatedDevice struct{}
  26. func newIsolatedDevice() IDeviceDriver {
  27. return &isolatedDevice{}
  28. }
  29. func (i isolatedDevice) GetType() apis.ContainerDeviceType {
  30. return apis.CONTAINER_DEVICE_TYPE_ISOLATED_DEVICE
  31. }
  32. func (i isolatedDevice) GetRuntimeDevices(input *hostapi.ContainerCreateInput, devs []*hostapi.ContainerDevice) ([]*runtimeapi.Device, error) {
  33. if len(devs) == 0 {
  34. return nil, nil
  35. }
  36. devsMap := map[string][]*hostapi.ContainerDevice{}
  37. for _, dev := range devs {
  38. if mapDevs, ok := devsMap[dev.IsolatedDevice.DeviceType]; ok {
  39. devsMap[dev.IsolatedDevice.DeviceType] = append(mapDevs, dev)
  40. } else {
  41. devsMap[dev.IsolatedDevice.DeviceType] = []*hostapi.ContainerDevice{dev}
  42. }
  43. }
  44. ret := make([]*runtimeapi.Device, 0)
  45. for devType, mappedDevs := range devsMap {
  46. man, err := isolated_device.GetContainerDeviceManager(isolated_device.ContainerDeviceType(devType))
  47. if err != nil {
  48. return nil, errors.Wrapf(err, "GetContainerDeviceManager by type %q", devType)
  49. }
  50. for idx := range mappedDevs {
  51. mDev := mappedDevs[idx]
  52. if mDev.IsolatedDevice != nil && mDev.IsolatedDevice.OnlyEnv != nil {
  53. continue
  54. }
  55. ctrDevs, commonDevs, err := man.NewContainerDevices(input, mappedDevs[idx])
  56. if err != nil {
  57. return nil, errors.Wrapf(err, "NewContainerDevices with %#v", devs)
  58. }
  59. ret = append(ret, ctrDevs...)
  60. // commonDevs add once
  61. if len(commonDevs) > 0 && idx == 0 {
  62. ret = append(ret, commonDevs...)
  63. }
  64. }
  65. }
  66. return ret, nil
  67. }