pv_controller_test.go 18 KB

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