non_csi_test.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 nodevolumelimits
  14. import (
  15. "context"
  16. "os"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. csilibplugins "k8s.io/csi-translation-lib/plugins"
  23. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  24. fakelisters "k8s.io/kubernetes/pkg/scheduler/listers/fake"
  25. utilpointer "k8s.io/utils/pointer"
  26. )
  27. func TestAzureDiskLimits(t *testing.T) {
  28. oneVolPod := &v1.Pod{
  29. Spec: v1.PodSpec{
  30. Volumes: []v1.Volume{
  31. {
  32. VolumeSource: v1.VolumeSource{
  33. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "ovp"},
  34. },
  35. },
  36. },
  37. },
  38. }
  39. twoVolPod := &v1.Pod{
  40. Spec: v1.PodSpec{
  41. Volumes: []v1.Volume{
  42. {
  43. VolumeSource: v1.VolumeSource{
  44. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "tvp1"},
  45. },
  46. },
  47. {
  48. VolumeSource: v1.VolumeSource{
  49. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "tvp2"},
  50. },
  51. },
  52. },
  53. },
  54. }
  55. splitVolsPod := &v1.Pod{
  56. Spec: v1.PodSpec{
  57. Volumes: []v1.Volume{
  58. {
  59. VolumeSource: v1.VolumeSource{
  60. HostPath: &v1.HostPathVolumeSource{},
  61. },
  62. },
  63. {
  64. VolumeSource: v1.VolumeSource{
  65. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "svp"},
  66. },
  67. },
  68. },
  69. },
  70. }
  71. nonApplicablePod := &v1.Pod{
  72. Spec: v1.PodSpec{
  73. Volumes: []v1.Volume{
  74. {
  75. VolumeSource: v1.VolumeSource{
  76. HostPath: &v1.HostPathVolumeSource{},
  77. },
  78. },
  79. },
  80. },
  81. }
  82. deletedPVCPod := &v1.Pod{
  83. Spec: v1.PodSpec{
  84. Volumes: []v1.Volume{
  85. {
  86. VolumeSource: v1.VolumeSource{
  87. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  88. ClaimName: "deletedPVC",
  89. },
  90. },
  91. },
  92. },
  93. },
  94. }
  95. twoDeletedPVCPod := &v1.Pod{
  96. Spec: v1.PodSpec{
  97. Volumes: []v1.Volume{
  98. {
  99. VolumeSource: v1.VolumeSource{
  100. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  101. ClaimName: "deletedPVC",
  102. },
  103. },
  104. },
  105. {
  106. VolumeSource: v1.VolumeSource{
  107. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  108. ClaimName: "anotherDeletedPVC",
  109. },
  110. },
  111. },
  112. },
  113. },
  114. }
  115. deletedPVPod := &v1.Pod{
  116. Spec: v1.PodSpec{
  117. Volumes: []v1.Volume{
  118. {
  119. VolumeSource: v1.VolumeSource{
  120. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  121. ClaimName: "pvcWithDeletedPV",
  122. },
  123. },
  124. },
  125. },
  126. },
  127. }
  128. // deletedPVPod2 is a different pod than deletedPVPod but using the same PVC
  129. deletedPVPod2 := &v1.Pod{
  130. Spec: v1.PodSpec{
  131. Volumes: []v1.Volume{
  132. {
  133. VolumeSource: v1.VolumeSource{
  134. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  135. ClaimName: "pvcWithDeletedPV",
  136. },
  137. },
  138. },
  139. },
  140. },
  141. }
  142. // anotherDeletedPVPod is a different pod than deletedPVPod and uses another PVC
  143. anotherDeletedPVPod := &v1.Pod{
  144. Spec: v1.PodSpec{
  145. Volumes: []v1.Volume{
  146. {
  147. VolumeSource: v1.VolumeSource{
  148. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  149. ClaimName: "anotherPVCWithDeletedPV",
  150. },
  151. },
  152. },
  153. },
  154. },
  155. }
  156. emptyPod := &v1.Pod{
  157. Spec: v1.PodSpec{},
  158. }
  159. unboundPVCPod := &v1.Pod{
  160. Spec: v1.PodSpec{
  161. Volumes: []v1.Volume{
  162. {
  163. VolumeSource: v1.VolumeSource{
  164. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  165. ClaimName: "unboundPVC",
  166. },
  167. },
  168. },
  169. },
  170. },
  171. }
  172. // Different pod than unboundPVCPod, but using the same unbound PVC
  173. unboundPVCPod2 := &v1.Pod{
  174. Spec: v1.PodSpec{
  175. Volumes: []v1.Volume{
  176. {
  177. VolumeSource: v1.VolumeSource{
  178. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  179. ClaimName: "unboundPVC",
  180. },
  181. },
  182. },
  183. },
  184. },
  185. }
  186. // pod with unbound PVC that's different to unboundPVC
  187. anotherUnboundPVCPod := &v1.Pod{
  188. Spec: v1.PodSpec{
  189. Volumes: []v1.Volume{
  190. {
  191. VolumeSource: v1.VolumeSource{
  192. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  193. ClaimName: "anotherUnboundPVC",
  194. },
  195. },
  196. },
  197. },
  198. },
  199. }
  200. tests := []struct {
  201. newPod *v1.Pod
  202. existingPods []*v1.Pod
  203. filterName string
  204. driverName string
  205. maxVols int
  206. test string
  207. wantStatus *framework.Status
  208. }{
  209. {
  210. newPod: oneVolPod,
  211. existingPods: []*v1.Pod{twoVolPod, oneVolPod},
  212. filterName: azureDiskVolumeFilterType,
  213. maxVols: 4,
  214. test: "fits when node capacity >= new pod's AzureDisk volumes",
  215. },
  216. {
  217. newPod: twoVolPod,
  218. existingPods: []*v1.Pod{oneVolPod},
  219. filterName: azureDiskVolumeFilterType,
  220. maxVols: 2,
  221. test: "fit when node capacity < new pod's AzureDisk volumes",
  222. },
  223. {
  224. newPod: splitVolsPod,
  225. existingPods: []*v1.Pod{twoVolPod},
  226. filterName: azureDiskVolumeFilterType,
  227. maxVols: 3,
  228. test: "new pod's count ignores non-AzureDisk volumes",
  229. },
  230. {
  231. newPod: twoVolPod,
  232. existingPods: []*v1.Pod{splitVolsPod, nonApplicablePod, emptyPod},
  233. filterName: azureDiskVolumeFilterType,
  234. maxVols: 3,
  235. test: "existing pods' counts ignore non-AzureDisk volumes",
  236. },
  237. {
  238. newPod: onePVCPod(azureDiskVolumeFilterType),
  239. existingPods: []*v1.Pod{splitVolsPod, nonApplicablePod, emptyPod},
  240. filterName: azureDiskVolumeFilterType,
  241. maxVols: 3,
  242. test: "new pod's count considers PVCs backed by AzureDisk volumes",
  243. },
  244. {
  245. newPod: splitPVCPod(azureDiskVolumeFilterType),
  246. existingPods: []*v1.Pod{splitVolsPod, oneVolPod},
  247. filterName: azureDiskVolumeFilterType,
  248. maxVols: 3,
  249. test: "new pod's count ignores PVCs not backed by AzureDisk volumes",
  250. },
  251. {
  252. newPod: twoVolPod,
  253. existingPods: []*v1.Pod{oneVolPod, onePVCPod(azureDiskVolumeFilterType)},
  254. filterName: azureDiskVolumeFilterType,
  255. maxVols: 3,
  256. test: "existing pods' counts considers PVCs backed by AzureDisk volumes",
  257. },
  258. {
  259. newPod: twoVolPod,
  260. existingPods: []*v1.Pod{oneVolPod, twoVolPod, onePVCPod(azureDiskVolumeFilterType)},
  261. filterName: azureDiskVolumeFilterType,
  262. maxVols: 4,
  263. test: "already-mounted AzureDisk volumes are always ok to allow",
  264. },
  265. {
  266. newPod: splitVolsPod,
  267. existingPods: []*v1.Pod{oneVolPod, oneVolPod, onePVCPod(azureDiskVolumeFilterType)},
  268. filterName: azureDiskVolumeFilterType,
  269. maxVols: 3,
  270. test: "the same AzureDisk volumes are not counted multiple times",
  271. },
  272. {
  273. newPod: onePVCPod(azureDiskVolumeFilterType),
  274. existingPods: []*v1.Pod{oneVolPod, deletedPVCPod},
  275. filterName: azureDiskVolumeFilterType,
  276. maxVols: 2,
  277. test: "pod with missing PVC is counted towards the PV limit",
  278. },
  279. {
  280. newPod: onePVCPod(azureDiskVolumeFilterType),
  281. existingPods: []*v1.Pod{oneVolPod, deletedPVCPod},
  282. filterName: azureDiskVolumeFilterType,
  283. maxVols: 3,
  284. test: "pod with missing PVC is counted towards the PV limit",
  285. },
  286. {
  287. newPod: onePVCPod(azureDiskVolumeFilterType),
  288. existingPods: []*v1.Pod{oneVolPod, twoDeletedPVCPod},
  289. filterName: azureDiskVolumeFilterType,
  290. maxVols: 3,
  291. test: "pod with missing two PVCs is counted towards the PV limit twice",
  292. },
  293. {
  294. newPod: onePVCPod(azureDiskVolumeFilterType),
  295. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  296. filterName: azureDiskVolumeFilterType,
  297. maxVols: 2,
  298. test: "pod with missing PV is counted towards the PV limit",
  299. },
  300. {
  301. newPod: onePVCPod(azureDiskVolumeFilterType),
  302. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  303. filterName: azureDiskVolumeFilterType,
  304. maxVols: 3,
  305. test: "pod with missing PV is counted towards the PV limit",
  306. },
  307. {
  308. newPod: deletedPVPod2,
  309. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  310. filterName: azureDiskVolumeFilterType,
  311. maxVols: 2,
  312. test: "two pods missing the same PV are counted towards the PV limit only once",
  313. },
  314. {
  315. newPod: anotherDeletedPVPod,
  316. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  317. filterName: azureDiskVolumeFilterType,
  318. maxVols: 2,
  319. test: "two pods missing different PVs are counted towards the PV limit twice",
  320. },
  321. {
  322. newPod: onePVCPod(azureDiskVolumeFilterType),
  323. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  324. filterName: azureDiskVolumeFilterType,
  325. maxVols: 2,
  326. test: "pod with unbound PVC is counted towards the PV limit",
  327. },
  328. {
  329. newPod: onePVCPod(azureDiskVolumeFilterType),
  330. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  331. filterName: azureDiskVolumeFilterType,
  332. maxVols: 3,
  333. test: "pod with unbound PVC is counted towards the PV limit",
  334. },
  335. {
  336. newPod: unboundPVCPod2,
  337. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  338. filterName: azureDiskVolumeFilterType,
  339. maxVols: 2,
  340. test: "the same unbound PVC in multiple pods is counted towards the PV limit only once",
  341. },
  342. {
  343. newPod: anotherUnboundPVCPod,
  344. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  345. filterName: azureDiskVolumeFilterType,
  346. maxVols: 2,
  347. test: "two different unbound PVCs are counted towards the PV limit as two volumes",
  348. },
  349. }
  350. for _, test := range tests {
  351. t.Run(test.test, func(t *testing.T) {
  352. node, csiNode := getNodeWithPodAndVolumeLimits("node", test.existingPods, int64(test.maxVols), test.filterName)
  353. p := newNonCSILimits(test.filterName, getFakeCSINodeLister(csiNode), getFakeCSIStorageClassLister(test.filterName, test.driverName), getFakePVLister(test.filterName), getFakePVCLister(test.filterName)).(framework.FilterPlugin)
  354. gotStatus := p.Filter(context.Background(), nil, test.newPod, node)
  355. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  356. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  357. }
  358. })
  359. }
  360. }
  361. func TestCinderLimits(t *testing.T) {
  362. twoVolCinderPod := &v1.Pod{
  363. Spec: v1.PodSpec{
  364. Volumes: []v1.Volume{
  365. {
  366. VolumeSource: v1.VolumeSource{
  367. Cinder: &v1.CinderVolumeSource{VolumeID: "tvp1"},
  368. },
  369. },
  370. {
  371. VolumeSource: v1.VolumeSource{
  372. Cinder: &v1.CinderVolumeSource{VolumeID: "tvp2"},
  373. },
  374. },
  375. },
  376. },
  377. }
  378. oneVolCinderPod := &v1.Pod{
  379. Spec: v1.PodSpec{
  380. Volumes: []v1.Volume{
  381. {
  382. VolumeSource: v1.VolumeSource{
  383. Cinder: &v1.CinderVolumeSource{VolumeID: "ovp"},
  384. },
  385. },
  386. },
  387. },
  388. }
  389. tests := []struct {
  390. newPod *v1.Pod
  391. existingPods []*v1.Pod
  392. filterName string
  393. driverName string
  394. maxVols int
  395. test string
  396. wantStatus *framework.Status
  397. }{
  398. {
  399. newPod: oneVolCinderPod,
  400. existingPods: []*v1.Pod{twoVolCinderPod},
  401. filterName: cinderVolumeFilterType,
  402. maxVols: 4,
  403. test: "fits when node capacity >= new pod's Cinder volumes",
  404. },
  405. {
  406. newPod: oneVolCinderPod,
  407. existingPods: []*v1.Pod{twoVolCinderPod},
  408. filterName: cinderVolumeFilterType,
  409. maxVols: 2,
  410. test: "not fit when node capacity < new pod's Cinder volumes",
  411. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  412. },
  413. }
  414. for _, test := range tests {
  415. t.Run(test.test, func(t *testing.T) {
  416. node, csiNode := getNodeWithPodAndVolumeLimits("node", test.existingPods, int64(test.maxVols), test.filterName)
  417. p := newNonCSILimits(test.filterName, getFakeCSINodeLister(csiNode), getFakeCSIStorageClassLister(test.filterName, test.driverName), getFakePVLister(test.filterName), getFakePVCLister(test.filterName)).(framework.FilterPlugin)
  418. gotStatus := p.Filter(context.Background(), nil, test.newPod, node)
  419. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  420. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  421. }
  422. })
  423. }
  424. }
  425. func TestEBSLimits(t *testing.T) {
  426. oneVolPod := &v1.Pod{
  427. Spec: v1.PodSpec{
  428. Volumes: []v1.Volume{
  429. {
  430. VolumeSource: v1.VolumeSource{
  431. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "ovp"},
  432. },
  433. },
  434. },
  435. },
  436. }
  437. twoVolPod := &v1.Pod{
  438. Spec: v1.PodSpec{
  439. Volumes: []v1.Volume{
  440. {
  441. VolumeSource: v1.VolumeSource{
  442. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "tvp1"},
  443. },
  444. },
  445. {
  446. VolumeSource: v1.VolumeSource{
  447. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "tvp2"},
  448. },
  449. },
  450. },
  451. },
  452. }
  453. unboundPVCwithInvalidSCPod := &v1.Pod{
  454. Spec: v1.PodSpec{
  455. Volumes: []v1.Volume{
  456. {
  457. VolumeSource: v1.VolumeSource{
  458. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  459. ClaimName: "unboundPVCwithInvalidSCPod",
  460. },
  461. },
  462. },
  463. },
  464. },
  465. }
  466. unboundPVCwithDefaultSCPod := &v1.Pod{
  467. Spec: v1.PodSpec{
  468. Volumes: []v1.Volume{
  469. {
  470. VolumeSource: v1.VolumeSource{
  471. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  472. ClaimName: "unboundPVCwithDefaultSCPod",
  473. },
  474. },
  475. },
  476. },
  477. },
  478. }
  479. splitVolsPod := &v1.Pod{
  480. Spec: v1.PodSpec{
  481. Volumes: []v1.Volume{
  482. {
  483. VolumeSource: v1.VolumeSource{
  484. HostPath: &v1.HostPathVolumeSource{},
  485. },
  486. },
  487. {
  488. VolumeSource: v1.VolumeSource{
  489. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "svp"},
  490. },
  491. },
  492. },
  493. },
  494. }
  495. nonApplicablePod := &v1.Pod{
  496. Spec: v1.PodSpec{
  497. Volumes: []v1.Volume{
  498. {
  499. VolumeSource: v1.VolumeSource{
  500. HostPath: &v1.HostPathVolumeSource{},
  501. },
  502. },
  503. },
  504. },
  505. }
  506. deletedPVCPod := &v1.Pod{
  507. Spec: v1.PodSpec{
  508. Volumes: []v1.Volume{
  509. {
  510. VolumeSource: v1.VolumeSource{
  511. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  512. ClaimName: "deletedPVC",
  513. },
  514. },
  515. },
  516. },
  517. },
  518. }
  519. twoDeletedPVCPod := &v1.Pod{
  520. Spec: v1.PodSpec{
  521. Volumes: []v1.Volume{
  522. {
  523. VolumeSource: v1.VolumeSource{
  524. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  525. ClaimName: "deletedPVC",
  526. },
  527. },
  528. },
  529. {
  530. VolumeSource: v1.VolumeSource{
  531. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  532. ClaimName: "anotherDeletedPVC",
  533. },
  534. },
  535. },
  536. },
  537. },
  538. }
  539. deletedPVPod := &v1.Pod{
  540. Spec: v1.PodSpec{
  541. Volumes: []v1.Volume{
  542. {
  543. VolumeSource: v1.VolumeSource{
  544. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  545. ClaimName: "pvcWithDeletedPV",
  546. },
  547. },
  548. },
  549. },
  550. },
  551. }
  552. // deletedPVPod2 is a different pod than deletedPVPod but using the same PVC
  553. deletedPVPod2 := &v1.Pod{
  554. Spec: v1.PodSpec{
  555. Volumes: []v1.Volume{
  556. {
  557. VolumeSource: v1.VolumeSource{
  558. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  559. ClaimName: "pvcWithDeletedPV",
  560. },
  561. },
  562. },
  563. },
  564. },
  565. }
  566. // anotherDeletedPVPod is a different pod than deletedPVPod and uses another PVC
  567. anotherDeletedPVPod := &v1.Pod{
  568. Spec: v1.PodSpec{
  569. Volumes: []v1.Volume{
  570. {
  571. VolumeSource: v1.VolumeSource{
  572. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  573. ClaimName: "anotherPVCWithDeletedPV",
  574. },
  575. },
  576. },
  577. },
  578. },
  579. }
  580. emptyPod := &v1.Pod{
  581. Spec: v1.PodSpec{},
  582. }
  583. unboundPVCPod := &v1.Pod{
  584. Spec: v1.PodSpec{
  585. Volumes: []v1.Volume{
  586. {
  587. VolumeSource: v1.VolumeSource{
  588. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  589. ClaimName: "unboundPVC",
  590. },
  591. },
  592. },
  593. },
  594. },
  595. }
  596. // Different pod than unboundPVCPod, but using the same unbound PVC
  597. unboundPVCPod2 := &v1.Pod{
  598. Spec: v1.PodSpec{
  599. Volumes: []v1.Volume{
  600. {
  601. VolumeSource: v1.VolumeSource{
  602. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  603. ClaimName: "unboundPVC",
  604. },
  605. },
  606. },
  607. },
  608. },
  609. }
  610. // pod with unbound PVC that's different to unboundPVC
  611. anotherUnboundPVCPod := &v1.Pod{
  612. Spec: v1.PodSpec{
  613. Volumes: []v1.Volume{
  614. {
  615. VolumeSource: v1.VolumeSource{
  616. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  617. ClaimName: "anotherUnboundPVC",
  618. },
  619. },
  620. },
  621. },
  622. },
  623. }
  624. tests := []struct {
  625. newPod *v1.Pod
  626. existingPods []*v1.Pod
  627. filterName string
  628. driverName string
  629. maxVols int
  630. test string
  631. wantStatus *framework.Status
  632. }{
  633. {
  634. newPod: oneVolPod,
  635. existingPods: []*v1.Pod{twoVolPod, oneVolPod},
  636. filterName: ebsVolumeFilterType,
  637. driverName: csilibplugins.AWSEBSInTreePluginName,
  638. maxVols: 4,
  639. test: "fits when node capacity >= new pod's EBS volumes",
  640. },
  641. {
  642. newPod: twoVolPod,
  643. existingPods: []*v1.Pod{oneVolPod},
  644. filterName: ebsVolumeFilterType,
  645. driverName: csilibplugins.AWSEBSInTreePluginName,
  646. maxVols: 2,
  647. test: "doesn't fit when node capacity < new pod's EBS volumes",
  648. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  649. },
  650. {
  651. newPod: splitVolsPod,
  652. existingPods: []*v1.Pod{twoVolPod},
  653. filterName: ebsVolumeFilterType,
  654. driverName: csilibplugins.AWSEBSInTreePluginName,
  655. maxVols: 3,
  656. test: "new pod's count ignores non-EBS volumes",
  657. },
  658. {
  659. newPod: twoVolPod,
  660. existingPods: []*v1.Pod{splitVolsPod, nonApplicablePod, emptyPod},
  661. filterName: ebsVolumeFilterType,
  662. driverName: csilibplugins.AWSEBSInTreePluginName,
  663. maxVols: 3,
  664. test: "existing pods' counts ignore non-EBS volumes",
  665. },
  666. {
  667. newPod: onePVCPod(ebsVolumeFilterType),
  668. existingPods: []*v1.Pod{splitVolsPod, nonApplicablePod, emptyPod},
  669. filterName: ebsVolumeFilterType,
  670. driverName: csilibplugins.AWSEBSInTreePluginName,
  671. maxVols: 3,
  672. test: "new pod's count considers PVCs backed by EBS volumes",
  673. },
  674. {
  675. newPod: splitPVCPod(ebsVolumeFilterType),
  676. existingPods: []*v1.Pod{splitVolsPod, oneVolPod},
  677. filterName: ebsVolumeFilterType,
  678. driverName: csilibplugins.AWSEBSInTreePluginName,
  679. maxVols: 3,
  680. test: "new pod's count ignores PVCs not backed by EBS volumes",
  681. },
  682. {
  683. newPod: twoVolPod,
  684. existingPods: []*v1.Pod{oneVolPod, onePVCPod(ebsVolumeFilterType)},
  685. filterName: ebsVolumeFilterType,
  686. driverName: csilibplugins.AWSEBSInTreePluginName,
  687. maxVols: 3,
  688. test: "existing pods' counts considers PVCs backed by EBS volumes",
  689. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  690. },
  691. {
  692. newPod: twoVolPod,
  693. existingPods: []*v1.Pod{oneVolPod, twoVolPod, onePVCPod(ebsVolumeFilterType)},
  694. filterName: ebsVolumeFilterType,
  695. driverName: csilibplugins.AWSEBSInTreePluginName,
  696. maxVols: 4,
  697. test: "already-mounted EBS volumes are always ok to allow",
  698. },
  699. {
  700. newPod: splitVolsPod,
  701. existingPods: []*v1.Pod{oneVolPod, oneVolPod, onePVCPod(ebsVolumeFilterType)},
  702. filterName: ebsVolumeFilterType,
  703. driverName: csilibplugins.AWSEBSInTreePluginName,
  704. maxVols: 3,
  705. test: "the same EBS volumes are not counted multiple times",
  706. },
  707. {
  708. newPod: onePVCPod(ebsVolumeFilterType),
  709. existingPods: []*v1.Pod{oneVolPod, deletedPVCPod},
  710. filterName: ebsVolumeFilterType,
  711. driverName: csilibplugins.AWSEBSInTreePluginName,
  712. maxVols: 1,
  713. test: "missing PVC is not counted towards the PV limit",
  714. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  715. },
  716. {
  717. newPod: onePVCPod(ebsVolumeFilterType),
  718. existingPods: []*v1.Pod{oneVolPod, deletedPVCPod},
  719. filterName: ebsVolumeFilterType,
  720. driverName: csilibplugins.AWSEBSInTreePluginName,
  721. maxVols: 2,
  722. test: "missing PVC is not counted towards the PV limit",
  723. },
  724. {
  725. newPod: onePVCPod(ebsVolumeFilterType),
  726. existingPods: []*v1.Pod{oneVolPod, twoDeletedPVCPod},
  727. filterName: ebsVolumeFilterType,
  728. driverName: csilibplugins.AWSEBSInTreePluginName,
  729. maxVols: 2,
  730. test: "two missing PVCs are not counted towards the PV limit twice",
  731. },
  732. {
  733. newPod: unboundPVCwithInvalidSCPod,
  734. existingPods: []*v1.Pod{oneVolPod},
  735. filterName: ebsVolumeFilterType,
  736. driverName: csilibplugins.AWSEBSInTreePluginName,
  737. maxVols: 1,
  738. test: "unbound PVC with invalid SC is not counted towards the PV limit",
  739. },
  740. {
  741. newPod: unboundPVCwithDefaultSCPod,
  742. existingPods: []*v1.Pod{oneVolPod},
  743. filterName: ebsVolumeFilterType,
  744. driverName: csilibplugins.AWSEBSInTreePluginName,
  745. maxVols: 1,
  746. test: "unbound PVC from different provisioner is not counted towards the PV limit",
  747. },
  748. {
  749. newPod: onePVCPod(ebsVolumeFilterType),
  750. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  751. filterName: ebsVolumeFilterType,
  752. driverName: csilibplugins.AWSEBSInTreePluginName,
  753. maxVols: 2,
  754. test: "pod with missing PV is counted towards the PV limit",
  755. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  756. },
  757. {
  758. newPod: onePVCPod(ebsVolumeFilterType),
  759. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  760. filterName: ebsVolumeFilterType,
  761. driverName: csilibplugins.AWSEBSInTreePluginName,
  762. maxVols: 3,
  763. test: "pod with missing PV is counted towards the PV limit",
  764. },
  765. {
  766. newPod: deletedPVPod2,
  767. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  768. filterName: ebsVolumeFilterType,
  769. driverName: csilibplugins.AWSEBSInTreePluginName,
  770. maxVols: 2,
  771. test: "two pods missing the same PV are counted towards the PV limit only once",
  772. },
  773. {
  774. newPod: anotherDeletedPVPod,
  775. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  776. filterName: ebsVolumeFilterType,
  777. driverName: csilibplugins.AWSEBSInTreePluginName,
  778. maxVols: 2,
  779. test: "two pods missing different PVs are counted towards the PV limit twice",
  780. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  781. },
  782. {
  783. newPod: onePVCPod(ebsVolumeFilterType),
  784. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  785. filterName: ebsVolumeFilterType,
  786. driverName: csilibplugins.AWSEBSInTreePluginName,
  787. maxVols: 2,
  788. test: "pod with unbound PVC is counted towards the PV limit",
  789. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  790. },
  791. {
  792. newPod: onePVCPod(ebsVolumeFilterType),
  793. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  794. filterName: ebsVolumeFilterType,
  795. driverName: csilibplugins.AWSEBSInTreePluginName,
  796. maxVols: 3,
  797. test: "pod with unbound PVC is counted towards the PV limit",
  798. },
  799. {
  800. newPod: unboundPVCPod2,
  801. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  802. filterName: ebsVolumeFilterType,
  803. driverName: csilibplugins.AWSEBSInTreePluginName,
  804. maxVols: 2,
  805. test: "the same unbound PVC in multiple pods is counted towards the PV limit only once",
  806. },
  807. {
  808. newPod: anotherUnboundPVCPod,
  809. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  810. filterName: ebsVolumeFilterType,
  811. driverName: csilibplugins.AWSEBSInTreePluginName,
  812. maxVols: 2,
  813. test: "two different unbound PVCs are counted towards the PV limit as two volumes",
  814. wantStatus: framework.NewStatus(framework.Unschedulable, ErrReasonMaxVolumeCountExceeded),
  815. },
  816. }
  817. for _, test := range tests {
  818. t.Run(test.test, func(t *testing.T) {
  819. node, csiNode := getNodeWithPodAndVolumeLimits("node", test.existingPods, int64(test.maxVols), test.filterName)
  820. p := newNonCSILimits(test.filterName, getFakeCSINodeLister(csiNode), getFakeCSIStorageClassLister(test.filterName, test.driverName), getFakePVLister(test.filterName), getFakePVCLister(test.filterName)).(framework.FilterPlugin)
  821. gotStatus := p.Filter(context.Background(), nil, test.newPod, node)
  822. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  823. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  824. }
  825. })
  826. }
  827. }
  828. func TestGCEPDLimits(t *testing.T) {
  829. oneVolPod := &v1.Pod{
  830. Spec: v1.PodSpec{
  831. Volumes: []v1.Volume{
  832. {
  833. VolumeSource: v1.VolumeSource{
  834. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "ovp"},
  835. },
  836. },
  837. },
  838. },
  839. }
  840. twoVolPod := &v1.Pod{
  841. Spec: v1.PodSpec{
  842. Volumes: []v1.Volume{
  843. {
  844. VolumeSource: v1.VolumeSource{
  845. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "tvp1"},
  846. },
  847. },
  848. {
  849. VolumeSource: v1.VolumeSource{
  850. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "tvp2"},
  851. },
  852. },
  853. },
  854. },
  855. }
  856. splitVolsPod := &v1.Pod{
  857. Spec: v1.PodSpec{
  858. Volumes: []v1.Volume{
  859. {
  860. VolumeSource: v1.VolumeSource{
  861. HostPath: &v1.HostPathVolumeSource{},
  862. },
  863. },
  864. {
  865. VolumeSource: v1.VolumeSource{
  866. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: "svp"},
  867. },
  868. },
  869. },
  870. },
  871. }
  872. nonApplicablePod := &v1.Pod{
  873. Spec: v1.PodSpec{
  874. Volumes: []v1.Volume{
  875. {
  876. VolumeSource: v1.VolumeSource{
  877. HostPath: &v1.HostPathVolumeSource{},
  878. },
  879. },
  880. },
  881. },
  882. }
  883. deletedPVCPod := &v1.Pod{
  884. Spec: v1.PodSpec{
  885. Volumes: []v1.Volume{
  886. {
  887. VolumeSource: v1.VolumeSource{
  888. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  889. ClaimName: "deletedPVC",
  890. },
  891. },
  892. },
  893. },
  894. },
  895. }
  896. twoDeletedPVCPod := &v1.Pod{
  897. Spec: v1.PodSpec{
  898. Volumes: []v1.Volume{
  899. {
  900. VolumeSource: v1.VolumeSource{
  901. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  902. ClaimName: "deletedPVC",
  903. },
  904. },
  905. },
  906. {
  907. VolumeSource: v1.VolumeSource{
  908. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  909. ClaimName: "anotherDeletedPVC",
  910. },
  911. },
  912. },
  913. },
  914. },
  915. }
  916. deletedPVPod := &v1.Pod{
  917. Spec: v1.PodSpec{
  918. Volumes: []v1.Volume{
  919. {
  920. VolumeSource: v1.VolumeSource{
  921. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  922. ClaimName: "pvcWithDeletedPV",
  923. },
  924. },
  925. },
  926. },
  927. },
  928. }
  929. // deletedPVPod2 is a different pod than deletedPVPod but using the same PVC
  930. deletedPVPod2 := &v1.Pod{
  931. Spec: v1.PodSpec{
  932. Volumes: []v1.Volume{
  933. {
  934. VolumeSource: v1.VolumeSource{
  935. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  936. ClaimName: "pvcWithDeletedPV",
  937. },
  938. },
  939. },
  940. },
  941. },
  942. }
  943. // anotherDeletedPVPod is a different pod than deletedPVPod and uses another PVC
  944. anotherDeletedPVPod := &v1.Pod{
  945. Spec: v1.PodSpec{
  946. Volumes: []v1.Volume{
  947. {
  948. VolumeSource: v1.VolumeSource{
  949. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  950. ClaimName: "anotherPVCWithDeletedPV",
  951. },
  952. },
  953. },
  954. },
  955. },
  956. }
  957. emptyPod := &v1.Pod{
  958. Spec: v1.PodSpec{},
  959. }
  960. unboundPVCPod := &v1.Pod{
  961. Spec: v1.PodSpec{
  962. Volumes: []v1.Volume{
  963. {
  964. VolumeSource: v1.VolumeSource{
  965. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  966. ClaimName: "unboundPVC",
  967. },
  968. },
  969. },
  970. },
  971. },
  972. }
  973. // Different pod than unboundPVCPod, but using the same unbound PVC
  974. unboundPVCPod2 := &v1.Pod{
  975. Spec: v1.PodSpec{
  976. Volumes: []v1.Volume{
  977. {
  978. VolumeSource: v1.VolumeSource{
  979. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  980. ClaimName: "unboundPVC",
  981. },
  982. },
  983. },
  984. },
  985. },
  986. }
  987. // pod with unbound PVC that's different to unboundPVC
  988. anotherUnboundPVCPod := &v1.Pod{
  989. Spec: v1.PodSpec{
  990. Volumes: []v1.Volume{
  991. {
  992. VolumeSource: v1.VolumeSource{
  993. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  994. ClaimName: "anotherUnboundPVC",
  995. },
  996. },
  997. },
  998. },
  999. },
  1000. }
  1001. tests := []struct {
  1002. newPod *v1.Pod
  1003. existingPods []*v1.Pod
  1004. filterName string
  1005. driverName string
  1006. maxVols int
  1007. test string
  1008. wantStatus *framework.Status
  1009. }{
  1010. {
  1011. newPod: oneVolPod,
  1012. existingPods: []*v1.Pod{twoVolPod, oneVolPod},
  1013. filterName: gcePDVolumeFilterType,
  1014. maxVols: 4,
  1015. test: "fits when node capacity >= new pod's GCE volumes",
  1016. },
  1017. {
  1018. newPod: twoVolPod,
  1019. existingPods: []*v1.Pod{oneVolPod},
  1020. filterName: gcePDVolumeFilterType,
  1021. maxVols: 2,
  1022. test: "fit when node capacity < new pod's GCE volumes",
  1023. },
  1024. {
  1025. newPod: splitVolsPod,
  1026. existingPods: []*v1.Pod{twoVolPod},
  1027. filterName: gcePDVolumeFilterType,
  1028. maxVols: 3,
  1029. test: "new pod's count ignores non-GCE volumes",
  1030. },
  1031. {
  1032. newPod: twoVolPod,
  1033. existingPods: []*v1.Pod{splitVolsPod, nonApplicablePod, emptyPod},
  1034. filterName: gcePDVolumeFilterType,
  1035. maxVols: 3,
  1036. test: "existing pods' counts ignore non-GCE volumes",
  1037. },
  1038. {
  1039. newPod: onePVCPod(gcePDVolumeFilterType),
  1040. existingPods: []*v1.Pod{splitVolsPod, nonApplicablePod, emptyPod},
  1041. filterName: gcePDVolumeFilterType,
  1042. maxVols: 3,
  1043. test: "new pod's count considers PVCs backed by GCE volumes",
  1044. },
  1045. {
  1046. newPod: splitPVCPod(gcePDVolumeFilterType),
  1047. existingPods: []*v1.Pod{splitVolsPod, oneVolPod},
  1048. filterName: gcePDVolumeFilterType,
  1049. maxVols: 3,
  1050. test: "new pod's count ignores PVCs not backed by GCE volumes",
  1051. },
  1052. {
  1053. newPod: twoVolPod,
  1054. existingPods: []*v1.Pod{oneVolPod, onePVCPod(gcePDVolumeFilterType)},
  1055. filterName: gcePDVolumeFilterType,
  1056. maxVols: 3,
  1057. test: "existing pods' counts considers PVCs backed by GCE volumes",
  1058. },
  1059. {
  1060. newPod: twoVolPod,
  1061. existingPods: []*v1.Pod{oneVolPod, twoVolPod, onePVCPod(gcePDVolumeFilterType)},
  1062. filterName: gcePDVolumeFilterType,
  1063. maxVols: 4,
  1064. test: "already-mounted EBS volumes are always ok to allow",
  1065. },
  1066. {
  1067. newPod: splitVolsPod,
  1068. existingPods: []*v1.Pod{oneVolPod, oneVolPod, onePVCPod(gcePDVolumeFilterType)},
  1069. filterName: gcePDVolumeFilterType,
  1070. maxVols: 3,
  1071. test: "the same GCE volumes are not counted multiple times",
  1072. },
  1073. {
  1074. newPod: onePVCPod(gcePDVolumeFilterType),
  1075. existingPods: []*v1.Pod{oneVolPod, deletedPVCPod},
  1076. filterName: gcePDVolumeFilterType,
  1077. maxVols: 2,
  1078. test: "pod with missing PVC is counted towards the PV limit",
  1079. },
  1080. {
  1081. newPod: onePVCPod(gcePDVolumeFilterType),
  1082. existingPods: []*v1.Pod{oneVolPod, deletedPVCPod},
  1083. filterName: gcePDVolumeFilterType,
  1084. maxVols: 3,
  1085. test: "pod with missing PVC is counted towards the PV limit",
  1086. },
  1087. {
  1088. newPod: onePVCPod(gcePDVolumeFilterType),
  1089. existingPods: []*v1.Pod{oneVolPod, twoDeletedPVCPod},
  1090. filterName: gcePDVolumeFilterType,
  1091. maxVols: 3,
  1092. test: "pod with missing two PVCs is counted towards the PV limit twice",
  1093. },
  1094. {
  1095. newPod: onePVCPod(gcePDVolumeFilterType),
  1096. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  1097. filterName: gcePDVolumeFilterType,
  1098. maxVols: 2,
  1099. test: "pod with missing PV is counted towards the PV limit",
  1100. },
  1101. {
  1102. newPod: onePVCPod(gcePDVolumeFilterType),
  1103. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  1104. filterName: gcePDVolumeFilterType,
  1105. maxVols: 3,
  1106. test: "pod with missing PV is counted towards the PV limit",
  1107. },
  1108. {
  1109. newPod: deletedPVPod2,
  1110. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  1111. filterName: gcePDVolumeFilterType,
  1112. maxVols: 2,
  1113. test: "two pods missing the same PV are counted towards the PV limit only once",
  1114. },
  1115. {
  1116. newPod: anotherDeletedPVPod,
  1117. existingPods: []*v1.Pod{oneVolPod, deletedPVPod},
  1118. filterName: gcePDVolumeFilterType,
  1119. maxVols: 2,
  1120. test: "two pods missing different PVs are counted towards the PV limit twice",
  1121. },
  1122. {
  1123. newPod: onePVCPod(gcePDVolumeFilterType),
  1124. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  1125. filterName: gcePDVolumeFilterType,
  1126. maxVols: 2,
  1127. test: "pod with unbound PVC is counted towards the PV limit",
  1128. },
  1129. {
  1130. newPod: onePVCPod(gcePDVolumeFilterType),
  1131. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  1132. filterName: gcePDVolumeFilterType,
  1133. maxVols: 3,
  1134. test: "pod with unbound PVC is counted towards the PV limit",
  1135. },
  1136. {
  1137. newPod: unboundPVCPod2,
  1138. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  1139. filterName: gcePDVolumeFilterType,
  1140. maxVols: 2,
  1141. test: "the same unbound PVC in multiple pods is counted towards the PV limit only once",
  1142. },
  1143. {
  1144. newPod: anotherUnboundPVCPod,
  1145. existingPods: []*v1.Pod{oneVolPod, unboundPVCPod},
  1146. filterName: gcePDVolumeFilterType,
  1147. maxVols: 2,
  1148. test: "two different unbound PVCs are counted towards the PV limit as two volumes",
  1149. },
  1150. }
  1151. for _, test := range tests {
  1152. t.Run(test.test, func(t *testing.T) {
  1153. node, csiNode := getNodeWithPodAndVolumeLimits("node", test.existingPods, int64(test.maxVols), test.filterName)
  1154. p := newNonCSILimits(test.filterName, getFakeCSINodeLister(csiNode), getFakeCSIStorageClassLister(test.filterName, test.driverName), getFakePVLister(test.filterName), getFakePVCLister(test.filterName)).(framework.FilterPlugin)
  1155. gotStatus := p.Filter(context.Background(), nil, test.newPod, node)
  1156. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  1157. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  1158. }
  1159. })
  1160. }
  1161. }
  1162. func TestGetMaxVols(t *testing.T) {
  1163. previousValue := os.Getenv(KubeMaxPDVols)
  1164. tests := []struct {
  1165. rawMaxVols string
  1166. expected int
  1167. name string
  1168. }{
  1169. {
  1170. rawMaxVols: "invalid",
  1171. expected: -1,
  1172. name: "Unable to parse maximum PD volumes value, using default value",
  1173. },
  1174. {
  1175. rawMaxVols: "-2",
  1176. expected: -1,
  1177. name: "Maximum PD volumes must be a positive value, using default value",
  1178. },
  1179. {
  1180. rawMaxVols: "40",
  1181. expected: 40,
  1182. name: "Parse maximum PD volumes value from env",
  1183. },
  1184. }
  1185. for _, test := range tests {
  1186. t.Run(test.name, func(t *testing.T) {
  1187. os.Setenv(KubeMaxPDVols, test.rawMaxVols)
  1188. result := getMaxVolLimitFromEnv()
  1189. if result != test.expected {
  1190. t.Errorf("expected %v got %v", test.expected, result)
  1191. }
  1192. })
  1193. }
  1194. os.Unsetenv(KubeMaxPDVols)
  1195. if previousValue != "" {
  1196. os.Setenv(KubeMaxPDVols, previousValue)
  1197. }
  1198. }
  1199. func getFakePVCLister(filterName string) fakelisters.PersistentVolumeClaimLister {
  1200. return fakelisters.PersistentVolumeClaimLister{
  1201. {
  1202. ObjectMeta: metav1.ObjectMeta{Name: "some" + filterName + "Vol"},
  1203. Spec: v1.PersistentVolumeClaimSpec{
  1204. VolumeName: "some" + filterName + "Vol",
  1205. StorageClassName: &filterName,
  1206. },
  1207. },
  1208. {
  1209. ObjectMeta: metav1.ObjectMeta{Name: "someNon" + filterName + "Vol"},
  1210. Spec: v1.PersistentVolumeClaimSpec{
  1211. VolumeName: "someNon" + filterName + "Vol",
  1212. StorageClassName: &filterName,
  1213. },
  1214. },
  1215. {
  1216. ObjectMeta: metav1.ObjectMeta{Name: "pvcWithDeletedPV"},
  1217. Spec: v1.PersistentVolumeClaimSpec{
  1218. VolumeName: "pvcWithDeletedPV",
  1219. StorageClassName: &filterName,
  1220. },
  1221. },
  1222. {
  1223. ObjectMeta: metav1.ObjectMeta{Name: "anotherPVCWithDeletedPV"},
  1224. Spec: v1.PersistentVolumeClaimSpec{
  1225. VolumeName: "anotherPVCWithDeletedPV",
  1226. StorageClassName: &filterName,
  1227. },
  1228. },
  1229. {
  1230. ObjectMeta: metav1.ObjectMeta{Name: "unboundPVC"},
  1231. Spec: v1.PersistentVolumeClaimSpec{
  1232. VolumeName: "",
  1233. StorageClassName: &filterName,
  1234. },
  1235. },
  1236. {
  1237. ObjectMeta: metav1.ObjectMeta{Name: "anotherUnboundPVC"},
  1238. Spec: v1.PersistentVolumeClaimSpec{
  1239. VolumeName: "",
  1240. StorageClassName: &filterName,
  1241. },
  1242. },
  1243. {
  1244. ObjectMeta: metav1.ObjectMeta{Name: "unboundPVCwithDefaultSCPod"},
  1245. Spec: v1.PersistentVolumeClaimSpec{
  1246. VolumeName: "",
  1247. StorageClassName: utilpointer.StringPtr("standard-sc"),
  1248. },
  1249. },
  1250. {
  1251. ObjectMeta: metav1.ObjectMeta{Name: "unboundPVCwithInvalidSCPod"},
  1252. Spec: v1.PersistentVolumeClaimSpec{
  1253. VolumeName: "",
  1254. StorageClassName: utilpointer.StringPtr("invalid-sc"),
  1255. },
  1256. },
  1257. }
  1258. }
  1259. func getFakePVLister(filterName string) fakelisters.PersistentVolumeLister {
  1260. return fakelisters.PersistentVolumeLister{
  1261. {
  1262. ObjectMeta: metav1.ObjectMeta{Name: "some" + filterName + "Vol"},
  1263. Spec: v1.PersistentVolumeSpec{
  1264. PersistentVolumeSource: v1.PersistentVolumeSource{
  1265. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: strings.ToLower(filterName) + "Vol"},
  1266. },
  1267. },
  1268. },
  1269. {
  1270. ObjectMeta: metav1.ObjectMeta{Name: "someNon" + filterName + "Vol"},
  1271. Spec: v1.PersistentVolumeSpec{
  1272. PersistentVolumeSource: v1.PersistentVolumeSource{},
  1273. },
  1274. },
  1275. }
  1276. }
  1277. func onePVCPod(filterName string) *v1.Pod {
  1278. return &v1.Pod{
  1279. Spec: v1.PodSpec{
  1280. Volumes: []v1.Volume{
  1281. {
  1282. VolumeSource: v1.VolumeSource{
  1283. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  1284. ClaimName: "some" + filterName + "Vol",
  1285. },
  1286. },
  1287. },
  1288. },
  1289. },
  1290. }
  1291. }
  1292. func splitPVCPod(filterName string) *v1.Pod {
  1293. return &v1.Pod{
  1294. Spec: v1.PodSpec{
  1295. Volumes: []v1.Volume{
  1296. {
  1297. VolumeSource: v1.VolumeSource{
  1298. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  1299. ClaimName: "someNon" + filterName + "Vol",
  1300. },
  1301. },
  1302. },
  1303. {
  1304. VolumeSource: v1.VolumeSource{
  1305. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  1306. ClaimName: "some" + filterName + "Vol",
  1307. },
  1308. },
  1309. },
  1310. },
  1311. },
  1312. }
  1313. }