workload_prep.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. )
  18. type keyVal struct {
  19. k string
  20. v string
  21. }
  22. // MakeNodesAndPodsForEvenPodsSpread serves as a testing helper for EvenPodsSpread feature.
  23. // It builds a fake cluster containing running Pods and Nodes.
  24. // The size of Pods and Nodes are determined by input arguments.
  25. // The specs of Pods and Nodes are generated with the following rules:
  26. // - Each generated node is applied with a unique label: "node: node<i>".
  27. // - Each generated node is applied with a rotating label: "zone: zone[0-9]".
  28. // - Depending on the input labels, each generated pod will be applied with
  29. // label "key1", "key1,key2", ..., "key1,key2,...,keyN" in a rotating manner.
  30. func MakeNodesAndPodsForEvenPodsSpread(labels map[string]string, existingPodsNum, allNodesNum, filteredNodesNum int) (existingPods []*v1.Pod, allNodes []*v1.Node, filteredNodes []*v1.Node) {
  31. var labelPairs []keyVal
  32. for k, v := range labels {
  33. labelPairs = append(labelPairs, keyVal{k: k, v: v})
  34. }
  35. zones := 10
  36. // build nodes
  37. for i := 0; i < allNodesNum; i++ {
  38. node := MakeNode().Name(fmt.Sprintf("node%d", i)).
  39. Label(v1.LabelZoneFailureDomain, fmt.Sprintf("zone%d", i%zones)).
  40. Label(v1.LabelHostname, fmt.Sprintf("node%d", i)).Obj()
  41. allNodes = append(allNodes, node)
  42. }
  43. filteredNodes = allNodes[:filteredNodesNum]
  44. // build pods
  45. for i := 0; i < existingPodsNum; i++ {
  46. podWrapper := MakePod().Name(fmt.Sprintf("pod%d", i)).Node(fmt.Sprintf("node%d", i%allNodesNum))
  47. // apply labels[0], labels[0,1], ..., labels[all] to each pod in turn
  48. for _, p := range labelPairs[:i%len(labelPairs)+1] {
  49. podWrapper = podWrapper.Label(p.k, p.v)
  50. }
  51. existingPods = append(existingPods, podWrapper.Obj())
  52. }
  53. return
  54. }
  55. // MakeNodesAndPodsForPodAffinity serves as a testing helper for Pod(Anti)Affinity feature.
  56. // It builds a fake cluster containing running Pods and Nodes.
  57. // For simplicity, the Nodes will be labelled with "region", "zone" and "node". Nodes[i] will be applied with:
  58. // - "region": "region" + i%3
  59. // - "zone": "zone" + i%10
  60. // - "node": "node" + i
  61. // The Pods will be applied with various combinations of PodAffinity and PodAntiAffinity terms.
  62. func MakeNodesAndPodsForPodAffinity(existingPodsNum, allNodesNum int) (existingPods []*v1.Pod, allNodes []*v1.Node) {
  63. tpKeyToSizeMap := map[string]int{
  64. "region": 3,
  65. "zone": 10,
  66. "node": allNodesNum,
  67. }
  68. // build nodes to spread across all topology domains
  69. for i := 0; i < allNodesNum; i++ {
  70. nodeName := fmt.Sprintf("node%d", i)
  71. nodeWrapper := MakeNode().Name(nodeName)
  72. for tpKey, size := range tpKeyToSizeMap {
  73. nodeWrapper = nodeWrapper.Label(tpKey, fmt.Sprintf("%s%d", tpKey, i%size))
  74. }
  75. allNodes = append(allNodes, nodeWrapper.Obj())
  76. }
  77. labels := []string{"foo", "bar", "baz"}
  78. tpKeys := []string{"region", "zone", "node"}
  79. // Build pods.
  80. // Each pod will be created with one affinity and one anti-affinity terms using all combinations of
  81. // affinity and anti-affinity kinds listed below
  82. // e.g., the first pod will have {affinity, anti-affinity} terms of kinds {NilPodAffinity, NilPodAffinity};
  83. // the second will be {NilPodAffinity, PodAntiAffinityWithRequiredReq}, etc.
  84. affinityKinds := []PodAffinityKind{
  85. NilPodAffinity,
  86. PodAffinityWithRequiredReq,
  87. PodAffinityWithPreferredReq,
  88. PodAffinityWithRequiredPreferredReq,
  89. }
  90. antiAffinityKinds := []PodAffinityKind{
  91. NilPodAffinity,
  92. PodAntiAffinityWithRequiredReq,
  93. PodAntiAffinityWithPreferredReq,
  94. PodAntiAffinityWithRequiredPreferredReq,
  95. }
  96. totalSize := len(affinityKinds) * len(antiAffinityKinds)
  97. for i := 0; i < existingPodsNum; i++ {
  98. podWrapper := MakePod().Name(fmt.Sprintf("pod%d", i)).Node(fmt.Sprintf("node%d", i%allNodesNum))
  99. label, tpKey := labels[i%len(labels)], tpKeys[i%len(tpKeys)]
  100. affinityIdx := i % totalSize
  101. // len(affinityKinds) is equal to len(antiAffinityKinds)
  102. leftIdx, rightIdx := affinityIdx/len(affinityKinds), affinityIdx%len(affinityKinds)
  103. podWrapper = podWrapper.PodAffinityExists(label, tpKey, affinityKinds[leftIdx])
  104. podWrapper = podWrapper.PodAntiAffinityExists(label, tpKey, antiAffinityKinds[rightIdx])
  105. existingPods = append(existingPods, podWrapper.Obj())
  106. }
  107. return
  108. }
  109. // MakeNodesAndPods serves as a testing helper to generate regular Nodes and Pods
  110. // that don't use any advanced scheduling features.
  111. func MakeNodesAndPods(existingPodsNum, allNodesNum int) (existingPods []*v1.Pod, allNodes []*v1.Node) {
  112. // build nodes
  113. for i := 0; i < allNodesNum; i++ {
  114. allNodes = append(allNodes, MakeNode().Name(fmt.Sprintf("node%d", i)).Obj())
  115. }
  116. // build pods
  117. for i := 0; i < existingPodsNum; i++ {
  118. podWrapper := MakePod().Name(fmt.Sprintf("pod%d", i)).Node(fmt.Sprintf("node%d", i%allNodesNum))
  119. existingPods = append(existingPods, podWrapper.Obj())
  120. }
  121. return
  122. }