preemption_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. Copyright 2017 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 preemption
  14. import (
  15. "fmt"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. "k8s.io/client-go/tools/record"
  22. featuregatetesting "k8s.io/component-base/featuregate/testing"
  23. kubeapi "k8s.io/kubernetes/pkg/apis/core"
  24. "k8s.io/kubernetes/pkg/apis/scheduling"
  25. "k8s.io/kubernetes/pkg/features"
  26. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  27. )
  28. const (
  29. critical = "critical"
  30. clusterCritical = "cluster-critical"
  31. nodeCritical = "node-critical"
  32. bestEffort = "bestEffort"
  33. burstable = "burstable"
  34. highRequestBurstable = "high-request-burstable"
  35. guaranteed = "guaranteed"
  36. highRequestGuaranteed = "high-request-guaranteed"
  37. tinyBurstable = "tiny"
  38. maxPods = 110
  39. )
  40. type fakePodKiller struct {
  41. killedPods []*v1.Pod
  42. errDuringPodKilling bool
  43. }
  44. func newFakePodKiller(errPodKilling bool) *fakePodKiller {
  45. return &fakePodKiller{killedPods: []*v1.Pod{}, errDuringPodKilling: errPodKilling}
  46. }
  47. func (f *fakePodKiller) clear() {
  48. f.killedPods = []*v1.Pod{}
  49. }
  50. func (f *fakePodKiller) getKilledPods() []*v1.Pod {
  51. return f.killedPods
  52. }
  53. func (f *fakePodKiller) killPodNow(pod *v1.Pod, status v1.PodStatus, gracePeriodOverride *int64) error {
  54. if f.errDuringPodKilling {
  55. f.killedPods = []*v1.Pod{}
  56. return fmt.Errorf("problem killing pod %v", pod)
  57. }
  58. f.killedPods = append(f.killedPods, pod)
  59. return nil
  60. }
  61. type fakePodProvider struct {
  62. pods []*v1.Pod
  63. }
  64. func newFakePodProvider() *fakePodProvider {
  65. return &fakePodProvider{pods: []*v1.Pod{}}
  66. }
  67. func (f *fakePodProvider) setPods(pods []*v1.Pod) {
  68. f.pods = pods
  69. }
  70. func (f *fakePodProvider) getPods() []*v1.Pod {
  71. return f.pods
  72. }
  73. func getTestCriticalPodAdmissionHandler(podProvider *fakePodProvider, podKiller *fakePodKiller) *CriticalPodAdmissionHandler {
  74. return &CriticalPodAdmissionHandler{
  75. getPodsFunc: podProvider.getPods,
  76. killPodFunc: podKiller.killPodNow,
  77. recorder: &record.FakeRecorder{},
  78. }
  79. }
  80. func TestEvictPodsToFreeRequestsWithError(t *testing.T) {
  81. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExperimentalCriticalPodAnnotation, true)()
  82. type testRun struct {
  83. testName string
  84. inputPods []*v1.Pod
  85. insufficientResources admissionRequirementList
  86. expectErr bool
  87. expectedOutput []*v1.Pod
  88. }
  89. podProvider := newFakePodProvider()
  90. podKiller := newFakePodKiller(true)
  91. criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
  92. allPods := getTestPods()
  93. runs := []testRun{
  94. {
  95. testName: "multiple pods eviction error",
  96. inputPods: []*v1.Pod{
  97. allPods[critical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
  98. allPods[guaranteed], allPods[highRequestGuaranteed]},
  99. insufficientResources: getAdmissionRequirementList(0, 550, 0),
  100. expectErr: false,
  101. expectedOutput: nil,
  102. },
  103. }
  104. for _, r := range runs {
  105. podProvider.setPods(r.inputPods)
  106. outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[critical], r.insufficientResources)
  107. outputPods := podKiller.getKilledPods()
  108. if !r.expectErr && outErr != nil {
  109. t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
  110. } else if r.expectErr && outErr == nil {
  111. t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
  112. } else if !podListEqual(r.expectedOutput, outputPods) {
  113. t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
  114. }
  115. podKiller.clear()
  116. }
  117. }
  118. func TestEvictPodsToFreeRequests(t *testing.T) {
  119. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExperimentalCriticalPodAnnotation, true)()
  120. type testRun struct {
  121. testName string
  122. inputPods []*v1.Pod
  123. insufficientResources admissionRequirementList
  124. expectErr bool
  125. expectedOutput []*v1.Pod
  126. }
  127. podProvider := newFakePodProvider()
  128. podKiller := newFakePodKiller(false)
  129. criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
  130. allPods := getTestPods()
  131. runs := []testRun{
  132. {
  133. testName: "critical pods cannot be preempted",
  134. inputPods: []*v1.Pod{allPods[critical]},
  135. insufficientResources: getAdmissionRequirementList(0, 0, 1),
  136. expectErr: true,
  137. expectedOutput: nil,
  138. },
  139. {
  140. testName: "best effort pods are not preempted when attempting to free resources",
  141. inputPods: []*v1.Pod{allPods[bestEffort]},
  142. insufficientResources: getAdmissionRequirementList(0, 1, 0),
  143. expectErr: true,
  144. expectedOutput: nil,
  145. },
  146. {
  147. testName: "multiple pods evicted",
  148. inputPods: []*v1.Pod{
  149. allPods[critical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
  150. allPods[guaranteed], allPods[highRequestGuaranteed]},
  151. insufficientResources: getAdmissionRequirementList(0, 550, 0),
  152. expectErr: false,
  153. expectedOutput: []*v1.Pod{allPods[highRequestBurstable], allPods[highRequestGuaranteed]},
  154. },
  155. }
  156. for _, r := range runs {
  157. podProvider.setPods(r.inputPods)
  158. outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[critical], r.insufficientResources)
  159. outputPods := podKiller.getKilledPods()
  160. if !r.expectErr && outErr != nil {
  161. t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
  162. } else if r.expectErr && outErr == nil {
  163. t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
  164. } else if !podListEqual(r.expectedOutput, outputPods) {
  165. t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
  166. }
  167. podKiller.clear()
  168. }
  169. }
  170. func BenchmarkGetPodsToPreempt(t *testing.B) {
  171. allPods := getTestPods()
  172. inputPods := []*v1.Pod{}
  173. for i := 0; i < maxPods; i++ {
  174. inputPods = append(inputPods, allPods[tinyBurstable])
  175. }
  176. for n := 0; n < t.N; n++ {
  177. getPodsToPreempt(nil, inputPods, admissionRequirementList([]*admissionRequirement{
  178. {
  179. resourceName: v1.ResourceCPU,
  180. quantity: parseCPUToInt64("110m"),
  181. }}))
  182. }
  183. }
  184. func TestGetPodsToPreempt(t *testing.T) {
  185. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ExperimentalCriticalPodAnnotation, true)()
  186. type testRun struct {
  187. testName string
  188. preemptor *v1.Pod
  189. inputPods []*v1.Pod
  190. insufficientResources admissionRequirementList
  191. expectErr bool
  192. expectedOutput []*v1.Pod
  193. }
  194. allPods := getTestPods()
  195. runs := []testRun{
  196. {
  197. testName: "no requirements",
  198. preemptor: allPods[critical],
  199. inputPods: []*v1.Pod{},
  200. insufficientResources: getAdmissionRequirementList(0, 0, 0),
  201. expectErr: false,
  202. expectedOutput: []*v1.Pod{},
  203. },
  204. {
  205. testName: "no pods",
  206. preemptor: allPods[critical],
  207. inputPods: []*v1.Pod{},
  208. insufficientResources: getAdmissionRequirementList(0, 0, 1),
  209. expectErr: true,
  210. expectedOutput: nil,
  211. },
  212. {
  213. testName: "equal pods and resources requirements",
  214. preemptor: allPods[critical],
  215. inputPods: []*v1.Pod{allPods[burstable]},
  216. insufficientResources: getAdmissionRequirementList(100, 100, 1),
  217. expectErr: false,
  218. expectedOutput: []*v1.Pod{allPods[burstable]},
  219. },
  220. {
  221. testName: "higher requirements than pod requests",
  222. preemptor: allPods[critical],
  223. inputPods: []*v1.Pod{allPods[burstable]},
  224. insufficientResources: getAdmissionRequirementList(200, 200, 2),
  225. expectErr: true,
  226. expectedOutput: nil,
  227. },
  228. {
  229. testName: "choose between bestEffort and burstable",
  230. preemptor: allPods[critical],
  231. inputPods: []*v1.Pod{allPods[burstable], allPods[bestEffort]},
  232. insufficientResources: getAdmissionRequirementList(0, 0, 1),
  233. expectErr: false,
  234. expectedOutput: []*v1.Pod{allPods[bestEffort]},
  235. },
  236. {
  237. testName: "choose between burstable and guaranteed",
  238. preemptor: allPods[critical],
  239. inputPods: []*v1.Pod{allPods[burstable], allPods[guaranteed]},
  240. insufficientResources: getAdmissionRequirementList(0, 0, 1),
  241. expectErr: false,
  242. expectedOutput: []*v1.Pod{allPods[burstable]},
  243. },
  244. {
  245. testName: "choose lower request burstable if it meets requirements",
  246. preemptor: allPods[critical],
  247. inputPods: []*v1.Pod{allPods[bestEffort], allPods[highRequestBurstable], allPods[burstable]},
  248. insufficientResources: getAdmissionRequirementList(100, 100, 0),
  249. expectErr: false,
  250. expectedOutput: []*v1.Pod{allPods[burstable]},
  251. },
  252. {
  253. testName: "choose higher request burstable if lower does not meet requirements",
  254. preemptor: allPods[critical],
  255. inputPods: []*v1.Pod{allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable]},
  256. insufficientResources: getAdmissionRequirementList(150, 150, 0),
  257. expectErr: false,
  258. expectedOutput: []*v1.Pod{allPods[highRequestBurstable]},
  259. },
  260. {
  261. testName: "multiple pods required",
  262. preemptor: allPods[critical],
  263. inputPods: []*v1.Pod{allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable], allPods[guaranteed], allPods[highRequestGuaranteed]},
  264. insufficientResources: getAdmissionRequirementList(350, 350, 0),
  265. expectErr: false,
  266. expectedOutput: []*v1.Pod{allPods[burstable], allPods[highRequestBurstable]},
  267. },
  268. {
  269. testName: "evict guaranteed when we have to, and dont evict the extra burstable",
  270. preemptor: allPods[critical],
  271. inputPods: []*v1.Pod{allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable], allPods[guaranteed], allPods[highRequestGuaranteed]},
  272. insufficientResources: getAdmissionRequirementList(0, 550, 0),
  273. expectErr: false,
  274. expectedOutput: []*v1.Pod{allPods[highRequestBurstable], allPods[highRequestGuaranteed]},
  275. },
  276. {
  277. testName: "evict cluster critical pod for node critical pod",
  278. preemptor: allPods[nodeCritical],
  279. inputPods: []*v1.Pod{allPods[clusterCritical]},
  280. insufficientResources: getAdmissionRequirementList(100, 0, 0),
  281. expectErr: false,
  282. expectedOutput: []*v1.Pod{allPods[clusterCritical]},
  283. },
  284. {
  285. testName: "can not evict node critical pod for cluster critical pod",
  286. preemptor: allPods[clusterCritical],
  287. inputPods: []*v1.Pod{allPods[nodeCritical]},
  288. insufficientResources: getAdmissionRequirementList(100, 0, 0),
  289. expectErr: true,
  290. expectedOutput: nil,
  291. },
  292. }
  293. for _, r := range runs {
  294. outputPods, outErr := getPodsToPreempt(r.preemptor, r.inputPods, r.insufficientResources)
  295. if !r.expectErr && outErr != nil {
  296. t.Errorf("getPodsToPreempt returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
  297. } else if r.expectErr && outErr == nil {
  298. t.Errorf("getPodsToPreempt expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
  299. } else if !podListEqual(r.expectedOutput, outputPods) {
  300. t.Errorf("getPodsToPreempt expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
  301. }
  302. }
  303. }
  304. func TestAdmissionRequirementsDistance(t *testing.T) {
  305. type testRun struct {
  306. testName string
  307. requirements admissionRequirementList
  308. inputPod *v1.Pod
  309. expectedOutput float64
  310. }
  311. allPods := getTestPods()
  312. runs := []testRun{
  313. {
  314. testName: "no requirements",
  315. requirements: getAdmissionRequirementList(0, 0, 0),
  316. inputPod: allPods[burstable],
  317. expectedOutput: 0,
  318. },
  319. {
  320. testName: "no requests, some requirements",
  321. requirements: getAdmissionRequirementList(100, 100, 1),
  322. inputPod: allPods[bestEffort],
  323. expectedOutput: 2,
  324. },
  325. {
  326. testName: "equal requests and requirements",
  327. requirements: getAdmissionRequirementList(100, 100, 1),
  328. inputPod: allPods[burstable],
  329. expectedOutput: 0,
  330. },
  331. {
  332. testName: "higher requests than requirements",
  333. requirements: getAdmissionRequirementList(50, 50, 0),
  334. inputPod: allPods[burstable],
  335. expectedOutput: 0,
  336. },
  337. }
  338. for _, run := range runs {
  339. output := run.requirements.distance(run.inputPod)
  340. if output != run.expectedOutput {
  341. t.Errorf("expected: %f, got: %f for %s test", run.expectedOutput, output, run.testName)
  342. }
  343. }
  344. }
  345. func TestAdmissionRequirementsSubtract(t *testing.T) {
  346. type testRun struct {
  347. testName string
  348. initial admissionRequirementList
  349. inputPod *v1.Pod
  350. expectedOutput admissionRequirementList
  351. }
  352. allPods := getTestPods()
  353. runs := []testRun{
  354. {
  355. testName: "subtract a pod from no requirements",
  356. initial: getAdmissionRequirementList(0, 0, 0),
  357. inputPod: allPods[burstable],
  358. expectedOutput: getAdmissionRequirementList(0, 0, 0),
  359. },
  360. {
  361. testName: "subtract no requests from some requirements",
  362. initial: getAdmissionRequirementList(100, 100, 1),
  363. inputPod: allPods[bestEffort],
  364. expectedOutput: getAdmissionRequirementList(100, 100, 0),
  365. },
  366. {
  367. testName: "equal requests and requirements",
  368. initial: getAdmissionRequirementList(100, 100, 1),
  369. inputPod: allPods[burstable],
  370. expectedOutput: getAdmissionRequirementList(0, 0, 0),
  371. },
  372. {
  373. testName: "subtract higher requests than requirements",
  374. initial: getAdmissionRequirementList(50, 50, 0),
  375. inputPod: allPods[burstable],
  376. expectedOutput: getAdmissionRequirementList(0, 0, 0),
  377. },
  378. {
  379. testName: "subtract lower requests than requirements",
  380. initial: getAdmissionRequirementList(200, 200, 1),
  381. inputPod: allPods[burstable],
  382. expectedOutput: getAdmissionRequirementList(100, 100, 0),
  383. },
  384. }
  385. for _, run := range runs {
  386. output := run.initial.subtract(run.inputPod)
  387. if !admissionRequirementListEqual(output, run.expectedOutput) {
  388. t.Errorf("expected: %s, got: %s for %s test", run.expectedOutput.toString(), output.toString(), run.testName)
  389. }
  390. }
  391. }
  392. func getTestPods() map[string]*v1.Pod {
  393. allPods := map[string]*v1.Pod{
  394. tinyBurstable: getPodWithResources(tinyBurstable, v1.ResourceRequirements{
  395. Requests: v1.ResourceList{
  396. v1.ResourceCPU: resource.MustParse("1m"),
  397. v1.ResourceMemory: resource.MustParse("1Mi"),
  398. },
  399. }),
  400. bestEffort: getPodWithResources(bestEffort, v1.ResourceRequirements{}),
  401. critical: getPodWithResources(critical, v1.ResourceRequirements{
  402. Requests: v1.ResourceList{
  403. v1.ResourceCPU: resource.MustParse("100m"),
  404. v1.ResourceMemory: resource.MustParse("100Mi"),
  405. },
  406. }),
  407. clusterCritical: getPodWithResources(clusterCritical, v1.ResourceRequirements{
  408. Requests: v1.ResourceList{
  409. v1.ResourceCPU: resource.MustParse("100m"),
  410. v1.ResourceMemory: resource.MustParse("100Mi"),
  411. },
  412. }),
  413. nodeCritical: getPodWithResources(nodeCritical, v1.ResourceRequirements{
  414. Requests: v1.ResourceList{
  415. v1.ResourceCPU: resource.MustParse("100m"),
  416. v1.ResourceMemory: resource.MustParse("100Mi"),
  417. },
  418. }),
  419. burstable: getPodWithResources(burstable, v1.ResourceRequirements{
  420. Requests: v1.ResourceList{
  421. v1.ResourceCPU: resource.MustParse("100m"),
  422. v1.ResourceMemory: resource.MustParse("100Mi"),
  423. },
  424. }),
  425. guaranteed: getPodWithResources(guaranteed, v1.ResourceRequirements{
  426. Requests: v1.ResourceList{
  427. v1.ResourceCPU: resource.MustParse("100m"),
  428. v1.ResourceMemory: resource.MustParse("100Mi"),
  429. },
  430. Limits: v1.ResourceList{
  431. v1.ResourceCPU: resource.MustParse("100m"),
  432. v1.ResourceMemory: resource.MustParse("100Mi"),
  433. },
  434. }),
  435. highRequestBurstable: getPodWithResources(highRequestBurstable, v1.ResourceRequirements{
  436. Requests: v1.ResourceList{
  437. v1.ResourceCPU: resource.MustParse("300m"),
  438. v1.ResourceMemory: resource.MustParse("300Mi"),
  439. },
  440. }),
  441. highRequestGuaranteed: getPodWithResources(highRequestGuaranteed, v1.ResourceRequirements{
  442. Requests: v1.ResourceList{
  443. v1.ResourceCPU: resource.MustParse("300m"),
  444. v1.ResourceMemory: resource.MustParse("300Mi"),
  445. },
  446. Limits: v1.ResourceList{
  447. v1.ResourceCPU: resource.MustParse("300m"),
  448. v1.ResourceMemory: resource.MustParse("300Mi"),
  449. },
  450. }),
  451. }
  452. allPods[critical].Namespace = kubeapi.NamespaceSystem
  453. allPods[critical].Annotations[kubetypes.CriticalPodAnnotationKey] = ""
  454. allPods[clusterCritical].Namespace = kubeapi.NamespaceSystem
  455. allPods[clusterCritical].Spec.PriorityClassName = scheduling.SystemClusterCritical
  456. clusterPriority := scheduling.SystemCriticalPriority
  457. allPods[clusterCritical].Spec.Priority = &clusterPriority
  458. allPods[nodeCritical].Namespace = kubeapi.NamespaceSystem
  459. allPods[nodeCritical].Spec.PriorityClassName = scheduling.SystemNodeCritical
  460. nodePriority := scheduling.SystemCriticalPriority + 100
  461. allPods[nodeCritical].Spec.Priority = &nodePriority
  462. return allPods
  463. }
  464. func getPodWithResources(name string, requests v1.ResourceRequirements) *v1.Pod {
  465. return &v1.Pod{
  466. ObjectMeta: metav1.ObjectMeta{
  467. GenerateName: name,
  468. Annotations: map[string]string{},
  469. },
  470. Spec: v1.PodSpec{
  471. Containers: []v1.Container{
  472. {
  473. Name: fmt.Sprintf("%s-container", name),
  474. Resources: requests,
  475. },
  476. },
  477. },
  478. }
  479. }
  480. func parseCPUToInt64(res string) int64 {
  481. r := resource.MustParse(res)
  482. return (&r).MilliValue()
  483. }
  484. func parseNonCpuResourceToInt64(res string) int64 {
  485. r := resource.MustParse(res)
  486. return (&r).Value()
  487. }
  488. func getAdmissionRequirementList(cpu, memory, pods int) admissionRequirementList {
  489. reqs := []*admissionRequirement{}
  490. if cpu > 0 {
  491. reqs = append(reqs, &admissionRequirement{
  492. resourceName: v1.ResourceCPU,
  493. quantity: parseCPUToInt64(fmt.Sprintf("%dm", cpu)),
  494. })
  495. }
  496. if memory > 0 {
  497. reqs = append(reqs, &admissionRequirement{
  498. resourceName: v1.ResourceMemory,
  499. quantity: parseNonCpuResourceToInt64(fmt.Sprintf("%dMi", memory)),
  500. })
  501. }
  502. if pods > 0 {
  503. reqs = append(reqs, &admissionRequirement{
  504. resourceName: v1.ResourcePods,
  505. quantity: int64(pods),
  506. })
  507. }
  508. return admissionRequirementList(reqs)
  509. }
  510. // this checks if the lists contents contain all of the same elements.
  511. // this is not correct if there are duplicate pods in the list.
  512. // for example: podListEqual([a, a, b], [a, b, b]) will return true
  513. func admissionRequirementListEqual(list1 admissionRequirementList, list2 admissionRequirementList) bool {
  514. if len(list1) != len(list2) {
  515. return false
  516. }
  517. for _, a := range list1 {
  518. contains := false
  519. for _, b := range list2 {
  520. if a.resourceName == b.resourceName && a.quantity == b.quantity {
  521. contains = true
  522. }
  523. }
  524. if !contains {
  525. return false
  526. }
  527. }
  528. return true
  529. }
  530. // podListEqual checks if the lists contents contain all of the same elements.
  531. func podListEqual(list1 []*v1.Pod, list2 []*v1.Pod) bool {
  532. if len(list1) != len(list2) {
  533. return false
  534. }
  535. m := map[*v1.Pod]int{}
  536. for _, val := range list1 {
  537. m[val] = m[val] + 1
  538. }
  539. for _, val := range list2 {
  540. m[val] = m[val] - 1
  541. }
  542. for _, v := range m {
  543. if v != 0 {
  544. return false
  545. }
  546. }
  547. return true
  548. }