|
| 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 | + |
| 22 | + v1 "k8s.io/api/core/v1" |
| 23 | + "k8s.io/apimachinery/pkg/api/resource" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 26 | + "k8s.io/kubernetes/test/e2e/framework" |
| 27 | + e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet" |
| 28 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/onsi/ginkgo" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = SIGDescribe("[Feature:Windows] Cpu Resources", func() { |
| 35 | + f := framework.NewDefaultFramework("cpu-resources-test-windows") |
| 36 | + |
| 37 | + // The Windows 'BusyBox' image is PowerShell plus a collection of scripts and utilities to mimic common busybox commands |
| 38 | + powershellImage := imageutils.GetConfig(imageutils.BusyBox) |
| 39 | + |
| 40 | + ginkgo.Context("Container limits", func() { |
| 41 | + ginkgo.It("should not be exceeded after waiting 2 minutes", func() { |
| 42 | + ginkgo.By("Creating one pod with limit set to '0.5'") |
| 43 | + podsDecimal := newCPUBurnPods(1, powershellImage, "0.5", "1Gi") |
| 44 | + f.PodClient().CreateBatch(podsDecimal) |
| 45 | + ginkgo.By("Creating one pod with limit set to '500m'") |
| 46 | + podsMilli := newCPUBurnPods(1, powershellImage, "500m", "1Gi") |
| 47 | + f.PodClient().CreateBatch(podsMilli) |
| 48 | + ginkgo.By("Waiting 2 minutes") |
| 49 | + time.Sleep(2 * time.Minute) |
| 50 | + ginkgo.By("Ensuring pods are still running") |
| 51 | + var allPods [](*v1.Pod) |
| 52 | + for _, p := range podsDecimal { |
| 53 | + pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get( |
| 54 | + context.TODO(), |
| 55 | + p.Name, |
| 56 | + metav1.GetOptions{}) |
| 57 | + framework.ExpectNoError(err, "Error retrieving pod") |
| 58 | + framework.ExpectEqual(pod.Status.Phase, v1.PodRunning) |
| 59 | + allPods = append(allPods, pod) |
| 60 | + } |
| 61 | + for _, p := range podsMilli { |
| 62 | + pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get( |
| 63 | + context.TODO(), |
| 64 | + p.Name, |
| 65 | + metav1.GetOptions{}) |
| 66 | + framework.ExpectNoError(err, "Error retrieving pod") |
| 67 | + framework.ExpectEqual(pod.Status.Phase, v1.PodRunning) |
| 68 | + allPods = append(allPods, pod) |
| 69 | + } |
| 70 | + ginkgo.By("Ensuring cpu doesn't exceed limit by >5%") |
| 71 | + for _, p := range allPods { |
| 72 | + ginkgo.By("Gathering node summary stats") |
| 73 | + nodeStats, err := e2ekubelet.GetStatsSummary(f.ClientSet, p.Spec.NodeName) |
| 74 | + framework.ExpectNoError(err, "Error grabbing node summary stats") |
| 75 | + found := false |
| 76 | + cpuUsage := float64(0) |
| 77 | + for _, pod := range nodeStats.Pods { |
| 78 | + if pod.PodRef.Name != p.Name || pod.PodRef.Namespace != p.Namespace { |
| 79 | + continue |
| 80 | + } |
| 81 | + cpuUsage = float64(*pod.CPU.UsageNanoCores) * 1e-9 |
| 82 | + found = true |
| 83 | + break |
| 84 | + } |
| 85 | + framework.ExpectEqual(found, true, "Found pod in stats summary") |
| 86 | + framework.Logf("Pod %s usage: %v", p.Name, cpuUsage) |
| 87 | + framework.ExpectEqual(cpuUsage > 0, true, "Pods reported usage should be > 0") |
| 88 | + framework.ExpectEqual((.5*1.05) > cpuUsage, true, "Pods reported usage should not exceed limit by >5%") |
| 89 | + } |
| 90 | + }) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +// newCPUBurnPods creates a list of pods (specification) with a workload that will consume all available CPU resources up to container limit |
| 95 | +func newCPUBurnPods(numPods int, image imageutils.Config, cpuLimit string, memoryLimit string) []*v1.Pod { |
| 96 | + var pods []*v1.Pod |
| 97 | + |
| 98 | + memLimitQuantity, err := resource.ParseQuantity(memoryLimit) |
| 99 | + framework.ExpectNoError(err) |
| 100 | + |
| 101 | + cpuLimitQuantity, err := resource.ParseQuantity(cpuLimit) |
| 102 | + framework.ExpectNoError(err) |
| 103 | + |
| 104 | + for i := 0; i < numPods; i++ { |
| 105 | + |
| 106 | + podName := "cpulimittest-" + string(uuid.NewUUID()) |
| 107 | + pod := v1.Pod{ |
| 108 | + ObjectMeta: metav1.ObjectMeta{ |
| 109 | + Name: podName, |
| 110 | + Labels: map[string]string{ |
| 111 | + "name": podName, |
| 112 | + "testapp": "cpuburn", |
| 113 | + }, |
| 114 | + }, |
| 115 | + Spec: v1.PodSpec{ |
| 116 | + // Restart policy is always (default). |
| 117 | + Containers: []v1.Container{ |
| 118 | + { |
| 119 | + Image: image.GetE2EImage(), |
| 120 | + Name: podName, |
| 121 | + Resources: v1.ResourceRequirements{ |
| 122 | + Limits: v1.ResourceList{ |
| 123 | + v1.ResourceMemory: memLimitQuantity, |
| 124 | + v1.ResourceCPU: cpuLimitQuantity, |
| 125 | + }, |
| 126 | + }, |
| 127 | + Command: []string{ |
| 128 | + "powershell.exe", |
| 129 | + "-Command", |
| 130 | + "foreach ($loopnumber in 1..8) { Start-Job -ScriptBlock { $result = 1; foreach($mm in 1..2147483647){$res1=1;foreach($num in 1..2147483647){$res1=$mm*$num*1340371};$res1} } } ; get-job | wait-job", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + NodeSelector: map[string]string{ |
| 135 | + "beta.kubernetes.io/os": "windows", |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + pods = append(pods, &pod) |
| 141 | + } |
| 142 | + |
| 143 | + return pods |
| 144 | +} |
0 commit comments