gpu_device_plugin_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 e2enode
  14. import (
  15. "context"
  16. "os/exec"
  17. "strconv"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/uuid"
  22. kubeletmetrics "k8s.io/kubernetes/pkg/kubelet/metrics"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. "k8s.io/kubernetes/test/e2e/framework/gpu"
  25. "k8s.io/kubernetes/test/e2e/framework/metrics"
  26. "github.com/onsi/ginkgo"
  27. "github.com/onsi/gomega"
  28. "github.com/prometheus/common/model"
  29. )
  30. // numberOfNVIDIAGPUs returns the number of GPUs advertised by a node
  31. // This is based on the Device Plugin system and expected to run on a COS based node
  32. // After the NVIDIA drivers were installed
  33. // TODO make this generic and not linked to COS only
  34. func numberOfNVIDIAGPUs(node *v1.Node) int64 {
  35. val, ok := node.Status.Capacity[gpu.NVIDIAGPUResourceName]
  36. if !ok {
  37. return 0
  38. }
  39. return val.Value()
  40. }
  41. // NVIDIADevicePlugin returns the official Google Device Plugin pod for NVIDIA GPU in GKE
  42. func NVIDIADevicePlugin() *v1.Pod {
  43. ds, err := framework.DsFromManifest(gpu.GPUDevicePluginDSYAML)
  44. framework.ExpectNoError(err)
  45. p := &v1.Pod{
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: "device-plugin-nvidia-gpu-" + string(uuid.NewUUID()),
  48. Namespace: metav1.NamespaceSystem,
  49. },
  50. Spec: ds.Spec.Template.Spec,
  51. }
  52. // Remove node affinity
  53. p.Spec.Affinity = nil
  54. return p
  55. }
  56. // Serial because the test restarts Kubelet
  57. var _ = framework.KubeDescribe("NVIDIA GPU Device Plugin [Feature:GPUDevicePlugin][NodeFeature:GPUDevicePlugin][Serial] [Disruptive]", func() {
  58. f := framework.NewDefaultFramework("device-plugin-gpus-errors")
  59. ginkgo.Context("DevicePlugin", func() {
  60. var devicePluginPod *v1.Pod
  61. var err error
  62. ginkgo.BeforeEach(func() {
  63. ginkgo.By("Ensuring that Nvidia GPUs exists on the node")
  64. if !checkIfNvidiaGPUsExistOnNode() {
  65. ginkgo.Skip("Nvidia GPUs do not exist on the node. Skipping test.")
  66. }
  67. ginkgo.By("Creating the Google Device Plugin pod for NVIDIA GPU in GKE")
  68. devicePluginPod, err = f.ClientSet.CoreV1().Pods(metav1.NamespaceSystem).Create(context.TODO(), NVIDIADevicePlugin(), metav1.CreateOptions{})
  69. framework.ExpectNoError(err)
  70. ginkgo.By("Waiting for GPUs to become available on the local node")
  71. gomega.Eventually(func() bool {
  72. return numberOfNVIDIAGPUs(getLocalNode(f)) > 0
  73. }, 5*time.Minute, framework.Poll).Should(gomega.BeTrue())
  74. if numberOfNVIDIAGPUs(getLocalNode(f)) < 2 {
  75. ginkgo.Skip("Not enough GPUs to execute this test (at least two needed)")
  76. }
  77. })
  78. ginkgo.AfterEach(func() {
  79. l, err := f.PodClient().List(context.TODO(), metav1.ListOptions{})
  80. framework.ExpectNoError(err)
  81. for _, p := range l.Items {
  82. if p.Namespace != f.Namespace.Name {
  83. continue
  84. }
  85. f.PodClient().Delete(context.TODO(), p.Name, &metav1.DeleteOptions{})
  86. }
  87. })
  88. ginkgo.It("checks that when Kubelet restarts exclusive GPU assignation to pods is kept.", func() {
  89. ginkgo.By("Creating one GPU pod on a node with at least two GPUs")
  90. podRECMD := "devs=$(ls /dev/ | egrep '^nvidia[0-9]+$') && echo gpu devices: $devs"
  91. p1 := f.PodClient().CreateSync(makeBusyboxPod(gpu.NVIDIAGPUResourceName, podRECMD))
  92. deviceIDRE := "gpu devices: (nvidia[0-9]+)"
  93. devID1 := parseLog(f, p1.Name, p1.Name, deviceIDRE)
  94. p1, err := f.PodClient().Get(context.TODO(), p1.Name, metav1.GetOptions{})
  95. framework.ExpectNoError(err)
  96. ginkgo.By("Restarting Kubelet and waiting for the current running pod to restart")
  97. restartKubelet()
  98. ginkgo.By("Confirming that after a kubelet and pod restart, GPU assignment is kept")
  99. ensurePodContainerRestart(f, p1.Name, p1.Name)
  100. devIDRestart1 := parseLog(f, p1.Name, p1.Name, deviceIDRE)
  101. framework.ExpectEqual(devIDRestart1, devID1)
  102. ginkgo.By("Restarting Kubelet and creating another pod")
  103. restartKubelet()
  104. framework.WaitForAllNodesSchedulable(f.ClientSet, framework.TestContext.NodeSchedulableTimeout)
  105. gomega.Eventually(func() bool {
  106. return numberOfNVIDIAGPUs(getLocalNode(f)) > 0
  107. }, 5*time.Minute, framework.Poll).Should(gomega.BeTrue())
  108. p2 := f.PodClient().CreateSync(makeBusyboxPod(gpu.NVIDIAGPUResourceName, podRECMD))
  109. ginkgo.By("Checking that pods got a different GPU")
  110. devID2 := parseLog(f, p2.Name, p2.Name, deviceIDRE)
  111. framework.ExpectEqual(devID1, devID2)
  112. ginkgo.By("Deleting device plugin.")
  113. f.ClientSet.CoreV1().Pods(metav1.NamespaceSystem).Delete(context.TODO(), devicePluginPod.Name, &metav1.DeleteOptions{})
  114. ginkgo.By("Waiting for GPUs to become unavailable on the local node")
  115. gomega.Eventually(func() bool {
  116. node, err := f.ClientSet.CoreV1().Nodes().Get(context.TODO(), framework.TestContext.NodeName, metav1.GetOptions{})
  117. framework.ExpectNoError(err)
  118. return numberOfNVIDIAGPUs(node) <= 0
  119. }, 10*time.Minute, framework.Poll).Should(gomega.BeTrue())
  120. ginkgo.By("Checking that scheduled pods can continue to run even after we delete device plugin.")
  121. ensurePodContainerRestart(f, p1.Name, p1.Name)
  122. devIDRestart1 = parseLog(f, p1.Name, p1.Name, deviceIDRE)
  123. framework.ExpectEqual(devIDRestart1, devID1)
  124. ensurePodContainerRestart(f, p2.Name, p2.Name)
  125. devIDRestart2 := parseLog(f, p2.Name, p2.Name, deviceIDRE)
  126. framework.ExpectEqual(devIDRestart2, devID2)
  127. ginkgo.By("Restarting Kubelet.")
  128. restartKubelet()
  129. ginkgo.By("Checking that scheduled pods can continue to run even after we delete device plugin and restart Kubelet.")
  130. ensurePodContainerRestart(f, p1.Name, p1.Name)
  131. devIDRestart1 = parseLog(f, p1.Name, p1.Name, deviceIDRE)
  132. framework.ExpectEqual(devIDRestart1, devID1)
  133. ensurePodContainerRestart(f, p2.Name, p2.Name)
  134. devIDRestart2 = parseLog(f, p2.Name, p2.Name, deviceIDRE)
  135. framework.ExpectEqual(devIDRestart2, devID2)
  136. logDevicePluginMetrics()
  137. // Cleanup
  138. f.PodClient().DeleteSync(p1.Name, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  139. f.PodClient().DeleteSync(p2.Name, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  140. })
  141. })
  142. })
  143. func checkIfNvidiaGPUsExistOnNode() bool {
  144. // Cannot use `lspci` because it is not installed on all distros by default.
  145. err := exec.Command("/bin/sh", "-c", "find /sys/devices/pci* -type f | grep vendor | xargs cat | grep 0x10de").Run()
  146. if err != nil {
  147. framework.Logf("check for nvidia GPUs failed. Got Error: %v", err)
  148. return false
  149. }
  150. return true
  151. }
  152. func logDevicePluginMetrics() {
  153. ms, err := metrics.GrabKubeletMetricsWithoutProxy(framework.TestContext.NodeName+":10255", "/metrics")
  154. framework.ExpectNoError(err)
  155. for msKey, samples := range ms {
  156. switch msKey {
  157. case kubeletmetrics.KubeletSubsystem + "_" + kubeletmetrics.DevicePluginAllocationDurationKey:
  158. for _, sample := range samples {
  159. latency := sample.Value
  160. resource := string(sample.Metric["resource_name"])
  161. var quantile float64
  162. if val, ok := sample.Metric[model.QuantileLabel]; ok {
  163. var err error
  164. if quantile, err = strconv.ParseFloat(string(val), 64); err != nil {
  165. continue
  166. }
  167. framework.Logf("Metric: %v ResourceName: %v Quantile: %v Latency: %v", msKey, resource, quantile, latency)
  168. }
  169. }
  170. case kubeletmetrics.KubeletSubsystem + "_" + kubeletmetrics.DevicePluginRegistrationCountKey:
  171. for _, sample := range samples {
  172. resource := string(sample.Metric["resource_name"])
  173. count := sample.Value
  174. framework.Logf("Metric: %v ResourceName: %v Count: %v", msKey, resource, count)
  175. }
  176. }
  177. }
  178. }