container_device.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 isolated_device
  15. import (
  16. "fmt"
  17. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  23. )
  24. type containerCDIFactory func() (IContainerCDIManager, error)
  25. var (
  26. containerDeviceManagers = make(map[ContainerDeviceType]IContainerDeviceManager)
  27. containerCDIManagers = make(map[apis.ContainerCDIKind]containerCDIFactory)
  28. )
  29. type ContainerDeviceType string
  30. const (
  31. ContainerDeviceTypeCphAMDGPU ContainerDeviceType = api.CONTAINER_DEV_CPH_AMD_GPU
  32. ContainerDeviceTypeCphASOPBinder ContainerDeviceType = api.CONTAINER_DEV_CPH_AOSP_BINDER
  33. ContainerNetintCAASIC ContainerDeviceType = api.CONTAINER_DEV_NETINT_CA_ASIC
  34. ContainerNetintCAQuadra ContainerDeviceType = api.CONTAINER_DEV_NETINT_CA_QUADRA
  35. ContainerDeviceTypeNvidiaGpu ContainerDeviceType = api.CONTAINER_DEV_NVIDIA_GPU
  36. ContainerDeviceTypeNvidiaMps ContainerDeviceType = api.CONTAINER_DEV_NVIDIA_MPS
  37. ContainerDeviceTypeNvidiaGpuShare ContainerDeviceType = api.CONTAINER_DEV_NVIDIA_GPU_SHARE
  38. ContainerDeviceTypeAscendNpu ContainerDeviceType = api.CONTAINER_DEV_ASCEND_NPU
  39. ContainerDeviceTypeVastaitechGpu ContainerDeviceType = api.CONTAINER_DEV_VASTAITECH_GPU
  40. )
  41. func GetContainerDeviceManager(t ContainerDeviceType) (IContainerDeviceManager, error) {
  42. man, ok := containerDeviceManagers[t]
  43. if !ok {
  44. return nil, errors.Wrapf(errors.ErrNotFound, "not found container device manager by %q", t)
  45. }
  46. return man, nil
  47. }
  48. func RegisterContainerDeviceManager(man IContainerDeviceManager) {
  49. if _, ok := containerDeviceManagers[man.GetType()]; ok {
  50. panic(fmt.Sprintf("container device manager %s is already registered", man.GetType()))
  51. }
  52. containerDeviceManagers[man.GetType()] = man
  53. }
  54. func GetContainerCDIManager(t apis.ContainerCDIKind) (IContainerCDIManager, error) {
  55. manF, ok := containerCDIManagers[t]
  56. if !ok {
  57. return nil, errors.Wrapf(errors.ErrNotFound, "not found container cdi manager by %q", t)
  58. }
  59. return manF()
  60. }
  61. func RegisterContainerCDIManaer(t apis.ContainerCDIKind, factoryFunc func() (IContainerCDIManager, error)) {
  62. if _, ok := containerCDIManagers[t]; ok {
  63. panic(fmt.Sprintf("CDI manager %s is already registered", t))
  64. }
  65. containerCDIManagers[t] = factoryFunc
  66. }
  67. type ContainerDevice struct {
  68. Path string `json:"path"`
  69. Type ContainerDeviceType `json:"type"`
  70. VirtualNumber int `json:"virtual_number"`
  71. }
  72. type ContainerDeviceConfiguration struct {
  73. Devices []*ContainerDevice `json:"devices"`
  74. }
  75. type IContainerDeviceManager interface {
  76. GetType() ContainerDeviceType
  77. NewDevices(dev *ContainerDevice) ([]IDevice, error)
  78. NewContainerDevices(input *hostapi.ContainerCreateInput, dev *hostapi.ContainerDevice) ([]*runtimeapi.Device, []*runtimeapi.Device, error)
  79. ProbeDevices() ([]IDevice, error)
  80. GetContainerExtraConfigures(devs []*hostapi.ContainerDevice) ([]*runtimeapi.KeyValue, []*runtimeapi.Mount)
  81. }
  82. type IContainerCDIManager interface {
  83. GetKind() apis.ContainerCDIKind
  84. GetSpecFilePath() string
  85. GetDeviceName(dev *hostapi.ContainerDevice) (string, error)
  86. }
  87. func GetContainerCDIDevices(devs []*hostapi.ContainerDevice) ([]*runtimeapi.CDIDevice, error) {
  88. errs := []error{}
  89. retDevs := make([]*runtimeapi.CDIDevice, 0)
  90. for i := range devs {
  91. dev := devs[i]
  92. cdiDev, err := getContainerCDIDevice(dev)
  93. if err != nil {
  94. errs = append(errs, errors.Wrapf(err, "get CDI by %s", jsonutils.Marshal(dev)))
  95. continue
  96. }
  97. retDevs = append(retDevs, cdiDev)
  98. }
  99. return retDevs, nil
  100. }
  101. func getContainerCDIDevice(dev *hostapi.ContainerDevice) (*runtimeapi.CDIDevice, error) {
  102. isoDev := dev.IsolatedDevice
  103. if isoDev == nil {
  104. return nil, errors.Wrap(errors.ErrNotEmpty, "isolated_device is nil")
  105. }
  106. cdi := isoDev.CDI
  107. if cdi == nil {
  108. return nil, errors.Wrap(errors.ErrNotEmpty, "CDI is nil")
  109. }
  110. man, err := GetContainerCDIManager(isoDev.CDI.Kind)
  111. if err != nil {
  112. return nil, errors.Wrapf(err, "GetContainerCDIManager by %s", cdi.Kind)
  113. }
  114. name, err := man.GetDeviceName(dev)
  115. if err != nil {
  116. return nil, errors.Wrapf(err, "get CDI device name")
  117. }
  118. return &runtimeapi.CDIDevice{
  119. Name: fmt.Sprintf("%s=%s", man.GetKind(), name),
  120. }, nil
  121. }