container.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 containerd
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "github.com/containerd/containerd"
  20. "github.com/containerd/containerd/containers"
  21. "github.com/containerd/typeurl"
  22. runtimespec "github.com/opencontainers/runtime-spec/specs-go"
  23. "yunion.io/x/jsonutils"
  24. "yunion.io/x/log"
  25. "yunion.io/x/pkg/errors"
  26. )
  27. // UpdateContainerDevices 通过 containerd API 更新 container spec 中的 devices
  28. // 用于 cgroup v2 场景
  29. func UpdateContainerDevices(ctx context.Context, client *containerd.Client, containerID string, devices []*runtimespec.LinuxDeviceCgroup) error {
  30. if len(devices) == 0 {
  31. return nil
  32. }
  33. log.Infof("[UpdateContainerDevices] Updating container %s devices via containerd API: %v", containerID, devices)
  34. // 获取 container
  35. container, err := client.LoadContainer(ctx, containerID)
  36. if err != nil {
  37. return errors.Wrapf(err, "load container %s", containerID)
  38. }
  39. cStatus := ContainerStatus(ctx, container)
  40. if cStatus == ProcessStatusRestarting {
  41. return errors.Errorf("container %q is in restarting state", containerID)
  42. }
  43. // 注意:我们允许在 created、stopped 等状态下更新 spec
  44. spec, err := container.Spec(ctx)
  45. if err != nil {
  46. return errors.Wrapf(err, "get container %s spec", containerID)
  47. }
  48. oldSpec, err := copySpec(spec)
  49. if err != nil {
  50. return errors.Wrapf(err, "copy container %s spec", containerID)
  51. }
  52. log.Infof("======oldSpec: %s, try update devices: %s", jsonutils.Marshal(oldSpec).PrettyString(), jsonutils.Marshal(devices).PrettyString())
  53. // 确保 Linux.Resources 存在
  54. if spec.Linux == nil {
  55. spec.Linux = &runtimespec.Linux{}
  56. }
  57. if spec.Linux.Resources == nil {
  58. spec.Linux.Resources = &runtimespec.LinuxResources{}
  59. }
  60. if spec.Linux.Resources.BlockIO == nil {
  61. spec.Linux.Resources.BlockIO = &runtimespec.LinuxBlockIO{}
  62. }
  63. if spec.Linux.Resources.CPU == nil {
  64. spec.Linux.Resources.CPU = &runtimespec.LinuxCPU{}
  65. }
  66. if spec.Linux.Resources.Memory == nil {
  67. spec.Linux.Resources.Memory = &runtimespec.LinuxMemory{}
  68. }
  69. if spec.Linux.Resources.Pids == nil {
  70. spec.Linux.Resources.Pids = &runtimespec.LinuxPids{}
  71. }
  72. if spec.Linux.Resources.HugepageLimits == nil {
  73. spec.Linux.Resources.HugepageLimits = []runtimespec.LinuxHugepageLimit{}
  74. }
  75. if spec.Linux.Resources.Network == nil {
  76. spec.Linux.Resources.Network = &runtimespec.LinuxNetwork{}
  77. }
  78. if spec.Linux.Resources.Rdma == nil {
  79. spec.Linux.Resources.Rdma = map[string]runtimespec.LinuxRdma{}
  80. }
  81. if spec.Linux.Resources.Unified == nil {
  82. spec.Linux.Resources.Unified = map[string]string{}
  83. }
  84. if spec.Linux.Resources.Devices == nil {
  85. spec.Linux.Resources.Devices = []runtimespec.LinuxDeviceCgroup{}
  86. }
  87. // 更新 devices
  88. // spec.Linux.Resources.Devices = devices
  89. for i := range devices {
  90. dev := devices[i]
  91. spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, *dev)
  92. }
  93. if err := updateContainerSpec(ctx, container, spec); err != nil {
  94. return errors.Wrapf(err, "update container %s spec", containerID)
  95. }
  96. log.Infof("[UpdateContainerDevices] Successfully updated container %s devices via containerd API", containerID)
  97. return nil
  98. }
  99. func updateContainerSpec(ctx context.Context, container containerd.Container, spec *runtimespec.Spec) error {
  100. if err := container.Update(ctx, func(ctx context.Context, client *containerd.Client, c *containers.Container) error {
  101. a, err := typeurl.MarshalAny(spec)
  102. if err != nil {
  103. return errors.Wrapf(err, "marshal container %s spec", container.ID())
  104. }
  105. c.Spec = a
  106. return nil
  107. }); err != nil {
  108. return errors.Wrapf(err, "update container %s spec", container.ID())
  109. }
  110. return nil
  111. }
  112. // DeepCopy makes a deep copy from src into dst.
  113. func DeepCopy(dst interface{}, src interface{}) error {
  114. if dst == nil {
  115. return errors.Errorf("dst cannot be nil")
  116. }
  117. if src == nil {
  118. return errors.Errorf("src cannot be nil")
  119. }
  120. bytes, err := json.Marshal(src)
  121. if err != nil {
  122. return fmt.Errorf("unable to marshal src: %w", err)
  123. }
  124. err = json.Unmarshal(bytes, dst)
  125. if err != nil {
  126. return fmt.Errorf("unable to unmarshal into dst: %w", err)
  127. }
  128. return nil
  129. }
  130. func copySpec(spec *runtimespec.Spec) (*runtimespec.Spec, error) {
  131. var copySpec runtimespec.Spec
  132. if err := DeepCopy(&copySpec, spec); err != nil {
  133. return nil, fmt.Errorf("failed to deep copy:%w", err)
  134. }
  135. return &copySpec, nil
  136. }