/
hyperv.go
146 lines (124 loc) · 5 KB
/
hyperv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package windows
import (
"context"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/test/e2e/framework"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
imageutils "k8s.io/kubernetes/test/utils/image"
admissionapi "k8s.io/pod-security-admission/api"
)
var (
WindowsHyperVContainerRuntimeClass = "runhcs-wcow-hypervisor"
)
var _ = sigDescribe("[Feature:WindowsHyperVContainers] HyperV containers", skipUnlessWindows(func() {
ginkgo.BeforeEach(func() {
e2eskipper.SkipUnlessNodeOSDistroIs("windows")
})
f := framework.NewDefaultFramework("windows-hyperv-test")
f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
ginkgo.It("should start a hyperv isolated container", func(ctx context.Context) {
// HyperV isolated containers are only supported on containerd 1.7+
skipUnlessContainerdOneSevenOrGreater(ctx, f)
// check if hyperv runtime class is on node and skip otherwise
// Note: the runtime class is expected to be added to a cluster before running this test.
// see https://github.com/kubernetes-sigs/windows-testing/tree/master/helpers/hyper-v-mutating-webhook/hyperv-runtimeclass.yaml
// for an example.
_, err := f.ClientSet.NodeV1().RuntimeClasses().Get(ctx, WindowsHyperVContainerRuntimeClass, metav1.GetOptions{})
if err != nil {
framework.Logf("error getting runtime class: %v", err)
e2eskipper.Skipf("skipping test because runhcs-wcow-hypervisor runtime class is not present")
}
ginkgo.By("selecting a Windows node")
targetNode, err := findWindowsNode(ctx, f)
framework.ExpectNoError(err, "error finding Windows node")
framework.Logf("Using node: %v", targetNode.Name)
ginkgo.By("schedule a pod to that node")
image := imageutils.GetE2EImage(imageutils.BusyBox)
hypervPodName := "hyperv-test-pod"
hypervPod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: hypervPodName,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Image: image,
Name: "busybox-1",
Command: []string{"powershell.exe", "-Command", "Write-Host 'Hello'; sleep -Seconds 600"},
},
{
Image: image,
Name: "busybox-2",
Command: []string{"powershell.exe", "-Command", "Write-Host 'Hello'; sleep -Seconds 600"},
},
},
RestartPolicy: v1.RestartPolicyNever,
RuntimeClassName: &WindowsHyperVContainerRuntimeClass,
NodeName: targetNode.Name,
},
}
pc := e2epod.NewPodClient(f)
pc.Create(ctx, hypervPod)
ginkgo.By("waiting for the pod to be running")
timeout := 3 * time.Minute
e2epod.WaitForPodsRunningReady(ctx, f.ClientSet, f.Namespace.Name, 1, 0, timeout)
ginkgo.By("creating a host process container in another pod to verify the pod is running hyperv isolated containers")
// Note: each pod runs in a separate UVM so even though we are scheduling 2 containers in the test pod
// we should only expect a single UVM to be running on the host.
podName := "validation-pod"
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
},
Spec: v1.PodSpec{
SecurityContext: &v1.PodSecurityContext{
WindowsOptions: &v1.WindowsSecurityContextOptions{
HostProcess: &trueVar,
RunAsUserName: &User_NTAuthoritySystem,
},
},
HostNetwork: true,
Containers: []v1.Container{
{
Image: image,
Name: "container",
Command: []string{"powershell.exe", "-Command", "$vms = Get-ComputeProcess | Where-Object { ($_.Type -EQ 'VirtualMachine') -and ($_.Owner -EQ 'containerd-shim-runhcs-v1.exe') } ; if ($vms.Length -le 0) { throw 'error' }"},
},
},
RestartPolicy: v1.RestartPolicyNever,
NodeName: targetNode.Name,
},
}
pc.Create(ctx, pod)
ginkgo.By("waiting for the pod to be run")
pc.WaitForFinish(ctx, podName, timeout)
ginkgo.By("then ensuring pod finished running successfully")
p, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(ctx, podName, metav1.GetOptions{})
framework.ExpectNoError(err, "error getting pod")
if p.Status.Phase != v1.PodSucceeded {
logs, err := e2epod.GetPodLogs(ctx, f.ClientSet, f.Namespace.Name, podName, "container")
if err != nil {
framework.Logf("Error pulling logs: %v", err)
}
framework.Logf("Pod phase: %v\nlogs:\n%s", p.Status.Phase, logs)
}
gomega.Expect(p.Status.Phase).To(gomega.Equal(v1.PodSucceeded), "pod should have succeeded")
})
}))