provision_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. Copyright 2016 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 persistentvolume
  14. import (
  15. "errors"
  16. "testing"
  17. v1 "k8s.io/api/core/v1"
  18. storage "k8s.io/api/storage/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. corelisters "k8s.io/client-go/listers/core/v1"
  22. "k8s.io/client-go/tools/cache"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. pvtesting "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/testing"
  25. pvutil "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/util"
  26. )
  27. var class1Parameters = map[string]string{
  28. "param1": "value1",
  29. }
  30. var class2Parameters = map[string]string{
  31. "param2": "value2",
  32. }
  33. var deleteReclaimPolicy = v1.PersistentVolumeReclaimDelete
  34. var modeImmediate = storage.VolumeBindingImmediate
  35. var storageClasses = []*storage.StorageClass{
  36. {
  37. TypeMeta: metav1.TypeMeta{
  38. Kind: "StorageClass",
  39. },
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "gold",
  42. },
  43. Provisioner: mockPluginName,
  44. Parameters: class1Parameters,
  45. ReclaimPolicy: &deleteReclaimPolicy,
  46. VolumeBindingMode: &modeImmediate,
  47. },
  48. {
  49. TypeMeta: metav1.TypeMeta{
  50. Kind: "StorageClass",
  51. },
  52. ObjectMeta: metav1.ObjectMeta{
  53. Name: "silver",
  54. },
  55. Provisioner: mockPluginName,
  56. Parameters: class2Parameters,
  57. ReclaimPolicy: &deleteReclaimPolicy,
  58. VolumeBindingMode: &modeImmediate,
  59. },
  60. {
  61. TypeMeta: metav1.TypeMeta{
  62. Kind: "StorageClass",
  63. },
  64. ObjectMeta: metav1.ObjectMeta{
  65. Name: "copper",
  66. },
  67. Provisioner: mockPluginName,
  68. Parameters: class1Parameters,
  69. ReclaimPolicy: &deleteReclaimPolicy,
  70. VolumeBindingMode: &modeWait,
  71. },
  72. {
  73. TypeMeta: metav1.TypeMeta{
  74. Kind: "StorageClass",
  75. },
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: "external",
  78. },
  79. Provisioner: "vendor.com/my-volume",
  80. Parameters: class1Parameters,
  81. ReclaimPolicy: &deleteReclaimPolicy,
  82. VolumeBindingMode: &modeImmediate,
  83. },
  84. {
  85. TypeMeta: metav1.TypeMeta{
  86. Kind: "StorageClass",
  87. },
  88. ObjectMeta: metav1.ObjectMeta{
  89. Name: "external-wait",
  90. },
  91. Provisioner: "vendor.com/my-volume-wait",
  92. Parameters: class1Parameters,
  93. ReclaimPolicy: &deleteReclaimPolicy,
  94. VolumeBindingMode: &modeWait,
  95. },
  96. {
  97. TypeMeta: metav1.TypeMeta{
  98. Kind: "StorageClass",
  99. },
  100. ObjectMeta: metav1.ObjectMeta{
  101. Name: "unknown-internal",
  102. },
  103. Provisioner: "kubernetes.io/unknown",
  104. Parameters: class1Parameters,
  105. ReclaimPolicy: &deleteReclaimPolicy,
  106. VolumeBindingMode: &modeImmediate,
  107. },
  108. {
  109. TypeMeta: metav1.TypeMeta{
  110. Kind: "StorageClass",
  111. },
  112. ObjectMeta: metav1.ObjectMeta{
  113. Name: "unsupported-mountoptions",
  114. },
  115. Provisioner: mockPluginName,
  116. Parameters: class1Parameters,
  117. ReclaimPolicy: &deleteReclaimPolicy,
  118. MountOptions: []string{"foo"},
  119. VolumeBindingMode: &modeImmediate,
  120. },
  121. }
  122. // call to storageClass 1, returning an error
  123. var provision1Error = provisionCall{
  124. ret: errors.New("Mock provisioner error"),
  125. expectedParameters: class1Parameters,
  126. }
  127. // call to storageClass 1, returning a valid PV
  128. var provision1Success = provisionCall{
  129. ret: nil,
  130. expectedParameters: class1Parameters,
  131. }
  132. // call to storageClass 2, returning a valid PV
  133. var provision2Success = provisionCall{
  134. ret: nil,
  135. expectedParameters: class2Parameters,
  136. }
  137. // Test single call to syncVolume, expecting provisioning to happen.
  138. // 1. Fill in the controller with initial data
  139. // 2. Call the syncVolume *once*.
  140. // 3. Compare resulting volumes with expected volumes.
  141. func TestProvisionSync(t *testing.T) {
  142. tests := []controllerTest{
  143. {
  144. // Provision a volume (with a default class)
  145. "11-1 - successful provision with storage class 1",
  146. novolumes,
  147. newVolumeArray("pvc-uid11-1", "1Gi", "uid11-1", "claim11-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned),
  148. newClaimArray("claim11-1", "uid11-1", "1Gi", "", v1.ClaimPending, &classGold),
  149. // Binding will be completed in the next syncClaim
  150. newClaimArray("claim11-1", "uid11-1", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  151. []string{"Normal ProvisioningSucceeded"}, noerrors, wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  152. },
  153. {
  154. // Provision failure - plugin not found
  155. "11-2 - plugin not found",
  156. novolumes,
  157. novolumes,
  158. newClaimArray("claim11-2", "uid11-2", "1Gi", "", v1.ClaimPending, &classGold),
  159. newClaimArray("claim11-2", "uid11-2", "1Gi", "", v1.ClaimPending, &classGold),
  160. []string{"Warning ProvisioningFailed"}, noerrors,
  161. testSyncClaim,
  162. },
  163. {
  164. // Provision failure - newProvisioner returns error
  165. "11-3 - newProvisioner failure",
  166. novolumes,
  167. novolumes,
  168. newClaimArray("claim11-3", "uid11-3", "1Gi", "", v1.ClaimPending, &classGold),
  169. newClaimArray("claim11-3", "uid11-3", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  170. []string{"Warning ProvisioningFailed"}, noerrors,
  171. wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  172. },
  173. {
  174. // Provision failure - Provision returns error
  175. "11-4 - provision failure",
  176. novolumes,
  177. novolumes,
  178. newClaimArray("claim11-4", "uid11-4", "1Gi", "", v1.ClaimPending, &classGold),
  179. newClaimArray("claim11-4", "uid11-4", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  180. []string{"Warning ProvisioningFailed"}, noerrors,
  181. wrapTestWithProvisionCalls([]provisionCall{provision1Error}, testSyncClaim),
  182. },
  183. {
  184. // No provisioning if there is a matching volume available
  185. "11-6 - provisioning when there is a volume available",
  186. newVolumeArray("volume11-6", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classGold),
  187. newVolumeArray("volume11-6", "1Gi", "uid11-6", "claim11-6", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classGold, pvutil.AnnBoundByController),
  188. newClaimArray("claim11-6", "uid11-6", "1Gi", "", v1.ClaimPending, &classGold),
  189. newClaimArray("claim11-6", "uid11-6", "1Gi", "volume11-6", v1.ClaimBound, &classGold, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  190. noevents, noerrors,
  191. // No provisioning plugin confingure - makes the test fail when
  192. // the controller erroneously tries to provision something
  193. wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  194. },
  195. {
  196. // Provision success? - claim is bound before provisioner creates
  197. // a volume.
  198. "11-7 - claim is bound before provisioning",
  199. novolumes,
  200. newVolumeArray("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned),
  201. newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold),
  202. // The claim would be bound in next syncClaim
  203. newClaimArray("claim11-7", "uid11-7", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  204. noevents, noerrors,
  205. wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
  206. // Create a volume before provisionClaimOperation starts.
  207. // This similates a parallel controller provisioning the volume.
  208. volume := newVolume("pvc-uid11-7", "1Gi", "uid11-7", "claim11-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned)
  209. reactor.AddVolume(volume)
  210. }),
  211. },
  212. {
  213. // Provision success - cannot save provisioned PV once,
  214. // second retry succeeds
  215. "11-8 - cannot save provisioned volume",
  216. novolumes,
  217. newVolumeArray("pvc-uid11-8", "1Gi", "uid11-8", "claim11-8", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned),
  218. newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold),
  219. // Binding will be completed in the next syncClaim
  220. newClaimArray("claim11-8", "uid11-8", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  221. []string{"Normal ProvisioningSucceeded"},
  222. []pvtesting.ReactorError{
  223. // Inject error to the first
  224. // kubeclient.PersistentVolumes.Create() call. All other calls
  225. // will succeed.
  226. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error")},
  227. },
  228. wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  229. },
  230. {
  231. // Provision success? - cannot save provisioned PV five times,
  232. // volume is deleted and delete succeeds
  233. "11-9 - cannot save provisioned volume, delete succeeds",
  234. novolumes,
  235. novolumes,
  236. newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold),
  237. newClaimArray("claim11-9", "uid11-9", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  238. []string{"Warning ProvisioningFailed"},
  239. []pvtesting.ReactorError{
  240. // Inject error to five kubeclient.PersistentVolumes.Create()
  241. // calls
  242. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
  243. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error2")},
  244. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error3")},
  245. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
  246. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
  247. },
  248. wrapTestWithPluginCalls(
  249. nil, // recycle calls
  250. []error{nil}, // delete calls
  251. []provisionCall{provision1Success}, // provision calls
  252. testSyncClaim,
  253. ),
  254. },
  255. {
  256. // Provision failure - cannot save provisioned PV five times,
  257. // volume delete failed - no plugin found
  258. "11-10 - cannot save provisioned volume, no delete plugin found",
  259. novolumes,
  260. novolumes,
  261. newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold),
  262. newClaimArray("claim11-10", "uid11-10", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  263. []string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"},
  264. []pvtesting.ReactorError{
  265. // Inject error to five kubeclient.PersistentVolumes.Create()
  266. // calls
  267. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
  268. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error2")},
  269. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error3")},
  270. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
  271. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
  272. },
  273. // No deleteCalls are configured, which results into no deleter plugin available for the volume
  274. wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  275. },
  276. {
  277. // Provision failure - cannot save provisioned PV five times,
  278. // volume delete failed - deleter returns error five times
  279. "11-11 - cannot save provisioned volume, deleter fails",
  280. novolumes,
  281. novolumes,
  282. newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold),
  283. newClaimArray("claim11-11", "uid11-11", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  284. []string{"Warning ProvisioningFailed", "Warning ProvisioningCleanupFailed"},
  285. []pvtesting.ReactorError{
  286. // Inject error to five kubeclient.PersistentVolumes.Create()
  287. // calls
  288. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
  289. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error2")},
  290. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error3")},
  291. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
  292. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
  293. },
  294. wrapTestWithPluginCalls(
  295. nil, // recycle calls
  296. []error{ // delete calls
  297. errors.New("Mock deletion error1"),
  298. errors.New("Mock deletion error2"),
  299. errors.New("Mock deletion error3"),
  300. errors.New("Mock deletion error4"),
  301. errors.New("Mock deletion error5"),
  302. },
  303. []provisionCall{provision1Success}, // provision calls
  304. testSyncClaim),
  305. },
  306. {
  307. // Provision failure - cannot save provisioned PV five times,
  308. // volume delete succeeds 2nd time
  309. "11-12 - cannot save provisioned volume, delete succeeds 2nd time",
  310. novolumes,
  311. novolumes,
  312. newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold),
  313. newClaimArray("claim11-12", "uid11-12", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  314. []string{"Warning ProvisioningFailed"},
  315. []pvtesting.ReactorError{
  316. // Inject error to five kubeclient.PersistentVolumes.Create()
  317. // calls
  318. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
  319. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error2")},
  320. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error3")},
  321. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error4")},
  322. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error5")},
  323. },
  324. wrapTestWithPluginCalls(
  325. nil, // recycle calls
  326. []error{ // delete calls
  327. errors.New("Mock deletion error1"),
  328. nil,
  329. }, // provison calls
  330. []provisionCall{provision1Success},
  331. testSyncClaim,
  332. ),
  333. },
  334. {
  335. // Provision a volume (with non-default class)
  336. "11-13 - successful provision with storage class 2",
  337. novolumes,
  338. newVolumeArray("pvc-uid11-13", "1Gi", "uid11-13", "claim11-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classSilver, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned),
  339. newClaimArray("claim11-13", "uid11-13", "1Gi", "", v1.ClaimPending, &classSilver),
  340. // Binding will be completed in the next syncClaim
  341. newClaimArray("claim11-13", "uid11-13", "1Gi", "", v1.ClaimPending, &classSilver, pvutil.AnnStorageProvisioner),
  342. []string{"Normal ProvisioningSucceeded"}, noerrors, wrapTestWithProvisionCalls([]provisionCall{provision2Success}, testSyncClaim),
  343. },
  344. {
  345. // Provision error - non existing class
  346. "11-14 - fail due to non-existing class",
  347. novolumes,
  348. novolumes,
  349. newClaimArray("claim11-14", "uid11-14", "1Gi", "", v1.ClaimPending, &classNonExisting),
  350. newClaimArray("claim11-14", "uid11-14", "1Gi", "", v1.ClaimPending, &classNonExisting),
  351. noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  352. },
  353. {
  354. // No provisioning with class=""
  355. "11-15 - no provisioning with class=''",
  356. novolumes,
  357. novolumes,
  358. newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, &classEmpty),
  359. newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, &classEmpty),
  360. noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  361. },
  362. {
  363. // No provisioning with class=nil
  364. "11-16 - no provisioning with class=nil",
  365. novolumes,
  366. novolumes,
  367. newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, nil),
  368. newClaimArray("claim11-15", "uid11-15", "1Gi", "", v1.ClaimPending, nil),
  369. noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  370. },
  371. {
  372. // No provisioning + normal event with external provisioner
  373. "11-17 - external provisioner",
  374. novolumes,
  375. novolumes,
  376. newClaimArray("claim11-17", "uid11-17", "1Gi", "", v1.ClaimPending, &classExternal),
  377. claimWithAnnotation(pvutil.AnnStorageProvisioner, "vendor.com/my-volume",
  378. newClaimArray("claim11-17", "uid11-17", "1Gi", "", v1.ClaimPending, &classExternal)),
  379. []string{"Normal ExternalProvisioning"},
  380. noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  381. },
  382. {
  383. // No provisioning + warning event with unknown internal provisioner
  384. "11-18 - unknown internal provisioner",
  385. novolumes,
  386. novolumes,
  387. newClaimArray("claim11-18", "uid11-18", "1Gi", "", v1.ClaimPending, &classUnknownInternal),
  388. newClaimArray("claim11-18", "uid11-18", "1Gi", "", v1.ClaimPending, &classUnknownInternal),
  389. []string{"Warning ProvisioningFailed"},
  390. noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  391. },
  392. {
  393. // Provision success - first save of a PV to API server fails (API
  394. // server has written the object to etcd, but crashed before sending
  395. // 200 OK response to the controller). Controller retries and the
  396. // second save of the PV returns "AlreadyExists" because the PV
  397. // object already is in the API server.
  398. //
  399. "11-19 - provisioned volume saved but API server crashed",
  400. novolumes,
  401. // We don't actually simulate API server saving the object and
  402. // crashing afterwards, Create() just returns error without saving
  403. // the volume in this test. So the set of expected volumes at the
  404. // end of the test is empty.
  405. novolumes,
  406. newClaimArray("claim11-19", "uid11-19", "1Gi", "", v1.ClaimPending, &classGold),
  407. newClaimArray("claim11-19", "uid11-19", "1Gi", "", v1.ClaimPending, &classGold, pvutil.AnnStorageProvisioner),
  408. noevents,
  409. []pvtesting.ReactorError{
  410. // Inject errors to simulate crashed API server during
  411. // kubeclient.PersistentVolumes.Create()
  412. {Verb: "create", Resource: "persistentvolumes", Error: errors.New("Mock creation error1")},
  413. {Verb: "create", Resource: "persistentvolumes", Error: apierrors.NewAlreadyExists(api.Resource("persistentvolumes"), "")},
  414. },
  415. wrapTestWithPluginCalls(
  416. nil, // recycle calls
  417. nil, // delete calls - if Delete was called the test would fail
  418. []provisionCall{provision1Success},
  419. testSyncClaim,
  420. ),
  421. },
  422. {
  423. // No provisioning + warning event with unsupported storageClass.mountOptions
  424. "11-20 - unsupported storageClass.mountOptions",
  425. novolumes,
  426. novolumes,
  427. newClaimArray("claim11-20", "uid11-20", "1Gi", "", v1.ClaimPending, &classUnsupportedMountOptions),
  428. newClaimArray("claim11-20", "uid11-20", "1Gi", "", v1.ClaimPending, &classUnsupportedMountOptions, pvutil.AnnStorageProvisioner),
  429. // Expect event to be prefixed with "Mount options" because saving PV will fail anyway
  430. []string{"Warning ProvisioningFailed Mount options"},
  431. noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  432. },
  433. {
  434. // No provisioning due to CSI migration + normal event with external provisioner
  435. "11-21 - external provisioner for CSI migration",
  436. novolumes,
  437. novolumes,
  438. newClaimArray("claim11-21", "uid11-21", "1Gi", "", v1.ClaimPending, &classGold),
  439. []*v1.PersistentVolumeClaim{
  440. annotateClaim(
  441. newClaim("claim11-21", "uid11-21", "1Gi", "", v1.ClaimPending, &classGold),
  442. map[string]string{
  443. pvutil.AnnStorageProvisioner: "vendor.com/MockCSIDriver",
  444. pvutil.AnnMigratedTo: "vendor.com/MockCSIDriver",
  445. }),
  446. },
  447. []string{"Normal ExternalProvisioning"},
  448. noerrors, wrapTestWithCSIMigrationProvisionCalls(testSyncClaim),
  449. },
  450. {
  451. // volume provisioned and available
  452. // in this case, NO normal event with external provisioner should be issued
  453. "11-22 - external provisioner with volume available",
  454. newVolumeArray("volume11-22", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classExternal),
  455. newVolumeArray("volume11-22", "1Gi", "uid11-22", "claim11-22", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, pvutil.AnnBoundByController),
  456. newClaimArray("claim11-22", "uid11-22", "1Gi", "", v1.ClaimPending, &classExternal),
  457. newClaimArray("claim11-22", "uid11-22", "1Gi", "volume11-22", v1.ClaimBound, &classExternal, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  458. noevents,
  459. noerrors, wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim),
  460. },
  461. {
  462. // volume provision for PVC scheduled
  463. "11-23 - skip finding PV and provision for PVC annotated with AnnSelectedNode",
  464. newVolumeArray("volume11-23", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classCopper),
  465. []*v1.PersistentVolume{
  466. newVolume("volume11-23", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classCopper),
  467. newVolume("pvc-uid11-23", "1Gi", "uid11-23", "claim11-23", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classCopper, pvutil.AnnDynamicallyProvisioned, pvutil.AnnBoundByController),
  468. },
  469. claimWithAnnotation(pvutil.AnnSelectedNode, "node1",
  470. newClaimArray("claim11-23", "uid11-23", "1Gi", "", v1.ClaimPending, &classCopper)),
  471. claimWithAnnotation(pvutil.AnnSelectedNode, "node1",
  472. newClaimArray("claim11-23", "uid11-23", "1Gi", "", v1.ClaimPending, &classCopper, pvutil.AnnStorageProvisioner)),
  473. []string{"Normal ProvisioningSucceeded"},
  474. noerrors,
  475. wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  476. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
  477. nodesIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
  478. node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node1"}}
  479. nodesIndexer.Add(node)
  480. ctrl.NodeLister = corelisters.NewNodeLister(nodesIndexer)
  481. }),
  482. },
  483. {
  484. // volume provision for PVC that scheduled
  485. "11-24 - skip finding PV and wait external provisioner for PVC annotated with AnnSelectedNode",
  486. newVolumeArray("volume11-24", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classExternalWait),
  487. newVolumeArray("volume11-24", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classExternalWait),
  488. claimWithAnnotation(pvutil.AnnSelectedNode, "node1",
  489. newClaimArray("claim11-24", "uid11-24", "1Gi", "", v1.ClaimPending, &classExternalWait)),
  490. claimWithAnnotation(pvutil.AnnStorageProvisioner, "vendor.com/my-volume-wait",
  491. claimWithAnnotation(pvutil.AnnSelectedNode, "node1",
  492. newClaimArray("claim11-24", "uid11-24", "1Gi", "", v1.ClaimPending, &classExternalWait))),
  493. []string{"Normal ExternalProvisioning"},
  494. noerrors, testSyncClaim,
  495. },
  496. }
  497. runSyncTests(t, tests, storageClasses, []*v1.Pod{})
  498. }
  499. // Test multiple calls to syncClaim/syncVolume and periodic sync of all
  500. // volume/claims. The test follows this pattern:
  501. // 0. Load the controller with initial data.
  502. // 1. Call controllerTest.testCall() once as in TestSync()
  503. // 2. For all volumes/claims changed by previous syncVolume/syncClaim calls,
  504. // call appropriate syncVolume/syncClaim (simulating "volume/claim changed"
  505. // events). Go to 2. if these calls change anything.
  506. // 3. When all changes are processed and no new changes were made, call
  507. // syncVolume/syncClaim on all volumes/claims (simulating "periodic sync").
  508. // 4. If some changes were done by step 3., go to 2. (simulation of
  509. // "volume/claim updated" events, eventually performing step 3. again)
  510. // 5. When 3. does not do any changes, finish the tests and compare final set
  511. // of volumes/claims with expected claims/volumes and report differences.
  512. // Some limit of calls in enforced to prevent endless loops.
  513. func TestProvisionMultiSync(t *testing.T) {
  514. tests := []controllerTest{
  515. {
  516. // Provision a volume with binding
  517. "12-1 - successful provision",
  518. novolumes,
  519. newVolumeArray("pvc-uid12-1", "1Gi", "uid12-1", "claim12-1", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classGold, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned),
  520. newClaimArray("claim12-1", "uid12-1", "1Gi", "", v1.ClaimPending, &classGold),
  521. newClaimArray("claim12-1", "uid12-1", "1Gi", "pvc-uid12-1", v1.ClaimBound, &classGold, pvutil.AnnBoundByController, pvutil.AnnBindCompleted, pvutil.AnnStorageProvisioner),
  522. noevents, noerrors, wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  523. },
  524. {
  525. // provision a volume (external provisioner) and binding + normal event with external provisioner
  526. "12-2 - external provisioner with volume provisioned success",
  527. novolumes,
  528. newVolumeArray("pvc-uid12-2", "1Gi", "uid12-2", "claim12-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, pvutil.AnnBoundByController),
  529. newClaimArray("claim12-2", "uid12-2", "1Gi", "", v1.ClaimPending, &classExternal),
  530. claimWithAnnotation(pvutil.AnnStorageProvisioner, "vendor.com/my-volume",
  531. newClaimArray("claim12-2", "uid12-2", "1Gi", "pvc-uid12-2", v1.ClaimBound, &classExternal, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  532. []string{"Normal ExternalProvisioning"},
  533. noerrors,
  534. wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
  535. // Create a volume before syncClaim tries to bind a PV to PVC
  536. // This simulates external provisioner creating a volume while the controller
  537. // is waiting for a volume to bind to the existed claim
  538. // the external provisioner workflow implemented in "provisionClaimOperationCSI"
  539. // should issue an ExternalProvisioning event to signal that some external provisioner
  540. // is working on provisioning the PV, also add the operation start timestamp into local cache
  541. // operationTimestamps. Rely on the existences of the start time stamp to create a PV for binding
  542. if ctrl.operationTimestamps.Has("default/claim12-2") {
  543. volume := newVolume("pvc-uid12-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classExternal)
  544. ctrl.volumes.store.Add(volume) // add the volume to controller
  545. reactor.AddVolume(volume)
  546. }
  547. }),
  548. },
  549. {
  550. // provision a volume (external provisioner) but binding will not happen + normal event with external provisioner
  551. "12-3 - external provisioner with volume to be provisioned",
  552. novolumes,
  553. novolumes,
  554. newClaimArray("claim12-3", "uid12-3", "1Gi", "", v1.ClaimPending, &classExternal),
  555. claimWithAnnotation(pvutil.AnnStorageProvisioner, "vendor.com/my-volume",
  556. newClaimArray("claim12-3", "uid12-3", "1Gi", "", v1.ClaimPending, &classExternal)),
  557. []string{"Normal ExternalProvisioning"},
  558. noerrors,
  559. wrapTestWithProvisionCalls([]provisionCall{provision1Success}, testSyncClaim),
  560. },
  561. {
  562. // provision a volume (external provisioner) and binding + normal event with external provisioner
  563. "12-4 - external provisioner with volume provisioned/bound success",
  564. novolumes,
  565. newVolumeArray("pvc-uid12-4", "1Gi", "uid12-4", "claim12-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, pvutil.AnnBoundByController),
  566. newClaimArray("claim12-4", "uid12-4", "1Gi", "", v1.ClaimPending, &classExternal),
  567. claimWithAnnotation(pvutil.AnnStorageProvisioner, "vendor.com/my-volume",
  568. newClaimArray("claim12-4", "uid12-4", "1Gi", "pvc-uid12-4", v1.ClaimBound, &classExternal, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  569. []string{"Normal ExternalProvisioning"},
  570. noerrors,
  571. wrapTestWithInjectedOperation(wrapTestWithProvisionCalls([]provisionCall{}, testSyncClaim), func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor) {
  572. // Create a volume before syncClaim tries to bind a PV to PVC
  573. // This simulates external provisioner creating a volume while the controller
  574. // is waiting for a volume to bind to the existed claim
  575. // the external provisioner workflow implemented in "provisionClaimOperationCSI"
  576. // should issue an ExternalProvisioning event to signal that some external provisioner
  577. // is working on provisioning the PV, also add the operation start timestamp into local cache
  578. // operationTimestamps. Rely on the existences of the start time stamp to create a PV for binding
  579. if ctrl.operationTimestamps.Has("default/claim12-4") {
  580. volume := newVolume("pvc-uid12-4", "1Gi", "uid12-4", "claim12-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classExternal, pvutil.AnnBoundByController)
  581. ctrl.volumes.store.Add(volume) // add the volume to controller
  582. reactor.AddVolume(volume)
  583. }
  584. }),
  585. },
  586. }
  587. runMultisyncTests(t, tests, storageClasses, storageClasses[0].Name)
  588. }
  589. // When provisioning is disabled, provisioning a claim should instantly return nil
  590. func TestDisablingDynamicProvisioner(t *testing.T) {
  591. ctrl, err := newTestController(nil, nil, false)
  592. if err != nil {
  593. t.Fatalf("Construct PersistentVolume controller failed: %v", err)
  594. }
  595. retVal := ctrl.provisionClaim(nil)
  596. if retVal != nil {
  597. t.Errorf("Expected nil return but got %v", retVal)
  598. }
  599. }