provisioning.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. Copyright 2018 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 testsuites
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/onsi/ginkgo"
  19. "github.com/onsi/gomega"
  20. v1 "k8s.io/api/core/v1"
  21. storagev1 "k8s.io/api/storage/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. "k8s.io/apimachinery/pkg/api/resource"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  26. "k8s.io/apimachinery/pkg/labels"
  27. "k8s.io/client-go/dynamic"
  28. clientset "k8s.io/client-go/kubernetes"
  29. "k8s.io/kubernetes/test/e2e/framework"
  30. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  31. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  32. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  33. "k8s.io/kubernetes/test/e2e/framework/volume"
  34. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  35. )
  36. // StorageClassTest represents parameters to be used by provisioning tests.
  37. // Not all parameters are used by all tests.
  38. type StorageClassTest struct {
  39. Client clientset.Interface
  40. Claim *v1.PersistentVolumeClaim
  41. SourceClaim *v1.PersistentVolumeClaim
  42. Class *storagev1.StorageClass
  43. Name string
  44. CloudProviders []string
  45. Provisioner string
  46. StorageClassName string
  47. Parameters map[string]string
  48. DelayBinding bool
  49. ClaimSize string
  50. ExpectedSize string
  51. PvCheck func(claim *v1.PersistentVolumeClaim)
  52. VolumeMode v1.PersistentVolumeMode
  53. AllowVolumeExpansion bool
  54. }
  55. type provisioningTestSuite struct {
  56. tsInfo TestSuiteInfo
  57. }
  58. var _ TestSuite = &provisioningTestSuite{}
  59. // InitProvisioningTestSuite returns provisioningTestSuite that implements TestSuite interface
  60. func InitProvisioningTestSuite() TestSuite {
  61. return &provisioningTestSuite{
  62. tsInfo: TestSuiteInfo{
  63. Name: "provisioning",
  64. TestPatterns: []testpatterns.TestPattern{
  65. testpatterns.DefaultFsDynamicPV,
  66. testpatterns.NtfsDynamicPV,
  67. },
  68. SupportedSizeRange: volume.SizeRange{
  69. Min: "1Mi",
  70. },
  71. },
  72. }
  73. }
  74. func (p *provisioningTestSuite) GetTestSuiteInfo() TestSuiteInfo {
  75. return p.tsInfo
  76. }
  77. func (p *provisioningTestSuite) SkipRedundantSuite(driver TestDriver, pattern testpatterns.TestPattern) {
  78. }
  79. func (p *provisioningTestSuite) DefineTests(driver TestDriver, pattern testpatterns.TestPattern) {
  80. type local struct {
  81. config *PerTestConfig
  82. driverCleanup func()
  83. testCase *StorageClassTest
  84. cs clientset.Interface
  85. pvc *v1.PersistentVolumeClaim
  86. sourcePVC *v1.PersistentVolumeClaim
  87. sc *storagev1.StorageClass
  88. intreeOps opCounts
  89. migratedOps opCounts
  90. }
  91. var (
  92. dInfo = driver.GetDriverInfo()
  93. dDriver DynamicPVTestDriver
  94. l local
  95. )
  96. ginkgo.BeforeEach(func() {
  97. // Check preconditions.
  98. if pattern.VolType != testpatterns.DynamicPV {
  99. e2eskipper.Skipf("Suite %q does not support %v", p.tsInfo.Name, pattern.VolType)
  100. }
  101. ok := false
  102. dDriver, ok = driver.(DynamicPVTestDriver)
  103. if !ok {
  104. e2eskipper.Skipf("Driver %s doesn't support %v -- skipping", dInfo.Name, pattern.VolType)
  105. }
  106. })
  107. // This intentionally comes after checking the preconditions because it
  108. // registers its own BeforeEach which creates the namespace. Beware that it
  109. // also registers an AfterEach which renders f unusable. Any code using
  110. // f must run inside an It or Context callback.
  111. f := framework.NewDefaultFramework("provisioning")
  112. init := func() {
  113. l = local{}
  114. // Now do the more expensive test initialization.
  115. l.config, l.driverCleanup = driver.PrepareTest(f)
  116. l.intreeOps, l.migratedOps = getMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName)
  117. l.cs = l.config.Framework.ClientSet
  118. testVolumeSizeRange := p.GetTestSuiteInfo().SupportedSizeRange
  119. driverVolumeSizeRange := dDriver.GetDriverInfo().SupportedSizeRange
  120. claimSize, err := getSizeRangesIntersection(testVolumeSizeRange, driverVolumeSizeRange)
  121. framework.ExpectNoError(err, "determine intersection of test size range %+v and driver size range %+v", testVolumeSizeRange, driverVolumeSizeRange)
  122. l.sc = dDriver.GetDynamicProvisionStorageClass(l.config, pattern.FsType)
  123. if l.sc == nil {
  124. e2eskipper.Skipf("Driver %q does not define Dynamic Provision StorageClass - skipping", dInfo.Name)
  125. }
  126. l.pvc = e2epv.MakePersistentVolumeClaim(e2epv.PersistentVolumeClaimConfig{
  127. ClaimSize: claimSize,
  128. StorageClassName: &(l.sc.Name),
  129. }, l.config.Framework.Namespace.Name)
  130. l.sourcePVC = e2epv.MakePersistentVolumeClaim(e2epv.PersistentVolumeClaimConfig{
  131. ClaimSize: claimSize,
  132. StorageClassName: &(l.sc.Name),
  133. }, l.config.Framework.Namespace.Name)
  134. framework.Logf("In creating storage class object and pvc objects for driver - sc: %v, pvc: %v, src-pvc: %v", l.sc, l.pvc, l.sourcePVC)
  135. l.testCase = &StorageClassTest{
  136. Client: l.config.Framework.ClientSet,
  137. Claim: l.pvc,
  138. SourceClaim: l.sourcePVC,
  139. Class: l.sc,
  140. ClaimSize: claimSize,
  141. ExpectedSize: claimSize,
  142. }
  143. }
  144. cleanup := func() {
  145. err := tryFunc(l.driverCleanup)
  146. l.driverCleanup = nil
  147. framework.ExpectNoError(err, "while cleaning up driver")
  148. validateMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName, l.intreeOps, l.migratedOps)
  149. }
  150. ginkgo.It("should provision storage with mount options", func() {
  151. if dInfo.SupportedMountOption == nil {
  152. e2eskipper.Skipf("Driver %q does not define supported mount option - skipping", dInfo.Name)
  153. }
  154. init()
  155. defer cleanup()
  156. l.testCase.Class.MountOptions = dInfo.SupportedMountOption.Union(dInfo.RequiredMountOption).List()
  157. l.testCase.PvCheck = func(claim *v1.PersistentVolumeClaim) {
  158. PVWriteReadSingleNodeCheck(l.cs, claim, l.config.ClientNodeSelection)
  159. }
  160. l.testCase.TestDynamicProvisioning()
  161. })
  162. ginkgo.It("should provision storage with snapshot data source [Feature:VolumeSnapshotDataSource]", func() {
  163. if !dInfo.Capabilities[CapSnapshotDataSource] {
  164. e2eskipper.Skipf("Driver %q does not support populate data from snapshot - skipping", dInfo.Name)
  165. }
  166. sDriver, ok := driver.(SnapshottableTestDriver)
  167. if !ok {
  168. framework.Failf("Driver %q has CapSnapshotDataSource but does not implement SnapshottableTestDriver", dInfo.Name)
  169. }
  170. init()
  171. defer cleanup()
  172. dc := l.config.Framework.DynamicClient
  173. vsc := sDriver.GetSnapshotClass(l.config)
  174. dataSource, cleanupFunc := prepareSnapshotDataSourceForProvisioning(l.config.ClientNodeSelection, l.cs, dc, l.pvc, l.sc, vsc)
  175. defer cleanupFunc()
  176. l.pvc.Spec.DataSource = dataSource
  177. l.testCase.PvCheck = func(claim *v1.PersistentVolumeClaim) {
  178. ginkgo.By("checking whether the created volume has the pre-populated data")
  179. command := fmt.Sprintf("grep '%s' /mnt/test/initialData", claim.Namespace)
  180. RunInPodWithVolume(l.cs, claim.Namespace, claim.Name, "pvc-snapshot-tester", command, l.config.ClientNodeSelection)
  181. }
  182. l.testCase.TestDynamicProvisioning()
  183. })
  184. ginkgo.It("should provision storage with pvc data source", func() {
  185. if !dInfo.Capabilities[CapPVCDataSource] {
  186. e2eskipper.Skipf("Driver %q does not support cloning - skipping", dInfo.Name)
  187. }
  188. init()
  189. defer cleanup()
  190. dc := l.config.Framework.DynamicClient
  191. dataSource, dataSourceCleanup := preparePVCDataSourceForProvisioning(l.config.ClientNodeSelection, l.cs, dc, l.sourcePVC, l.sc)
  192. defer dataSourceCleanup()
  193. l.pvc.Spec.DataSource = dataSource
  194. l.testCase.PvCheck = func(claim *v1.PersistentVolumeClaim) {
  195. ginkgo.By("checking whether the created volume has the pre-populated data")
  196. command := fmt.Sprintf("grep '%s' /mnt/test/initialData", claim.Namespace)
  197. RunInPodWithVolume(l.cs, claim.Namespace, claim.Name, "pvc-datasource-tester", command, l.config.ClientNodeSelection)
  198. }
  199. l.testCase.TestDynamicProvisioning()
  200. })
  201. }
  202. // TestDynamicProvisioning tests dynamic provisioning with specified StorageClassTest
  203. func (t StorageClassTest) TestDynamicProvisioning() *v1.PersistentVolume {
  204. client := t.Client
  205. gomega.Expect(client).NotTo(gomega.BeNil(), "StorageClassTest.Client is required")
  206. claim := t.Claim
  207. gomega.Expect(claim).NotTo(gomega.BeNil(), "StorageClassTest.Claim is required")
  208. class := t.Class
  209. var err error
  210. if class != nil {
  211. framework.ExpectEqual(*claim.Spec.StorageClassName, class.Name)
  212. ginkgo.By("creating a StorageClass " + class.Name)
  213. _, err = client.StorageV1().StorageClasses().Create(context.TODO(), class, metav1.CreateOptions{})
  214. // The "should provision storage with snapshot data source" test already has created the class.
  215. // TODO: make class creation optional and remove the IsAlreadyExists exception
  216. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  217. class, err = client.StorageV1().StorageClasses().Get(context.TODO(), class.Name, metav1.GetOptions{})
  218. framework.ExpectNoError(err)
  219. defer func() {
  220. framework.Logf("deleting storage class %s", class.Name)
  221. framework.ExpectNoError(client.StorageV1().StorageClasses().Delete(context.TODO(), class.Name, nil))
  222. }()
  223. }
  224. ginkgo.By("creating a claim")
  225. claim, err = client.CoreV1().PersistentVolumeClaims(claim.Namespace).Create(context.TODO(), claim, metav1.CreateOptions{})
  226. framework.ExpectNoError(err)
  227. defer func() {
  228. framework.Logf("deleting claim %q/%q", claim.Namespace, claim.Name)
  229. // typically this claim has already been deleted
  230. err = client.CoreV1().PersistentVolumeClaims(claim.Namespace).Delete(context.TODO(), claim.Name, nil)
  231. if err != nil && !apierrors.IsNotFound(err) {
  232. framework.Failf("Error deleting claim %q. Error: %v", claim.Name, err)
  233. }
  234. }()
  235. // Run the checker
  236. if t.PvCheck != nil {
  237. t.PvCheck(claim)
  238. }
  239. pv := t.checkProvisioning(client, claim, class)
  240. ginkgo.By(fmt.Sprintf("deleting claim %q/%q", claim.Namespace, claim.Name))
  241. framework.ExpectNoError(client.CoreV1().PersistentVolumeClaims(claim.Namespace).Delete(context.TODO(), claim.Name, nil))
  242. // Wait for the PV to get deleted if reclaim policy is Delete. (If it's
  243. // Retain, there's no use waiting because the PV won't be auto-deleted and
  244. // it's expected for the caller to do it.) Technically, the first few delete
  245. // attempts may fail, as the volume is still attached to a node because
  246. // kubelet is slowly cleaning up the previous pod, however it should succeed
  247. // in a couple of minutes. Wait 20 minutes to recover from random cloud
  248. // hiccups.
  249. if pv != nil && pv.Spec.PersistentVolumeReclaimPolicy == v1.PersistentVolumeReclaimDelete {
  250. ginkgo.By(fmt.Sprintf("deleting the claim's PV %q", pv.Name))
  251. framework.ExpectNoError(framework.WaitForPersistentVolumeDeleted(client, pv.Name, 5*time.Second, 20*time.Minute))
  252. }
  253. return pv
  254. }
  255. // getBoundPV returns a PV details.
  256. func getBoundPV(client clientset.Interface, pvc *v1.PersistentVolumeClaim) (*v1.PersistentVolume, error) {
  257. // Get new copy of the claim
  258. claim, err := client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(context.TODO(), pvc.Name, metav1.GetOptions{})
  259. if err != nil {
  260. return nil, err
  261. }
  262. // Get the bound PV
  263. pv, err := client.CoreV1().PersistentVolumes().Get(context.TODO(), claim.Spec.VolumeName, metav1.GetOptions{})
  264. return pv, err
  265. }
  266. // checkProvisioning verifies that the claim is bound and has the correct properities
  267. func (t StorageClassTest) checkProvisioning(client clientset.Interface, claim *v1.PersistentVolumeClaim, class *storagev1.StorageClass) *v1.PersistentVolume {
  268. err := e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, claim.Namespace, claim.Name, framework.Poll, framework.ClaimProvisionTimeout)
  269. framework.ExpectNoError(err)
  270. ginkgo.By("checking the claim")
  271. pv, err := getBoundPV(client, claim)
  272. framework.ExpectNoError(err)
  273. // Check sizes
  274. expectedCapacity := resource.MustParse(t.ExpectedSize)
  275. pvCapacity := pv.Spec.Capacity[v1.ResourceName(v1.ResourceStorage)]
  276. framework.ExpectEqual(pvCapacity.Value(), expectedCapacity.Value(), "pvCapacity is not equal to expectedCapacity")
  277. requestedCapacity := resource.MustParse(t.ClaimSize)
  278. claimCapacity := claim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
  279. framework.ExpectEqual(claimCapacity.Value(), requestedCapacity.Value(), "claimCapacity is not equal to requestedCapacity")
  280. // Check PV properties
  281. ginkgo.By("checking the PV")
  282. // Every access mode in PV should be in PVC
  283. gomega.Expect(pv.Spec.AccessModes).NotTo(gomega.BeZero())
  284. for _, pvMode := range pv.Spec.AccessModes {
  285. found := false
  286. for _, pvcMode := range claim.Spec.AccessModes {
  287. if pvMode == pvcMode {
  288. found = true
  289. break
  290. }
  291. }
  292. framework.ExpectEqual(found, true)
  293. }
  294. framework.ExpectEqual(pv.Spec.ClaimRef.Name, claim.ObjectMeta.Name)
  295. framework.ExpectEqual(pv.Spec.ClaimRef.Namespace, claim.ObjectMeta.Namespace)
  296. if class == nil {
  297. framework.ExpectEqual(pv.Spec.PersistentVolumeReclaimPolicy, v1.PersistentVolumeReclaimDelete)
  298. } else {
  299. framework.ExpectEqual(pv.Spec.PersistentVolumeReclaimPolicy, *class.ReclaimPolicy)
  300. framework.ExpectEqual(pv.Spec.MountOptions, class.MountOptions)
  301. }
  302. if claim.Spec.VolumeMode != nil {
  303. gomega.Expect(pv.Spec.VolumeMode).NotTo(gomega.BeNil())
  304. framework.ExpectEqual(*pv.Spec.VolumeMode, *claim.Spec.VolumeMode)
  305. }
  306. return pv
  307. }
  308. // PVWriteReadSingleNodeCheck checks that a PV retains data on a single node
  309. // and returns the PV.
  310. //
  311. // It starts two pods:
  312. // - The first pod writes 'hello word' to the /mnt/test (= the volume) on one node.
  313. // - The second pod runs grep 'hello world' on /mnt/test on the same node.
  314. //
  315. // The node is selected by Kubernetes when scheduling the first
  316. // pod. It's then selected via its name for the second pod.
  317. //
  318. // If both succeed, Kubernetes actually allocated something that is
  319. // persistent across pods.
  320. //
  321. // This is a common test that can be called from a StorageClassTest.PvCheck.
  322. func PVWriteReadSingleNodeCheck(client clientset.Interface, claim *v1.PersistentVolumeClaim, node e2epod.NodeSelection) *v1.PersistentVolume {
  323. ginkgo.By(fmt.Sprintf("checking the created volume is writable on node %+v", node))
  324. command := "echo 'hello world' > /mnt/test/data"
  325. pod := StartInPodWithVolume(client, claim.Namespace, claim.Name, "pvc-volume-tester-writer", command, node)
  326. defer func() {
  327. // pod might be nil now.
  328. StopPod(client, pod)
  329. }()
  330. framework.ExpectNoError(e2epod.WaitForPodSuccessInNamespaceSlow(client, pod.Name, pod.Namespace))
  331. runningPod, err := client.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
  332. framework.ExpectNoError(err, "get pod")
  333. actualNodeName := runningPod.Spec.NodeName
  334. StopPod(client, pod)
  335. pod = nil // Don't stop twice.
  336. // Get a new copy of the PV
  337. volume, err := getBoundPV(client, claim)
  338. framework.ExpectNoError(err)
  339. ginkgo.By(fmt.Sprintf("checking the created volume has the correct mount options, is readable and retains data on the same node %q", actualNodeName))
  340. command = "grep 'hello world' /mnt/test/data"
  341. // We give the second pod the additional responsibility of checking the volume has
  342. // been mounted with the PV's mount options, if the PV was provisioned with any
  343. for _, option := range volume.Spec.MountOptions {
  344. // Get entry, get mount options at 6th word, replace brackets with commas
  345. command += fmt.Sprintf(" && ( mount | grep 'on /mnt/test' | awk '{print $6}' | sed 's/^(/,/; s/)$/,/' | grep -q ,%s, )", option)
  346. }
  347. command += " || (mount | grep 'on /mnt/test'; false)"
  348. if framework.NodeOSDistroIs("windows") {
  349. command = "select-string 'hello world' /mnt/test/data"
  350. }
  351. RunInPodWithVolume(client, claim.Namespace, claim.Name, "pvc-volume-tester-reader", command, e2epod.NodeSelection{Name: actualNodeName})
  352. return volume
  353. }
  354. // PVMultiNodeCheck checks that a PV retains data when moved between nodes.
  355. //
  356. // It starts these pods:
  357. // - The first pod writes 'hello word' to the /mnt/test (= the volume) on one node.
  358. // - The second pod runs grep 'hello world' on /mnt/test on another node.
  359. //
  360. // The first node is selected by Kubernetes when scheduling the first pod. The second pod uses the same criteria, except that a special anti-affinity
  361. // for the first node gets added. This test can only pass if the cluster has more than one
  362. // suitable node. The caller has to ensure that.
  363. //
  364. // If all succeeds, Kubernetes actually allocated something that is
  365. // persistent across pods and across nodes.
  366. //
  367. // This is a common test that can be called from a StorageClassTest.PvCheck.
  368. func PVMultiNodeCheck(client clientset.Interface, claim *v1.PersistentVolumeClaim, node e2epod.NodeSelection) {
  369. framework.ExpectEqual(node.Name, "", "this test only works when not locked onto a single node")
  370. var pod *v1.Pod
  371. defer func() {
  372. // passing pod = nil is okay.
  373. StopPod(client, pod)
  374. }()
  375. ginkgo.By(fmt.Sprintf("checking the created volume is writable on node %+v", node))
  376. command := "echo 'hello world' > /mnt/test/data"
  377. pod = StartInPodWithVolume(client, claim.Namespace, claim.Name, "pvc-writer-node1", command, node)
  378. framework.ExpectNoError(e2epod.WaitForPodSuccessInNamespaceSlow(client, pod.Name, pod.Namespace))
  379. runningPod, err := client.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
  380. framework.ExpectNoError(err, "get pod")
  381. actualNodeName := runningPod.Spec.NodeName
  382. StopPod(client, pod)
  383. pod = nil // Don't stop twice.
  384. // Add node-anti-affinity.
  385. secondNode := node
  386. e2epod.SetAntiAffinity(&secondNode, actualNodeName)
  387. ginkgo.By(fmt.Sprintf("checking the created volume is readable and retains data on another node %+v", secondNode))
  388. command = "grep 'hello world' /mnt/test/data"
  389. if framework.NodeOSDistroIs("windows") {
  390. command = "select-string 'hello world' /mnt/test/data"
  391. }
  392. pod = StartInPodWithVolume(client, claim.Namespace, claim.Name, "pvc-reader-node2", command, secondNode)
  393. framework.ExpectNoError(e2epod.WaitForPodSuccessInNamespaceSlow(client, pod.Name, pod.Namespace))
  394. runningPod, err = client.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
  395. framework.ExpectNoError(err, "get pod")
  396. framework.ExpectNotEqual(runningPod.Spec.NodeName, actualNodeName, "second pod should have run on a different node")
  397. StopPod(client, pod)
  398. pod = nil
  399. }
  400. // TestBindingWaitForFirstConsumerMultiPVC tests the binding with WaitForFirstConsumer mode
  401. func (t StorageClassTest) TestBindingWaitForFirstConsumerMultiPVC(claims []*v1.PersistentVolumeClaim, nodeSelector map[string]string, expectUnschedulable bool) ([]*v1.PersistentVolume, *v1.Node) {
  402. var err error
  403. framework.ExpectNotEqual(len(claims), 0)
  404. namespace := claims[0].Namespace
  405. ginkgo.By("creating a storage class " + t.Class.Name)
  406. class, err := t.Client.StorageV1().StorageClasses().Create(context.TODO(), t.Class, metav1.CreateOptions{})
  407. framework.ExpectNoError(err)
  408. defer func() {
  409. err = deleteStorageClass(t.Client, class.Name)
  410. framework.ExpectNoError(err, "While deleting storage class")
  411. }()
  412. ginkgo.By("creating claims")
  413. var claimNames []string
  414. var createdClaims []*v1.PersistentVolumeClaim
  415. for _, claim := range claims {
  416. c, err := t.Client.CoreV1().PersistentVolumeClaims(claim.Namespace).Create(context.TODO(), claim, metav1.CreateOptions{})
  417. claimNames = append(claimNames, c.Name)
  418. createdClaims = append(createdClaims, c)
  419. framework.ExpectNoError(err)
  420. }
  421. defer func() {
  422. errors := map[string]error{}
  423. for _, claim := range createdClaims {
  424. err := e2epv.DeletePersistentVolumeClaim(t.Client, claim.Name, claim.Namespace)
  425. if err != nil {
  426. errors[claim.Name] = err
  427. }
  428. }
  429. if len(errors) > 0 {
  430. for claimName, err := range errors {
  431. framework.Logf("Failed to delete PVC: %s due to error: %v", claimName, err)
  432. }
  433. }
  434. }()
  435. // Wait for ClaimProvisionTimeout (across all PVCs in parallel) and make sure the phase did not become Bound i.e. the Wait errors out
  436. ginkgo.By("checking the claims are in pending state")
  437. err = e2epv.WaitForPersistentVolumeClaimsPhase(v1.ClaimBound, t.Client, namespace, claimNames, 2*time.Second /* Poll */, framework.ClaimProvisionShortTimeout, true)
  438. framework.ExpectError(err)
  439. verifyPVCsPending(t.Client, createdClaims)
  440. ginkgo.By("creating a pod referring to the claims")
  441. // Create a pod referring to the claim and wait for it to get to running
  442. var pod *v1.Pod
  443. if expectUnschedulable {
  444. pod, err = e2epod.CreateUnschedulablePod(t.Client, namespace, nodeSelector, createdClaims, true /* isPrivileged */, "" /* command */)
  445. } else {
  446. pod, err = e2epod.CreatePod(t.Client, namespace, nil /* nodeSelector */, createdClaims, true /* isPrivileged */, "" /* command */)
  447. }
  448. framework.ExpectNoError(err)
  449. defer func() {
  450. e2epod.DeletePodOrFail(t.Client, pod.Namespace, pod.Name)
  451. e2epod.WaitForPodToDisappear(t.Client, pod.Namespace, pod.Name, labels.Everything(), framework.Poll, framework.PodDeleteTimeout)
  452. }()
  453. if expectUnschedulable {
  454. // Verify that no claims are provisioned.
  455. verifyPVCsPending(t.Client, createdClaims)
  456. return nil, nil
  457. }
  458. // collect node details
  459. node, err := t.Client.CoreV1().Nodes().Get(context.TODO(), pod.Spec.NodeName, metav1.GetOptions{})
  460. framework.ExpectNoError(err)
  461. ginkgo.By("re-checking the claims to see they bound")
  462. var pvs []*v1.PersistentVolume
  463. for _, claim := range createdClaims {
  464. // Get new copy of the claim
  465. claim, err = t.Client.CoreV1().PersistentVolumeClaims(claim.Namespace).Get(context.TODO(), claim.Name, metav1.GetOptions{})
  466. framework.ExpectNoError(err)
  467. // make sure claim did bind
  468. err = e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, t.Client, claim.Namespace, claim.Name, framework.Poll, framework.ClaimProvisionTimeout)
  469. framework.ExpectNoError(err)
  470. pv, err := t.Client.CoreV1().PersistentVolumes().Get(context.TODO(), claim.Spec.VolumeName, metav1.GetOptions{})
  471. framework.ExpectNoError(err)
  472. pvs = append(pvs, pv)
  473. }
  474. framework.ExpectEqual(len(pvs), len(createdClaims))
  475. return pvs, node
  476. }
  477. // RunInPodWithVolume runs a command in a pod with given claim mounted to /mnt directory.
  478. // It starts, checks, collects output and stops it.
  479. func RunInPodWithVolume(c clientset.Interface, ns, claimName, podName, command string, node e2epod.NodeSelection) {
  480. pod := StartInPodWithVolume(c, ns, claimName, podName, command, node)
  481. defer StopPod(c, pod)
  482. framework.ExpectNoError(e2epod.WaitForPodSuccessInNamespaceSlow(c, pod.Name, pod.Namespace))
  483. }
  484. // StartInPodWithVolume starts a command in a pod with given claim mounted to /mnt directory
  485. // The caller is responsible for checking the pod and deleting it.
  486. func StartInPodWithVolume(c clientset.Interface, ns, claimName, podName, command string, node e2epod.NodeSelection) *v1.Pod {
  487. pod := &v1.Pod{
  488. TypeMeta: metav1.TypeMeta{
  489. Kind: "Pod",
  490. APIVersion: "v1",
  491. },
  492. ObjectMeta: metav1.ObjectMeta{
  493. GenerateName: podName + "-",
  494. Labels: map[string]string{
  495. "app": podName,
  496. },
  497. },
  498. Spec: v1.PodSpec{
  499. Containers: []v1.Container{
  500. {
  501. Name: "volume-tester",
  502. Image: volume.GetTestImage(framework.BusyBoxImage),
  503. Command: volume.GenerateScriptCmd(command),
  504. VolumeMounts: []v1.VolumeMount{
  505. {
  506. Name: "my-volume",
  507. MountPath: "/mnt/test",
  508. },
  509. },
  510. },
  511. },
  512. RestartPolicy: v1.RestartPolicyNever,
  513. Volumes: []v1.Volume{
  514. {
  515. Name: "my-volume",
  516. VolumeSource: v1.VolumeSource{
  517. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  518. ClaimName: claimName,
  519. ReadOnly: false,
  520. },
  521. },
  522. },
  523. },
  524. },
  525. }
  526. e2epod.SetNodeSelection(pod, node)
  527. pod, err := c.CoreV1().Pods(ns).Create(context.TODO(), pod, metav1.CreateOptions{})
  528. framework.ExpectNoError(err, "Failed to create pod: %v", err)
  529. return pod
  530. }
  531. // StopPod first tries to log the output of the pod's container, then deletes the pod and
  532. // waits for that to succeed.
  533. func StopPod(c clientset.Interface, pod *v1.Pod) {
  534. if pod == nil {
  535. return
  536. }
  537. body, err := c.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{}).Do(context.TODO()).Raw()
  538. if err != nil {
  539. framework.Logf("Error getting logs for pod %s: %v", pod.Name, err)
  540. } else {
  541. framework.Logf("Pod %s has the following logs: %s", pod.Name, body)
  542. }
  543. e2epod.DeletePodOrFail(c, pod.Namespace, pod.Name)
  544. e2epod.WaitForPodNoLongerRunningInNamespace(c, pod.Name, pod.Namespace)
  545. }
  546. func verifyPVCsPending(client clientset.Interface, pvcs []*v1.PersistentVolumeClaim) {
  547. for _, claim := range pvcs {
  548. // Get new copy of the claim
  549. claim, err := client.CoreV1().PersistentVolumeClaims(claim.Namespace).Get(context.TODO(), claim.Name, metav1.GetOptions{})
  550. framework.ExpectNoError(err)
  551. framework.ExpectEqual(claim.Status.Phase, v1.ClaimPending)
  552. }
  553. }
  554. func prepareSnapshotDataSourceForProvisioning(
  555. node e2epod.NodeSelection,
  556. client clientset.Interface,
  557. dynamicClient dynamic.Interface,
  558. initClaim *v1.PersistentVolumeClaim,
  559. class *storagev1.StorageClass,
  560. snapshotClass *unstructured.Unstructured,
  561. ) (*v1.TypedLocalObjectReference, func()) {
  562. var err error
  563. if class != nil {
  564. ginkgo.By("[Initialize dataSource]creating a StorageClass " + class.Name)
  565. _, err = client.StorageV1().StorageClasses().Create(context.TODO(), class, metav1.CreateOptions{})
  566. framework.ExpectNoError(err)
  567. }
  568. ginkgo.By("[Initialize dataSource]creating a initClaim")
  569. updatedClaim, err := client.CoreV1().PersistentVolumeClaims(initClaim.Namespace).Create(context.TODO(), initClaim, metav1.CreateOptions{})
  570. framework.ExpectNoError(err)
  571. // write namespace to the /mnt/test (= the volume).
  572. ginkgo.By("[Initialize dataSource]write data to volume")
  573. command := fmt.Sprintf("echo '%s' > /mnt/test/initialData", updatedClaim.GetNamespace())
  574. RunInPodWithVolume(client, updatedClaim.Namespace, updatedClaim.Name, "pvc-snapshot-writer", command, node)
  575. err = e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, updatedClaim.Namespace, updatedClaim.Name, framework.Poll, framework.ClaimProvisionTimeout)
  576. framework.ExpectNoError(err)
  577. ginkgo.By("[Initialize dataSource]checking the initClaim")
  578. // Get new copy of the initClaim
  579. _, err = client.CoreV1().PersistentVolumeClaims(updatedClaim.Namespace).Get(context.TODO(), updatedClaim.Name, metav1.GetOptions{})
  580. framework.ExpectNoError(err)
  581. ginkgo.By("[Initialize dataSource]creating a SnapshotClass")
  582. snapshotClass, err = dynamicClient.Resource(snapshotClassGVR).Create(snapshotClass, metav1.CreateOptions{})
  583. ginkgo.By("[Initialize dataSource]creating a snapshot")
  584. snapshot := getSnapshot(updatedClaim.Name, updatedClaim.Namespace, snapshotClass.GetName())
  585. snapshot, err = dynamicClient.Resource(snapshotGVR).Namespace(updatedClaim.Namespace).Create(snapshot, metav1.CreateOptions{})
  586. framework.ExpectNoError(err)
  587. WaitForSnapshotReady(dynamicClient, snapshot.GetNamespace(), snapshot.GetName(), framework.Poll, framework.SnapshotCreateTimeout)
  588. framework.ExpectNoError(err)
  589. ginkgo.By("[Initialize dataSource]checking the snapshot")
  590. // Get new copy of the snapshot
  591. snapshot, err = dynamicClient.Resource(snapshotGVR).Namespace(snapshot.GetNamespace()).Get(snapshot.GetName(), metav1.GetOptions{})
  592. framework.ExpectNoError(err)
  593. group := "snapshot.storage.k8s.io"
  594. dataSourceRef := &v1.TypedLocalObjectReference{
  595. APIGroup: &group,
  596. Kind: "VolumeSnapshot",
  597. Name: snapshot.GetName(),
  598. }
  599. cleanupFunc := func() {
  600. framework.Logf("deleting snapshot %q/%q", snapshot.GetNamespace(), snapshot.GetName())
  601. err = dynamicClient.Resource(snapshotGVR).Namespace(updatedClaim.Namespace).Delete(snapshot.GetName(), nil)
  602. if err != nil && !apierrors.IsNotFound(err) {
  603. framework.Failf("Error deleting snapshot %q. Error: %v", snapshot.GetName(), err)
  604. }
  605. framework.Logf("deleting initClaim %q/%q", updatedClaim.Namespace, updatedClaim.Name)
  606. err = client.CoreV1().PersistentVolumeClaims(updatedClaim.Namespace).Delete(context.TODO(), updatedClaim.Name, nil)
  607. if err != nil && !apierrors.IsNotFound(err) {
  608. framework.Failf("Error deleting initClaim %q. Error: %v", updatedClaim.Name, err)
  609. }
  610. framework.Logf("deleting SnapshotClass %s", snapshotClass.GetName())
  611. framework.ExpectNoError(dynamicClient.Resource(snapshotClassGVR).Delete(snapshotClass.GetName(), nil))
  612. }
  613. return dataSourceRef, cleanupFunc
  614. }
  615. func preparePVCDataSourceForProvisioning(
  616. node e2epod.NodeSelection,
  617. client clientset.Interface,
  618. dynamicClient dynamic.Interface,
  619. source *v1.PersistentVolumeClaim,
  620. class *storagev1.StorageClass,
  621. ) (*v1.TypedLocalObjectReference, func()) {
  622. var err error
  623. if class != nil {
  624. ginkgo.By("[Initialize dataSource]creating a StorageClass " + class.Name)
  625. _, err = client.StorageV1().StorageClasses().Create(context.TODO(), class, metav1.CreateOptions{})
  626. framework.ExpectNoError(err)
  627. }
  628. ginkgo.By("[Initialize dataSource]creating a source PVC")
  629. sourcePVC, err := client.CoreV1().PersistentVolumeClaims(source.Namespace).Create(context.TODO(), source, metav1.CreateOptions{})
  630. framework.ExpectNoError(err)
  631. // write namespace to the /mnt/test (= the volume).
  632. ginkgo.By("[Initialize dataSource]write data to volume")
  633. command := fmt.Sprintf("echo '%s' > /mnt/test/initialData", sourcePVC.GetNamespace())
  634. RunInPodWithVolume(client, sourcePVC.Namespace, sourcePVC.Name, "pvc-datasource-writer", command, node)
  635. dataSourceRef := &v1.TypedLocalObjectReference{
  636. Kind: "PersistentVolumeClaim",
  637. Name: sourcePVC.GetName(),
  638. }
  639. cleanupFunc := func() {
  640. framework.Logf("deleting source PVC %q/%q", sourcePVC.Namespace, sourcePVC.Name)
  641. err = client.CoreV1().PersistentVolumeClaims(sourcePVC.Namespace).Delete(context.TODO(), sourcePVC.Name, nil)
  642. if err != nil && !apierrors.IsNotFound(err) {
  643. framework.Failf("Error deleting source PVC %q. Error: %v", sourcePVC.Name, err)
  644. }
  645. }
  646. return dataSourceRef, cleanupFunc
  647. }