|
| 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 scheduler |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/api/core/v1" |
| 23 | + "k8s.io/apimachinery/pkg/api/resource" |
| 24 | + utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 25 | + featuregatetesting "k8s.io/component-base/featuregate/testing" |
| 26 | + "k8s.io/kubernetes/pkg/features" |
| 27 | +) |
| 28 | + |
| 29 | +func TestNodeResourceLimits(t *testing.T) { |
| 30 | + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ResourceLimitsPriorityFunction, true)() |
| 31 | + |
| 32 | + context := initTest(t, "node-resource-limits") |
| 33 | + defer cleanupTest(t, context) |
| 34 | + |
| 35 | + // Add one node |
| 36 | + expectedNode, err := createNode(context.clientSet, "test-node1", &v1.ResourceList{ |
| 37 | + v1.ResourcePods: *resource.NewQuantity(32, resource.DecimalSI), |
| 38 | + v1.ResourceCPU: *resource.NewMilliQuantity(2000, resource.DecimalSI), |
| 39 | + v1.ResourceMemory: *resource.NewQuantity(2000, resource.DecimalSI), |
| 40 | + }) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Cannot create node: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Add another node with less resource |
| 46 | + _, err = createNode(context.clientSet, "test-node2", &v1.ResourceList{ |
| 47 | + v1.ResourcePods: *resource.NewQuantity(32, resource.DecimalSI), |
| 48 | + v1.ResourceCPU: *resource.NewMilliQuantity(1000, resource.DecimalSI), |
| 49 | + v1.ResourceMemory: *resource.NewQuantity(1000, resource.DecimalSI), |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("Cannot create node: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + podName := "pod-with-resource-limits" |
| 56 | + pod, err := runPausePod(context.clientSet, initPausePod(context.clientSet, &pausePodConfig{ |
| 57 | + Name: podName, |
| 58 | + Namespace: context.ns.Name, |
| 59 | + Resources: &v1.ResourceRequirements{Requests: v1.ResourceList{ |
| 60 | + v1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI), |
| 61 | + v1.ResourceMemory: *resource.NewQuantity(500, resource.DecimalSI)}, |
| 62 | + }, |
| 63 | + })) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("Error running pause pod: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + if pod.Spec.NodeName != expectedNode.Name { |
| 69 | + t.Errorf("pod %v got scheduled on an unexpected node: %v. Expected node: %v.", podName, pod.Spec.NodeName, expectedNode.Name) |
| 70 | + } else { |
| 71 | + t.Logf("pod %v got successfully scheduled on node %v.", podName, pod.Spec.NodeName) |
| 72 | + } |
| 73 | +} |
0 commit comments