container.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 hostinfo
  15. import (
  16. "context"
  17. "path"
  18. "time"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/utils"
  22. apis "yunion.io/x/onecloud/pkg/apis/compute"
  23. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  24. "yunion.io/x/onecloud/pkg/hostman/isolated_device"
  25. "yunion.io/x/onecloud/pkg/hostman/options"
  26. "yunion.io/x/onecloud/pkg/util/pod"
  27. "yunion.io/x/onecloud/pkg/util/pod/cadvisor"
  28. "yunion.io/x/onecloud/pkg/util/pod/stats"
  29. )
  30. func (h *SHostInfo) initCRI() error {
  31. cri, err := pod.NewCRI(h.GetContainerRuntimeEndpoint(), 3*time.Second)
  32. if err != nil {
  33. return errors.Wrapf(err, "New CRI by endpoint %q", h.GetContainerRuntimeEndpoint())
  34. }
  35. ver, err := cri.Version(context.Background())
  36. if err != nil {
  37. return errors.Wrap(err, "get runtime version")
  38. }
  39. log.Infof("Init container runtime: %s", ver)
  40. h.cri = cri
  41. return nil
  42. }
  43. func (h *SHostInfo) initContainerCPUMap(topo *hostapi.HostTopology) error {
  44. statefile := path.Join(options.HostOptions.ServersPath, "container_cpu_map")
  45. cm, err := pod.NewHostContainerCPUMap(topo, statefile)
  46. if err != nil {
  47. return errors.Wrap(err, "NewHostContainerCPUMap")
  48. }
  49. h.containerCPUMap = cm
  50. return nil
  51. }
  52. func (h *SHostInfo) startContainerStatsProvider(cri pod.CRI) error {
  53. ca, err := cadvisor.New(nil, "/opt/cloud/workspace", []string{"cloudpods"})
  54. if err != nil {
  55. return errors.Wrap(err, "new cadvisor")
  56. }
  57. if err := ca.Start(); err != nil {
  58. return errors.Wrap(err, "start cadvisor")
  59. }
  60. h.containerStatsProvider = stats.NewCRIContainerStatsProvider(ca, cri.GetRuntimeClient(), cri.GetImageClient())
  61. return nil
  62. }
  63. func (h *SHostInfo) GetCRI() pod.CRI {
  64. return h.cri
  65. }
  66. func (h *SHostInfo) GetContainerCPUMap() *pod.HostContainerCPUMap {
  67. return h.containerCPUMap
  68. }
  69. func (h *SHostInfo) GetContainerStatsProvider() stats.ContainerStatsProvider {
  70. return h.containerStatsProvider
  71. }
  72. type INvidiaGpuIndexMemoryInterface interface {
  73. GetNvidiaDevMemSize() int
  74. GetNvidiaDevIndex() string
  75. }
  76. func (h *SHostInfo) GetNvidiaGpuIndexMemoryMap() map[string]int {
  77. res := map[string]int{}
  78. for i := range h.containerNvidiaGpus {
  79. iDev, ok := h.containerNvidiaGpus[i].(INvidiaGpuIndexMemoryInterface)
  80. if !ok {
  81. continue
  82. }
  83. index := iDev.GetNvidiaDevIndex()
  84. memSize := iDev.GetNvidiaDevMemSize()
  85. res[index] = memSize
  86. }
  87. return res
  88. }
  89. func (h *SHostInfo) HasContainerVastaitechGpu() bool {
  90. if h.hasVastaitechGpus != nil {
  91. return *h.hasVastaitechGpus
  92. }
  93. hasVastaitechGpus := false
  94. devs := h.IsolatedDeviceMan.GetDevices()
  95. for i := range devs {
  96. if devs[i].GetDeviceType() == apis.CONTAINER_DEV_VASTAITECH_GPU {
  97. hasVastaitechGpus = true
  98. }
  99. }
  100. h.hasVastaitechGpus = &hasVastaitechGpus
  101. return *h.hasVastaitechGpus
  102. }
  103. func (h *SHostInfo) HasContainerCphAmdGpu() bool {
  104. if h.hasCphAmdGpus != nil {
  105. return *h.hasCphAmdGpus
  106. }
  107. hasCphAmdGpus := false
  108. devs := h.IsolatedDeviceMan.GetDevices()
  109. for i := range devs {
  110. if devs[i].GetDeviceType() == apis.CONTAINER_DEV_CPH_AMD_GPU {
  111. hasCphAmdGpus = true
  112. }
  113. }
  114. h.hasCphAmdGpus = &hasCphAmdGpus
  115. return *h.hasCphAmdGpus
  116. }
  117. func (h *SHostInfo) HasContainerNvidiaGpu() bool {
  118. if h.hasNvidiaGpus != nil {
  119. return *h.hasNvidiaGpus
  120. }
  121. hasNvidiaGpus := false
  122. nvDevs := make([]isolated_device.IDevice, 0)
  123. devs := h.IsolatedDeviceMan.GetDevices()
  124. for i := range devs {
  125. if utils.IsInStringArray(devs[i].GetDeviceType(), apis.NVIDIA_GPU_TYPES) {
  126. hasNvidiaGpus = true
  127. nvDevs = append(nvDevs, devs[i])
  128. }
  129. }
  130. h.hasNvidiaGpus = &hasNvidiaGpus
  131. h.containerNvidiaGpus = nvDevs
  132. return *h.hasNvidiaGpus
  133. }