k8s.io/kubernetes@v1.29.3/pkg/scheduler/testing/workload_prep.go (about) 1 /* 2 Copyright 2019 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 testing 18 19 import ( 20 "fmt" 21 22 v1 "k8s.io/api/core/v1" 23 ) 24 25 type keyVal struct { 26 k string 27 v string 28 } 29 30 // MakeNodesAndPodsForEvenPodsSpread serves as a testing helper for EvenPodsSpread feature. 31 // It builds a fake cluster containing running Pods and Nodes. 32 // The size of Pods and Nodes are determined by input arguments. 33 // The specs of Pods and Nodes are generated with the following rules: 34 // - Each generated node is applied with a unique label: "node: node<i>". 35 // - Each generated node is applied with a rotating label: "zone: zone[0-9]". 36 // - Depending on the input labels, each generated pod will be applied with 37 // label "key1", "key1,key2", ..., "key1,key2,...,keyN" in a rotating manner. 38 func MakeNodesAndPodsForEvenPodsSpread(labels map[string]string, existingPodsNum, allNodesNum, filteredNodesNum int) (existingPods []*v1.Pod, allNodes []*v1.Node, filteredNodes []*v1.Node) { 39 var labelPairs []keyVal 40 for k, v := range labels { 41 labelPairs = append(labelPairs, keyVal{k: k, v: v}) 42 } 43 zones := 10 44 // build nodes 45 for i := 0; i < allNodesNum; i++ { 46 node := MakeNode().Name(fmt.Sprintf("node%d", i)). 47 Label(v1.LabelTopologyZone, fmt.Sprintf("zone%d", i%zones)). 48 Label(v1.LabelHostname, fmt.Sprintf("node%d", i)).Obj() 49 allNodes = append(allNodes, node) 50 } 51 filteredNodes = allNodes[:filteredNodesNum] 52 // build pods 53 for i := 0; i < existingPodsNum; i++ { 54 podWrapper := MakePod().Name(fmt.Sprintf("pod%d", i)).Node(fmt.Sprintf("node%d", i%allNodesNum)) 55 // apply labels[0], labels[0,1], ..., labels[all] to each pod in turn 56 for _, p := range labelPairs[:i%len(labelPairs)+1] { 57 podWrapper = podWrapper.Label(p.k, p.v) 58 } 59 existingPods = append(existingPods, podWrapper.Obj()) 60 } 61 return 62 }