pv_controller_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. "reflect"
  17. "testing"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. storagev1 "k8s.io/api/storage/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/watch"
  23. utilfeature "k8s.io/apiserver/pkg/util/feature"
  24. "k8s.io/client-go/informers"
  25. "k8s.io/client-go/kubernetes/fake"
  26. storagelisters "k8s.io/client-go/listers/storage/v1"
  27. core "k8s.io/client-go/testing"
  28. "k8s.io/client-go/tools/cache"
  29. "k8s.io/component-base/featuregate"
  30. featuregatetesting "k8s.io/component-base/featuregate/testing"
  31. csitrans "k8s.io/csi-translation-lib"
  32. "k8s.io/klog"
  33. "k8s.io/kubernetes/pkg/controller"
  34. pvtesting "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/testing"
  35. pvutil "k8s.io/kubernetes/pkg/controller/volume/persistentvolume/util"
  36. "k8s.io/kubernetes/pkg/features"
  37. "k8s.io/kubernetes/pkg/volume/csimigration"
  38. )
  39. var (
  40. classNotHere = "not-here"
  41. classNoMode = "no-mode"
  42. classImmediateMode = "immediate-mode"
  43. classWaitMode = "wait-mode"
  44. )
  45. // Test the real controller methods (add/update/delete claim/volume) with
  46. // a fake API server.
  47. // There is no controller API to 'initiate syncAll now', therefore these tests
  48. // can't reliably simulate periodic sync of volumes/claims - it would be
  49. // either very timing-sensitive or slow to wait for real periodic sync.
  50. func TestControllerSync(t *testing.T) {
  51. tests := []controllerTest{
  52. // [Unit test set 5] - controller tests.
  53. // We test the controller as if
  54. // it was connected to real API server, i.e. we call add/update/delete
  55. // Claim/Volume methods. Also, all changes to volumes and claims are
  56. // sent to add/update/delete Claim/Volume as real controller would do.
  57. {
  58. // addClaim gets a new claim. Check it's bound to a volume.
  59. "5-2 - complete bind",
  60. newVolumeArray("volume5-2", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimRetain, classEmpty),
  61. newVolumeArray("volume5-2", "1Gi", "uid5-2", "claim5-2", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  62. noclaims, /* added in testAddClaim5_2 */
  63. newClaimArray("claim5-2", "uid5-2", "1Gi", "volume5-2", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  64. noevents, noerrors,
  65. // Custom test function that generates an add event
  66. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  67. claim := newClaim("claim5-2", "uid5-2", "1Gi", "", v1.ClaimPending, nil)
  68. reactor.AddClaimEvent(claim)
  69. return nil
  70. },
  71. },
  72. {
  73. // deleteClaim with a bound claim makes bound volume released.
  74. "5-3 - delete claim",
  75. newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  76. newVolumeArray("volume5-3", "10Gi", "uid5-3", "claim5-3", v1.VolumeReleased, v1.PersistentVolumeReclaimRetain, classEmpty, pvutil.AnnBoundByController),
  77. newClaimArray("claim5-3", "uid5-3", "1Gi", "volume5-3", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  78. noclaims,
  79. noevents, noerrors,
  80. // Custom test function that generates a delete event
  81. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  82. obj := ctrl.claims.List()[0]
  83. claim := obj.(*v1.PersistentVolumeClaim)
  84. reactor.DeleteClaimEvent(claim)
  85. return nil
  86. },
  87. },
  88. {
  89. // deleteVolume with a bound volume. Check the claim is Lost.
  90. "5-4 - delete volume",
  91. newVolumeArray("volume5-4", "1Gi", "uid5-4", "claim5-4", v1.VolumeBound, v1.PersistentVolumeReclaimRetain, classEmpty),
  92. novolumes,
  93. newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimBound, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  94. newClaimArray("claim5-4", "uid5-4", "1Gi", "volume5-4", v1.ClaimLost, nil, pvutil.AnnBoundByController, pvutil.AnnBindCompleted),
  95. []string{"Warning ClaimLost"}, noerrors,
  96. // Custom test function that generates a delete event
  97. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  98. obj := ctrl.volumes.store.List()[0]
  99. volume := obj.(*v1.PersistentVolume)
  100. reactor.DeleteVolumeEvent(volume)
  101. return nil
  102. },
  103. },
  104. {
  105. // deleteClaim with a bound claim makes bound volume released with external deleter.
  106. // delete the corresponding volume from apiserver, and report latency metric
  107. "5-5 - delete claim and delete volume report metric",
  108. volumesWithAnnotation(pvutil.AnnDynamicallyProvisioned, "gcr.io/vendor-csi",
  109. newVolumeArray("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, pvutil.AnnBoundByController)),
  110. novolumes,
  111. claimWithAnnotation(pvutil.AnnStorageProvisioner, "gcr.io/vendor-csi",
  112. newClaimArray("claim5-5", "uid5-5", "1Gi", "volume5-5", v1.ClaimBound, &classExternal, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  113. noclaims,
  114. noevents, noerrors,
  115. // Custom test function that generates a delete claim event which should have been caught by
  116. // "deleteClaim" to remove the claim from controller's cache, after that, a volume deleted
  117. // event will be generated to trigger "deleteVolume" call for metric reporting
  118. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  119. test.initialVolumes[0].Annotations[pvutil.AnnDynamicallyProvisioned] = "gcr.io/vendor-csi"
  120. obj := ctrl.claims.List()[0]
  121. claim := obj.(*v1.PersistentVolumeClaim)
  122. reactor.DeleteClaimEvent(claim)
  123. for len(ctrl.claims.ListKeys()) > 0 {
  124. time.Sleep(10 * time.Millisecond)
  125. }
  126. // claim has been removed from controller's cache, generate a volume deleted event
  127. volume := ctrl.volumes.store.List()[0].(*v1.PersistentVolume)
  128. reactor.DeleteVolumeEvent(volume)
  129. return nil
  130. },
  131. },
  132. {
  133. // deleteClaim with a bound claim makes bound volume released with external deleter pending
  134. // there should be an entry in operation timestamps cache in controller
  135. "5-6 - delete claim and waiting for external volume deletion",
  136. volumesWithAnnotation(pvutil.AnnDynamicallyProvisioned, "gcr.io/vendor-csi",
  137. newVolumeArray("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, pvutil.AnnBoundByController)),
  138. volumesWithAnnotation(pvutil.AnnDynamicallyProvisioned, "gcr.io/vendor-csi",
  139. newVolumeArray("volume5-6", "10Gi", "uid5-6", "claim5-6", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classExternal, pvutil.AnnBoundByController)),
  140. claimWithAnnotation(pvutil.AnnStorageProvisioner, "gcr.io/vendor-csi",
  141. newClaimArray("claim5-6", "uid5-6", "1Gi", "volume5-6", v1.ClaimBound, &classExternal, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  142. noclaims,
  143. noevents, noerrors,
  144. // Custom test function that generates a delete claim event which should have been caught by
  145. // "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released
  146. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  147. // should have been provisioned by external provisioner
  148. obj := ctrl.claims.List()[0]
  149. claim := obj.(*v1.PersistentVolumeClaim)
  150. reactor.DeleteClaimEvent(claim)
  151. // wait until claim is cleared from cache, i.e., deleteClaim is called
  152. for len(ctrl.claims.ListKeys()) > 0 {
  153. time.Sleep(10 * time.Millisecond)
  154. }
  155. // make sure the operation timestamp cache is NOT empty
  156. if !ctrl.operationTimestamps.Has("volume5-6") {
  157. return errors.New("failed checking timestamp cache: should not be empty")
  158. }
  159. return nil
  160. },
  161. },
  162. {
  163. // deleteVolume event issued before deleteClaim, no metric should have been reported
  164. // and no delete operation start timestamp should be inserted into controller.operationTimestamps cache
  165. "5-7 - delete volume event makes claim lost, delete claim event will not report metric",
  166. newVolumeArray("volume5-7", "10Gi", "uid5-7", "claim5-7", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classExternal, pvutil.AnnBoundByController, pvutil.AnnDynamicallyProvisioned),
  167. novolumes,
  168. claimWithAnnotation(pvutil.AnnStorageProvisioner, "gcr.io/vendor-csi",
  169. newClaimArray("claim5-7", "uid5-7", "1Gi", "volume5-7", v1.ClaimBound, &classExternal, pvutil.AnnBoundByController, pvutil.AnnBindCompleted)),
  170. noclaims,
  171. []string{"Warning ClaimLost"},
  172. noerrors,
  173. // Custom test function that generates a delete claim event which should have been caught by
  174. // "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released
  175. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  176. volume := ctrl.volumes.store.List()[0].(*v1.PersistentVolume)
  177. reactor.DeleteVolumeEvent(volume)
  178. for len(ctrl.volumes.store.ListKeys()) > 0 {
  179. time.Sleep(10 * time.Millisecond)
  180. }
  181. // trying to remove the claim as well
  182. obj := ctrl.claims.List()[0]
  183. claim := obj.(*v1.PersistentVolumeClaim)
  184. reactor.DeleteClaimEvent(claim)
  185. // wait until claim is cleared from cache, i.e., deleteClaim is called
  186. for len(ctrl.claims.ListKeys()) > 0 {
  187. time.Sleep(10 * time.Millisecond)
  188. }
  189. // make sure operation timestamp cache is empty
  190. if ctrl.operationTimestamps.Has("volume5-7") {
  191. return errors.New("failed checking timestamp cache")
  192. }
  193. return nil
  194. },
  195. },
  196. {
  197. // delete a claim waiting for being bound cleans up provision(volume ref == "") entry from timestamp cache
  198. "5-8 - delete claim cleans up operation timestamp cache for provision",
  199. novolumes,
  200. novolumes,
  201. claimWithAnnotation(pvutil.AnnStorageProvisioner, "gcr.io/vendor-csi",
  202. newClaimArray("claim5-8", "uid5-8", "1Gi", "", v1.ClaimPending, &classExternal)),
  203. noclaims,
  204. []string{"Normal ExternalProvisioning"},
  205. noerrors,
  206. // Custom test function that generates a delete claim event which should have been caught by
  207. // "deleteClaim" to remove the claim from controller's cache and mark bound volume to be released
  208. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  209. // wait until the provision timestamp has been inserted
  210. for !ctrl.operationTimestamps.Has("default/claim5-8") {
  211. time.Sleep(10 * time.Millisecond)
  212. }
  213. // delete the claim
  214. obj := ctrl.claims.List()[0]
  215. claim := obj.(*v1.PersistentVolumeClaim)
  216. reactor.DeleteClaimEvent(claim)
  217. // wait until claim is cleared from cache, i.e., deleteClaim is called
  218. for len(ctrl.claims.ListKeys()) > 0 {
  219. time.Sleep(10 * time.Millisecond)
  220. }
  221. // make sure operation timestamp cache is empty
  222. if ctrl.operationTimestamps.Has("default/claim5-8") {
  223. return errors.New("failed checking timestamp cache")
  224. }
  225. return nil
  226. },
  227. },
  228. {
  229. // delete success(?) - volume has deletion timestamp before doDelete() starts
  230. "8-13 - volume is has deletion timestamp and is not processed",
  231. withVolumeDeletionTimestamp(newVolumeArray("volume8-13", "1Gi", "uid8-13", "claim8-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty)),
  232. withVolumeDeletionTimestamp(newVolumeArray("volume8-13", "1Gi", "uid8-13", "claim8-13", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classEmpty)),
  233. noclaims,
  234. noclaims,
  235. noevents, noerrors,
  236. // We don't need to do anything in test function because deletion will be noticed automatically and synced.
  237. // Attempting to use testSyncVolume here will cause an error because of race condition between manually
  238. // calling testSyncVolume and volume loop running.
  239. func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
  240. return nil
  241. },
  242. },
  243. }
  244. for _, test := range tests {
  245. klog.V(4).Infof("starting test %q", test.name)
  246. // Initialize the controller
  247. client := &fake.Clientset{}
  248. fakeVolumeWatch := watch.NewFake()
  249. client.PrependWatchReactor("persistentvolumes", core.DefaultWatchReactor(fakeVolumeWatch, nil))
  250. fakeClaimWatch := watch.NewFake()
  251. client.PrependWatchReactor("persistentvolumeclaims", core.DefaultWatchReactor(fakeClaimWatch, nil))
  252. client.PrependWatchReactor("storageclasses", core.DefaultWatchReactor(watch.NewFake(), nil))
  253. informers := informers.NewSharedInformerFactory(client, controller.NoResyncPeriodFunc())
  254. ctrl, err := newTestController(client, informers, true)
  255. if err != nil {
  256. t.Fatalf("Test %q construct persistent volume failed: %v", test.name, err)
  257. }
  258. // Inject storage classes into controller via a custom lister for test [5-5]
  259. storageClasses := []*storagev1.StorageClass{
  260. makeStorageClass(classExternal, &modeImmediate),
  261. }
  262. storageClasses[0].Provisioner = "gcr.io/vendor-csi"
  263. indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
  264. for _, class := range storageClasses {
  265. indexer.Add(class)
  266. }
  267. ctrl.classLister = storagelisters.NewStorageClassLister(indexer)
  268. reactor := newVolumeReactor(client, ctrl, fakeVolumeWatch, fakeClaimWatch, test.errors)
  269. for _, claim := range test.initialClaims {
  270. claim = claim.DeepCopy()
  271. reactor.AddClaim(claim)
  272. go func(claim *v1.PersistentVolumeClaim) {
  273. fakeClaimWatch.Add(claim)
  274. }(claim)
  275. }
  276. for _, volume := range test.initialVolumes {
  277. volume = volume.DeepCopy()
  278. reactor.AddVolume(volume)
  279. go func(volume *v1.PersistentVolume) {
  280. fakeVolumeWatch.Add(volume)
  281. }(volume)
  282. }
  283. // Start the controller
  284. stopCh := make(chan struct{})
  285. informers.Start(stopCh)
  286. go ctrl.Run(stopCh)
  287. // Wait for the controller to pass initial sync and fill its caches.
  288. for !ctrl.volumeListerSynced() ||
  289. !ctrl.claimListerSynced() ||
  290. len(ctrl.claims.ListKeys()) < len(test.initialClaims) ||
  291. len(ctrl.volumes.store.ListKeys()) < len(test.initialVolumes) {
  292. time.Sleep(10 * time.Millisecond)
  293. }
  294. klog.V(4).Infof("controller synced, starting test")
  295. // Call the tested function
  296. err = test.test(ctrl, reactor.VolumeReactor, test)
  297. if err != nil {
  298. t.Errorf("Test %q initial test call failed: %v", test.name, err)
  299. }
  300. // Simulate a periodic resync, just in case some events arrived in a
  301. // wrong order.
  302. ctrl.resync()
  303. err = reactor.waitTest(test)
  304. if err != nil {
  305. t.Errorf("Failed to run test %s: %v", test.name, err)
  306. }
  307. close(stopCh)
  308. evaluateTestResults(ctrl, reactor.VolumeReactor, test, t)
  309. }
  310. }
  311. func storeVersion(t *testing.T, prefix string, c cache.Store, version string, expectedReturn bool) {
  312. pv := newVolume("pvName", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classEmpty)
  313. pv.ResourceVersion = version
  314. ret, err := storeObjectUpdate(c, pv, "volume")
  315. if err != nil {
  316. t.Errorf("%s: expected storeObjectUpdate to succeed, got: %v", prefix, err)
  317. }
  318. if expectedReturn != ret {
  319. t.Errorf("%s: expected storeObjectUpdate to return %v, got: %v", prefix, expectedReturn, ret)
  320. }
  321. // find the stored version
  322. pvObj, found, err := c.GetByKey("pvName")
  323. if err != nil {
  324. t.Errorf("expected volume 'pvName' in the cache, got error instead: %v", err)
  325. }
  326. if !found {
  327. t.Errorf("expected volume 'pvName' in the cache but it was not found")
  328. }
  329. pv, ok := pvObj.(*v1.PersistentVolume)
  330. if !ok {
  331. t.Errorf("expected volume in the cache, got different object instead: %#v", pvObj)
  332. }
  333. if ret {
  334. if pv.ResourceVersion != version {
  335. t.Errorf("expected volume with version %s in the cache, got %s instead", version, pv.ResourceVersion)
  336. }
  337. } else {
  338. if pv.ResourceVersion == version {
  339. t.Errorf("expected volume with version other than %s in the cache, got %s instead", version, pv.ResourceVersion)
  340. }
  341. }
  342. }
  343. // TestControllerCache tests func storeObjectUpdate()
  344. func TestControllerCache(t *testing.T) {
  345. // Cache under test
  346. c := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
  347. // Store new PV
  348. storeVersion(t, "Step1", c, "1", true)
  349. // Store the same PV
  350. storeVersion(t, "Step2", c, "1", true)
  351. // Store newer PV
  352. storeVersion(t, "Step3", c, "2", true)
  353. // Store older PV - simulating old "PV updated" event or periodic sync with
  354. // old data
  355. storeVersion(t, "Step4", c, "1", false)
  356. // Store newer PV - test integer parsing ("2" > "10" as string,
  357. // while 2 < 10 as integers)
  358. storeVersion(t, "Step5", c, "10", true)
  359. }
  360. func TestControllerCacheParsingError(t *testing.T) {
  361. c := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
  362. // There must be something in the cache to compare with
  363. storeVersion(t, "Step1", c, "1", true)
  364. pv := newVolume("pvName", "1Gi", "", "", v1.VolumeAvailable, v1.PersistentVolumeReclaimDelete, classEmpty)
  365. pv.ResourceVersion = "xxx"
  366. _, err := storeObjectUpdate(c, pv, "volume")
  367. if err == nil {
  368. t.Errorf("Expected parsing error, got nil instead")
  369. }
  370. }
  371. func makePVCClass(scName *string) *v1.PersistentVolumeClaim {
  372. claim := &v1.PersistentVolumeClaim{
  373. ObjectMeta: metav1.ObjectMeta{
  374. Annotations: map[string]string{},
  375. },
  376. Spec: v1.PersistentVolumeClaimSpec{
  377. StorageClassName: scName,
  378. },
  379. }
  380. return claim
  381. }
  382. func makeStorageClass(scName string, mode *storagev1.VolumeBindingMode) *storagev1.StorageClass {
  383. return &storagev1.StorageClass{
  384. ObjectMeta: metav1.ObjectMeta{
  385. Name: scName,
  386. },
  387. VolumeBindingMode: mode,
  388. }
  389. }
  390. func TestDelayBindingMode(t *testing.T) {
  391. tests := map[string]struct {
  392. pvc *v1.PersistentVolumeClaim
  393. shouldDelay bool
  394. shouldFail bool
  395. }{
  396. "nil-class": {
  397. pvc: makePVCClass(nil),
  398. shouldDelay: false,
  399. },
  400. "class-not-found": {
  401. pvc: makePVCClass(&classNotHere),
  402. shouldDelay: false,
  403. },
  404. "no-mode-class": {
  405. pvc: makePVCClass(&classNoMode),
  406. shouldDelay: false,
  407. shouldFail: true,
  408. },
  409. "immediate-mode-class": {
  410. pvc: makePVCClass(&classImmediateMode),
  411. shouldDelay: false,
  412. },
  413. "wait-mode-class": {
  414. pvc: makePVCClass(&classWaitMode),
  415. shouldDelay: true,
  416. },
  417. }
  418. classes := []*storagev1.StorageClass{
  419. makeStorageClass(classNoMode, nil),
  420. makeStorageClass(classImmediateMode, &modeImmediate),
  421. makeStorageClass(classWaitMode, &modeWait),
  422. }
  423. client := &fake.Clientset{}
  424. informerFactory := informers.NewSharedInformerFactory(client, controller.NoResyncPeriodFunc())
  425. classInformer := informerFactory.Storage().V1().StorageClasses()
  426. ctrl := &PersistentVolumeController{
  427. classLister: classInformer.Lister(),
  428. translator: csitrans.New(),
  429. }
  430. for _, class := range classes {
  431. if err := classInformer.Informer().GetIndexer().Add(class); err != nil {
  432. t.Fatalf("Failed to add storage class %q: %v", class.Name, err)
  433. }
  434. }
  435. for name, test := range tests {
  436. shouldDelay, err := pvutil.IsDelayBindingMode(test.pvc, ctrl.classLister)
  437. if err != nil && !test.shouldFail {
  438. t.Errorf("Test %q returned error: %v", name, err)
  439. }
  440. if err == nil && test.shouldFail {
  441. t.Errorf("Test %q returned success, expected error", name)
  442. }
  443. if shouldDelay != test.shouldDelay {
  444. t.Errorf("Test %q returned unexpected %v", name, test.shouldDelay)
  445. }
  446. }
  447. }
  448. func TestAnnealMigrationAnnotations(t *testing.T) {
  449. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, true)()
  450. const testPlugin = "non-migrated-plugin"
  451. const gcePlugin = "kubernetes.io/gce-pd"
  452. const gceDriver = "pd.csi.storage.gke.io"
  453. tests := []struct {
  454. name string
  455. volumeAnnotations map[string]string
  456. expVolumeAnnotations map[string]string
  457. claimAnnotations map[string]string
  458. expClaimAnnotations map[string]string
  459. migratedDriverGates []featuregate.Feature
  460. }{
  461. {
  462. name: "migration on for GCE",
  463. volumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: gcePlugin},
  464. expVolumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: gcePlugin, pvutil.AnnMigratedTo: gceDriver},
  465. claimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: gcePlugin},
  466. expClaimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: gcePlugin, pvutil.AnnMigratedTo: gceDriver},
  467. migratedDriverGates: []featuregate.Feature{features.CSIMigrationGCE},
  468. },
  469. {
  470. name: "migration off for GCE",
  471. volumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: gcePlugin},
  472. expVolumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: gcePlugin},
  473. claimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: gcePlugin},
  474. expClaimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: gcePlugin},
  475. migratedDriverGates: []featuregate.Feature{},
  476. },
  477. {
  478. name: "migration off for GCE removes migrated to (rollback)",
  479. volumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: gcePlugin, pvutil.AnnMigratedTo: gceDriver},
  480. expVolumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: gcePlugin},
  481. claimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: gcePlugin, pvutil.AnnMigratedTo: gceDriver},
  482. expClaimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: gcePlugin},
  483. migratedDriverGates: []featuregate.Feature{},
  484. },
  485. {
  486. name: "migration on for GCE other plugin not affected",
  487. volumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: testPlugin},
  488. expVolumeAnnotations: map[string]string{pvutil.AnnDynamicallyProvisioned: testPlugin},
  489. claimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: testPlugin},
  490. expClaimAnnotations: map[string]string{pvutil.AnnStorageProvisioner: testPlugin},
  491. migratedDriverGates: []featuregate.Feature{features.CSIMigrationGCE},
  492. },
  493. {
  494. name: "not dynamically provisioned migration off for GCE",
  495. volumeAnnotations: map[string]string{},
  496. expVolumeAnnotations: map[string]string{},
  497. claimAnnotations: map[string]string{},
  498. expClaimAnnotations: map[string]string{},
  499. migratedDriverGates: []featuregate.Feature{},
  500. },
  501. {
  502. name: "not dynamically provisioned migration on for GCE",
  503. volumeAnnotations: map[string]string{},
  504. expVolumeAnnotations: map[string]string{},
  505. claimAnnotations: map[string]string{},
  506. expClaimAnnotations: map[string]string{},
  507. migratedDriverGates: []featuregate.Feature{features.CSIMigrationGCE},
  508. },
  509. {
  510. name: "nil annotations migration off for GCE",
  511. volumeAnnotations: nil,
  512. expVolumeAnnotations: nil,
  513. claimAnnotations: nil,
  514. expClaimAnnotations: nil,
  515. migratedDriverGates: []featuregate.Feature{},
  516. },
  517. {
  518. name: "nil annotations migration on for GCE",
  519. volumeAnnotations: nil,
  520. expVolumeAnnotations: nil,
  521. claimAnnotations: nil,
  522. expClaimAnnotations: nil,
  523. migratedDriverGates: []featuregate.Feature{features.CSIMigrationGCE},
  524. },
  525. }
  526. translator := csitrans.New()
  527. cmpm := csimigration.NewPluginManager(translator)
  528. for _, tc := range tests {
  529. t.Run(tc.name, func(t *testing.T) {
  530. for _, f := range tc.migratedDriverGates {
  531. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, f, true)()
  532. }
  533. if tc.volumeAnnotations != nil {
  534. ann := tc.volumeAnnotations
  535. updateMigrationAnnotations(cmpm, translator, ann, pvutil.AnnDynamicallyProvisioned)
  536. if !reflect.DeepEqual(tc.expVolumeAnnotations, ann) {
  537. t.Errorf("got volume annoations: %v, but expected: %v", ann, tc.expVolumeAnnotations)
  538. }
  539. }
  540. if tc.claimAnnotations != nil {
  541. ann := tc.claimAnnotations
  542. updateMigrationAnnotations(cmpm, translator, ann, pvutil.AnnStorageProvisioner)
  543. if !reflect.DeepEqual(tc.expClaimAnnotations, ann) {
  544. t.Errorf("got volume annoations: %v, but expected: %v", ann, tc.expVolumeAnnotations)
  545. }
  546. }
  547. })
  548. }
  549. }