gpu_device_plugin.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e_node
  14. import (
  15. "os/exec"
  16. "strconv"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. kubeletmetrics "k8s.io/kubernetes/pkg/kubelet/metrics"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. "k8s.io/kubernetes/test/e2e/framework/gpu"
  23. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  24. "k8s.io/kubernetes/test/e2e/framework/metrics"
  25. . "github.com/onsi/ginkgo"
  26. . "github.com/onsi/gomega"
  27. "github.com/prometheus/common/model"
  28. )
  29. // Serial because the test restarts Kubelet
  30. var _ = framework.KubeDescribe("NVIDIA GPU Device Plugin [Feature:GPUDevicePlugin][NodeFeature:GPUDevicePlugin][Serial] [Disruptive]", func() {
  31. f := framework.NewDefaultFramework("device-plugin-gpus-errors")
  32. Context("DevicePlugin", func() {
  33. var devicePluginPod *v1.Pod
  34. var err error
  35. BeforeEach(func() {
  36. By("Ensuring that Nvidia GPUs exists on the node")
  37. if !checkIfNvidiaGPUsExistOnNode() {
  38. Skip("Nvidia GPUs do not exist on the node. Skipping test.")
  39. }
  40. By("Creating the Google Device Plugin pod for NVIDIA GPU in GKE")
  41. devicePluginPod, err = f.ClientSet.CoreV1().Pods(metav1.NamespaceSystem).Create(gpu.NVIDIADevicePlugin())
  42. framework.ExpectNoError(err)
  43. By("Waiting for GPUs to become available on the local node")
  44. Eventually(func() bool {
  45. return gpu.NumberOfNVIDIAGPUs(getLocalNode(f)) > 0
  46. }, 5*time.Minute, framework.Poll).Should(BeTrue())
  47. if gpu.NumberOfNVIDIAGPUs(getLocalNode(f)) < 2 {
  48. Skip("Not enough GPUs to execute this test (at least two needed)")
  49. }
  50. })
  51. AfterEach(func() {
  52. l, err := f.PodClient().List(metav1.ListOptions{})
  53. framework.ExpectNoError(err)
  54. for _, p := range l.Items {
  55. if p.Namespace != f.Namespace.Name {
  56. continue
  57. }
  58. f.PodClient().Delete(p.Name, &metav1.DeleteOptions{})
  59. }
  60. })
  61. It("checks that when Kubelet restarts exclusive GPU assignation to pods is kept.", func() {
  62. By("Creating one GPU pod on a node with at least two GPUs")
  63. podRECMD := "devs=$(ls /dev/ | egrep '^nvidia[0-9]+$') && echo gpu devices: $devs"
  64. p1 := f.PodClient().CreateSync(makeBusyboxPod(gpu.NVIDIAGPUResourceName, podRECMD))
  65. deviceIDRE := "gpu devices: (nvidia[0-9]+)"
  66. devId1 := parseLog(f, p1.Name, p1.Name, deviceIDRE)
  67. p1, err := f.PodClient().Get(p1.Name, metav1.GetOptions{})
  68. framework.ExpectNoError(err)
  69. By("Restarting Kubelet and waiting for the current running pod to restart")
  70. restartKubelet()
  71. By("Confirming that after a kubelet and pod restart, GPU assignment is kept")
  72. ensurePodContainerRestart(f, p1.Name, p1.Name)
  73. devIdRestart1 := parseLog(f, p1.Name, p1.Name, deviceIDRE)
  74. Expect(devIdRestart1).To(Equal(devId1))
  75. By("Restarting Kubelet and creating another pod")
  76. restartKubelet()
  77. framework.WaitForAllNodesSchedulable(f.ClientSet, framework.TestContext.NodeSchedulableTimeout)
  78. Eventually(func() bool {
  79. return gpu.NumberOfNVIDIAGPUs(getLocalNode(f)) > 0
  80. }, 5*time.Minute, framework.Poll).Should(BeTrue())
  81. p2 := f.PodClient().CreateSync(makeBusyboxPod(gpu.NVIDIAGPUResourceName, podRECMD))
  82. By("Checking that pods got a different GPU")
  83. devId2 := parseLog(f, p2.Name, p2.Name, deviceIDRE)
  84. Expect(devId1).To(Not(Equal(devId2)))
  85. By("Deleting device plugin.")
  86. f.ClientSet.CoreV1().Pods(metav1.NamespaceSystem).Delete(devicePluginPod.Name, &metav1.DeleteOptions{})
  87. By("Waiting for GPUs to become unavailable on the local node")
  88. Eventually(func() bool {
  89. node, err := f.ClientSet.CoreV1().Nodes().Get(framework.TestContext.NodeName, metav1.GetOptions{})
  90. framework.ExpectNoError(err)
  91. return gpu.NumberOfNVIDIAGPUs(node) <= 0
  92. }, 10*time.Minute, framework.Poll).Should(BeTrue())
  93. By("Checking that scheduled pods can continue to run even after we delete device plugin.")
  94. ensurePodContainerRestart(f, p1.Name, p1.Name)
  95. devIdRestart1 = parseLog(f, p1.Name, p1.Name, deviceIDRE)
  96. Expect(devIdRestart1).To(Equal(devId1))
  97. ensurePodContainerRestart(f, p2.Name, p2.Name)
  98. devIdRestart2 := parseLog(f, p2.Name, p2.Name, deviceIDRE)
  99. Expect(devIdRestart2).To(Equal(devId2))
  100. By("Restarting Kubelet.")
  101. restartKubelet()
  102. By("Checking that scheduled pods can continue to run even after we delete device plugin and restart Kubelet.")
  103. ensurePodContainerRestart(f, p1.Name, p1.Name)
  104. devIdRestart1 = parseLog(f, p1.Name, p1.Name, deviceIDRE)
  105. Expect(devIdRestart1).To(Equal(devId1))
  106. ensurePodContainerRestart(f, p2.Name, p2.Name)
  107. devIdRestart2 = parseLog(f, p2.Name, p2.Name, deviceIDRE)
  108. Expect(devIdRestart2).To(Equal(devId2))
  109. logDevicePluginMetrics()
  110. // Cleanup
  111. f.PodClient().DeleteSync(p1.Name, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  112. f.PodClient().DeleteSync(p2.Name, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  113. })
  114. })
  115. })
  116. func checkIfNvidiaGPUsExistOnNode() bool {
  117. // Cannot use `lspci` because it is not installed on all distros by default.
  118. err := exec.Command("/bin/sh", "-c", "find /sys/devices/pci* -type f | grep vendor | xargs cat | grep 0x10de").Run()
  119. if err != nil {
  120. e2elog.Logf("check for nvidia GPUs failed. Got Error: %v", err)
  121. return false
  122. }
  123. return true
  124. }
  125. func logDevicePluginMetrics() {
  126. ms, err := metrics.GrabKubeletMetricsWithoutProxy(framework.TestContext.NodeName+":10255", "/metrics")
  127. framework.ExpectNoError(err)
  128. for msKey, samples := range ms {
  129. switch msKey {
  130. case kubeletmetrics.KubeletSubsystem + "_" + kubeletmetrics.DevicePluginAllocationDurationKey:
  131. for _, sample := range samples {
  132. latency := sample.Value
  133. resource := string(sample.Metric["resource_name"])
  134. var quantile float64
  135. if val, ok := sample.Metric[model.QuantileLabel]; ok {
  136. var err error
  137. if quantile, err = strconv.ParseFloat(string(val), 64); err != nil {
  138. continue
  139. }
  140. e2elog.Logf("Metric: %v ResourceName: %v Quantile: %v Latency: %v", msKey, resource, quantile, latency)
  141. }
  142. }
  143. case kubeletmetrics.KubeletSubsystem + "_" + kubeletmetrics.DevicePluginRegistrationCountKey:
  144. for _, sample := range samples {
  145. resource := string(sample.Metric["resource_name"])
  146. count := sample.Value
  147. e2elog.Logf("Metric: %v ResourceName: %v Count: %v", msKey, resource, count)
  148. }
  149. }
  150. }
  151. }