k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/windows/device_plugin.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package windows 18 19 import ( 20 "context" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 clientset "k8s.io/client-go/kubernetes" 27 "k8s.io/kubernetes/test/e2e/feature" 28 "k8s.io/kubernetes/test/e2e/framework" 29 e2edaemonset "k8s.io/kubernetes/test/e2e/framework/daemonset" 30 e2epod "k8s.io/kubernetes/test/e2e/framework/pod" 31 e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output" 32 e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" 33 admissionapi "k8s.io/pod-security-admission/api" 34 35 "github.com/onsi/ginkgo/v2" 36 ) 37 38 const ( 39 testSlowMultiplier = 60 40 ) 41 42 var _ = sigDescribe(feature.GPUDevicePlugin, "Device Plugin", skipUnlessWindows(func() { 43 f := framework.NewDefaultFramework("device-plugin") 44 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 45 46 var cs clientset.Interface 47 48 ginkgo.BeforeEach(func() { 49 //Only for Windows containers 50 e2eskipper.SkipUnlessNodeOSDistroIs("windows") 51 cs = f.ClientSet 52 }) 53 ginkgo.It("should be able to create a functioning device plugin for Windows", func(ctx context.Context) { 54 ginkgo.By("creating Windows device plugin daemonset") 55 dsName := "directx-device-plugin" 56 daemonsetNameLabel := "daemonset-name" 57 image := "e2eteam/k8s-directx-device-plugin:0.9.0-1809" 58 mountName := "device-plugin" 59 mountPath := "/var/lib/kubelet/device-plugins" 60 labels := map[string]string{ 61 daemonsetNameLabel: dsName, 62 } 63 volumes := []v1.Volume{ 64 { 65 Name: mountName, 66 VolumeSource: v1.VolumeSource{ 67 HostPath: &v1.HostPathVolumeSource{ 68 Path: mountPath, 69 }, 70 }, 71 }, 72 } 73 mounts := []v1.VolumeMount{ 74 { 75 Name: mountName, 76 MountPath: mountPath, 77 }, 78 } 79 ds := e2edaemonset.NewDaemonSet(dsName, image, labels, volumes, mounts, nil) 80 ds.Spec.Template.Spec.PriorityClassName = "system-node-critical" 81 ds.Spec.Template.Spec.Tolerations = []v1.Toleration{ 82 { 83 Key: "CriticalAddonsOnly", 84 Operator: "Exists", 85 }, 86 } 87 ds.Spec.Template.Spec.NodeSelector = map[string]string{ 88 "kubernetes.io/os": "windows", 89 } 90 ds.Spec.Template.Spec.Containers[0].Env = []v1.EnvVar{ 91 { 92 Name: "DIRECTX_GPU_MATCH_NAME", 93 Value: " ", 94 }, 95 } 96 97 sysNs := "kube-system" 98 _, err := cs.AppsV1().DaemonSets(sysNs).Create(ctx, ds, metav1.CreateOptions{}) 99 framework.ExpectNoError(err) 100 101 // Windows device plugin tests require the *full* windows image (not nanoserver or servercore) 102 // because those images do not contain the necessary DirectX components. 103 fullWindowsContainerImage := "mcr.microsoft.com/windows:ltsc2019" 104 105 ginkgo.By("creating Windows testing Pod") 106 windowsPod := createTestPod(f, fullWindowsContainerImage, windowsOS) 107 windowsPod.Spec.Containers[0].Args = []string{"powershell.exe", "Start-Sleep", "3600"} 108 windowsPod.Spec.Containers[0].Resources.Limits = v1.ResourceList{ 109 "microsoft.com/directx": resource.MustParse("1"), 110 } 111 windowsPod, err = cs.CoreV1().Pods(f.Namespace.Name).Create(ctx, windowsPod, metav1.CreateOptions{}) 112 framework.ExpectNoError(err) 113 ginkgo.By("Waiting for the pod Running") 114 err = e2epod.WaitTimeoutForPodRunningInNamespace(ctx, cs, windowsPod.Name, f.Namespace.Name, testSlowMultiplier*framework.PodStartTimeout) 115 framework.ExpectNoError(err) 116 117 ginkgo.By("verifying device access in Windows testing Pod") 118 dxdiagCommand := []string{"cmd.exe", "/c", "dxdiag", "/t", "dxdiag_output.txt", "&", "type", "dxdiag_output.txt"} 119 //If DirectX version issues caused by supsequent windows releases occur, these tests need to do version checks 120 //based on the windows version running the test. 121 dxdiagDirectxVersion := "DirectX Version: DirectX 12" 122 defaultNs := f.Namespace.Name 123 _, dxdiagDirectxVersionErr := e2eoutput.LookForStringInPodExec(defaultNs, windowsPod.Name, dxdiagCommand, dxdiagDirectxVersion, time.Minute) 124 framework.ExpectNoError(dxdiagDirectxVersionErr, "failed: didn't find directX version dxdiag output.") 125 126 dxdiagDdiVersion := "DDI Version: 12" 127 _, dxdiagDdiVersionErr := e2eoutput.LookForStringInPodExec(defaultNs, windowsPod.Name, dxdiagCommand, dxdiagDdiVersion, time.Minute) 128 framework.ExpectNoError(dxdiagDdiVersionErr, "failed: didn't find DDI version in dxdiag output.") 129 130 dxdiagVendorID := "Vendor ID: 0x" 131 _, dxdiagVendorIDErr := e2eoutput.LookForStringInPodExec(defaultNs, windowsPod.Name, dxdiagCommand, dxdiagVendorID, time.Minute) 132 framework.ExpectNoError(dxdiagVendorIDErr, "failed: didn't find vendorID in dxdiag output.") 133 134 envVarCommand := []string{"cmd.exe", "/c", "set", "DIRECTX_GPU_Name"} 135 envVarDirectxGpuName := "DIRECTX_GPU_Name=" 136 _, envVarDirectxGpuNameErr := e2eoutput.LookForStringInPodExec(defaultNs, windowsPod.Name, envVarCommand, envVarDirectxGpuName, time.Minute) 137 framework.ExpectNoError(envVarDirectxGpuNameErr, "failed: didn't find expected environment variable.") 138 }) 139 }))