main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 main
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/util/pod"
  23. )
  24. func getLxcfsMounts() []*runtimeapi.Mount {
  25. lxcfsPath := "/var/lib/lxcfs"
  26. const (
  27. procCpuinfo = "/proc/cpuinfo"
  28. procDiskstats = "/proc/diskstats"
  29. procMeminfo = "/proc/meminfo"
  30. procStat = "/proc/stat"
  31. procSwaps = "/proc/swaps"
  32. procUptime = "/proc/uptime"
  33. )
  34. newLxcfsMount := func(fp string) *runtimeapi.Mount {
  35. return &runtimeapi.Mount{
  36. ContainerPath: fp,
  37. HostPath: fmt.Sprintf("%s%s", lxcfsPath, fp),
  38. }
  39. }
  40. return []*runtimeapi.Mount{
  41. newLxcfsMount(procUptime),
  42. newLxcfsMount(procMeminfo),
  43. newLxcfsMount(procStat),
  44. newLxcfsMount(procCpuinfo),
  45. newLxcfsMount(procSwaps),
  46. newLxcfsMount(procDiskstats),
  47. }
  48. }
  49. func main() {
  50. ctl, err := pod.NewCRI("unix:///var/run/onecloud/containerd/containerd.sock", 3*time.Second)
  51. if err != nil {
  52. log.Fatalf("NewCRI: %v", err)
  53. }
  54. ctx := context.Background()
  55. imgs, err := ctl.ListImages(ctx, nil)
  56. if err != nil {
  57. log.Fatalf("ListImages: %v", err)
  58. }
  59. for _, img := range imgs {
  60. log.Infof("get img: %s", img.String())
  61. }
  62. ver, err := ctl.Version(context.Background())
  63. if err != nil {
  64. log.Fatalf("get version: %v", err)
  65. }
  66. log.Infof("get version: %s", ver.String())
  67. // create container
  68. podCfg := &runtimeapi.PodSandboxConfig{
  69. Metadata: &runtimeapi.PodSandboxMetadata{
  70. Name: "test-gpu",
  71. Uid: "6659d5d0-9187-4b4f-8143-dbe0453229af",
  72. Namespace: "27c9464ab54947328a29298761895be3",
  73. Attempt: 1,
  74. },
  75. Hostname: "test-gpu",
  76. LogDirectory: "",
  77. DnsConfig: nil,
  78. PortMappings: nil,
  79. Labels: nil,
  80. Annotations: nil,
  81. Linux: nil,
  82. Windows: nil,
  83. }
  84. var defaultCPUPeriod int64 = 100000
  85. ctrCfgs := []*runtimeapi.ContainerConfig{
  86. {
  87. Metadata: &runtimeapi.ContainerMetadata{
  88. Name: "nvidia-smi",
  89. },
  90. Image: &runtimeapi.ImageSpec{
  91. Image: "ubuntu:18.04",
  92. },
  93. Command: []string{"sleep", "100d"},
  94. Linux: &runtimeapi.LinuxContainerConfig{
  95. //SecurityContext: &runtimeapi.LinuxContainerSecurityContext{
  96. // Privileged: true,
  97. //},
  98. Resources: &runtimeapi.LinuxContainerResources{
  99. CpuPeriod: defaultCPUPeriod,
  100. CpuQuota: 2 * defaultCPUPeriod,
  101. CpuShares: 0,
  102. MemoryLimitInBytes: 512 * 1024 * 1024,
  103. OomScoreAdj: 0,
  104. CpusetCpus: "",
  105. CpusetMems: "",
  106. HugepageLimits: nil,
  107. Unified: nil,
  108. MemorySwapLimitInBytes: 0,
  109. },
  110. },
  111. Envs: []*runtimeapi.KeyValue{
  112. {
  113. Key: "NVIDIA_VISIBLE_DEVICES",
  114. Value: "GPU-e588f4f5-29a4-4374-a335-86e120b50e14,GPU-f7160578-ba3b-3e42-6991-14c815ce032a,GPU-679b381b-eb98-62b7-c7c4-175f7d751aad",
  115. },
  116. {
  117. Key: "NVIDIA_DRIVER_CAPABILITIES",
  118. Value: "compute,utility",
  119. },
  120. },
  121. // Mounts: getLxcfsMounts(),
  122. /*Devices: []*runtimeapi.Device{
  123. {
  124. HostPath: "/dev/nvidia0",
  125. ContainerPath: "/dev/nvidia0",
  126. Permissions: "rwm",
  127. },
  128. },*/
  129. },
  130. }
  131. resp, err := ctl.RunContainers(ctx, podCfg, ctrCfgs, "")
  132. if err != nil {
  133. log.Fatalf("RunContainers: %v", err)
  134. }
  135. log.Infof("RunContainers: %s", jsonutils.Marshal(resp))
  136. }