pv.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. Copyright 2015 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 framework
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/onsi/ginkgo"
  19. v1 "k8s.io/api/core/v1"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/labels"
  24. "k8s.io/apimachinery/pkg/types"
  25. clientset "k8s.io/client-go/kubernetes"
  26. storageutil "k8s.io/kubernetes/pkg/apis/storage/v1/util"
  27. "k8s.io/kubernetes/pkg/volume/util"
  28. "k8s.io/kubernetes/test/e2e/framework"
  29. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  30. )
  31. const (
  32. pdRetryTimeout = 5 * time.Minute
  33. pdRetryPollTime = 5 * time.Second
  34. // PVBindingTimeout is how long PVs have to become bound.
  35. PVBindingTimeout = 3 * time.Minute
  36. // ClaimBindingTimeout is how long claims have to become bound.
  37. ClaimBindingTimeout = 3 * time.Minute
  38. // PVReclaimingTimeout is how long PVs have to beome reclaimed.
  39. PVReclaimingTimeout = 3 * time.Minute
  40. // PVDeletingTimeout is how long PVs have to become deleted.
  41. PVDeletingTimeout = 3 * time.Minute
  42. // VolumeSelectorKey is the key for volume selector.
  43. VolumeSelectorKey = "e2e-pv-pool"
  44. )
  45. var (
  46. // SELinuxLabel is common selinux labels.
  47. SELinuxLabel = &v1.SELinuxOptions{
  48. Level: "s0:c0,c1"}
  49. )
  50. type pvval struct{}
  51. // PVMap is a map of all PVs used in the multi pv-pvc tests. The key is the PV's name, which is
  52. // guaranteed to be unique. The value is {} (empty struct) since we're only interested
  53. // in the PV's name and if it is present. We must always Get the pv object before
  54. // referencing any of its values, eg its ClaimRef.
  55. type PVMap map[string]pvval
  56. type pvcval struct{}
  57. // PVCMap is a map of all PVCs used in the multi pv-pvc tests. The key is "namespace/pvc.Name". The
  58. // value is {} (empty struct) since we're only interested in the PVC's name and if it is
  59. // present. We must always Get the pvc object before referencing any of its values, eg.
  60. // its VolumeName.
  61. // Note: It's unsafe to add keys to a map in a loop. Their insertion in the map is
  62. // unpredictable and can result in the same key being iterated over again.
  63. type PVCMap map[types.NamespacedName]pvcval
  64. // PersistentVolumeConfig is consumed by MakePersistentVolume() to generate a PV object
  65. // for varying storage options (NFS, ceph, glusterFS, etc.).
  66. // (+optional) prebind holds a pre-bound PVC
  67. // Example pvSource:
  68. // pvSource: api.PersistentVolumeSource{
  69. // NFS: &api.NFSVolumeSource{
  70. // ...
  71. // },
  72. // }
  73. type PersistentVolumeConfig struct {
  74. // [Optional] NamePrefix defaults to "pv-" if unset
  75. NamePrefix string
  76. // [Optional] Labels contains information used to organize and categorize
  77. // objects
  78. Labels labels.Set
  79. // PVSource contains the details of the underlying volume and must be set
  80. PVSource v1.PersistentVolumeSource
  81. // [Optional] Prebind lets you specify a PVC to bind this PV to before
  82. // creation
  83. Prebind *v1.PersistentVolumeClaim
  84. // [Optiona] ReclaimPolicy defaults to "Reclaim" if unset
  85. ReclaimPolicy v1.PersistentVolumeReclaimPolicy
  86. StorageClassName string
  87. // [Optional] NodeAffinity defines constraints that limit what nodes this
  88. // volume can be accessed from.
  89. NodeAffinity *v1.VolumeNodeAffinity
  90. // [Optional] VolumeMode defaults to "Filesystem" if unset
  91. VolumeMode *v1.PersistentVolumeMode
  92. // [Optional] AccessModes defaults to RWO if unset
  93. AccessModes []v1.PersistentVolumeAccessMode
  94. // [Optional] Capacity is the storage capacity in Quantity format. Defaults
  95. // to "2Gi" if unset
  96. Capacity string
  97. }
  98. // PersistentVolumeClaimConfig is consumed by MakePersistentVolumeClaim() to
  99. // generate a PVC object.
  100. type PersistentVolumeClaimConfig struct {
  101. // NamePrefix defaults to "pvc-" if unspecified
  102. NamePrefix string
  103. // ClaimSize must be specified in the Quantity format. Defaults to 2Gi if
  104. // unspecified
  105. ClaimSize string
  106. // AccessModes defaults to RWO if unspecified
  107. AccessModes []v1.PersistentVolumeAccessMode
  108. Annotations map[string]string
  109. Selector *metav1.LabelSelector
  110. StorageClassName *string
  111. // VolumeMode defaults to nil if unspecified or specified as the empty
  112. // string
  113. VolumeMode *v1.PersistentVolumeMode
  114. }
  115. // PVPVCCleanup cleans up a pv and pvc in a single pv/pvc test case.
  116. // Note: delete errors are appended to []error so that we can attempt to delete both the pvc and pv.
  117. func PVPVCCleanup(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) []error {
  118. var errs []error
  119. if pvc != nil {
  120. err := DeletePersistentVolumeClaim(c, pvc.Name, ns)
  121. if err != nil {
  122. errs = append(errs, fmt.Errorf("failed to delete PVC %q: %v", pvc.Name, err))
  123. }
  124. } else {
  125. framework.Logf("pvc is nil")
  126. }
  127. if pv != nil {
  128. err := DeletePersistentVolume(c, pv.Name)
  129. if err != nil {
  130. errs = append(errs, fmt.Errorf("failed to delete PV %q: %v", pv.Name, err))
  131. }
  132. } else {
  133. framework.Logf("pv is nil")
  134. }
  135. return errs
  136. }
  137. // PVPVCMapCleanup Cleans up pvs and pvcs in multi-pv-pvc test cases. Entries found in the pv and claim maps are
  138. // deleted as long as the Delete api call succeeds.
  139. // Note: delete errors are appended to []error so that as many pvcs and pvs as possible are deleted.
  140. func PVPVCMapCleanup(c clientset.Interface, ns string, pvols PVMap, claims PVCMap) []error {
  141. var errs []error
  142. for pvcKey := range claims {
  143. err := DeletePersistentVolumeClaim(c, pvcKey.Name, ns)
  144. if err != nil {
  145. errs = append(errs, fmt.Errorf("failed to delete PVC %q: %v", pvcKey.Name, err))
  146. } else {
  147. delete(claims, pvcKey)
  148. }
  149. }
  150. for pvKey := range pvols {
  151. err := DeletePersistentVolume(c, pvKey)
  152. if err != nil {
  153. errs = append(errs, fmt.Errorf("failed to delete PV %q: %v", pvKey, err))
  154. } else {
  155. delete(pvols, pvKey)
  156. }
  157. }
  158. return errs
  159. }
  160. // DeletePersistentVolume deletes the PV.
  161. func DeletePersistentVolume(c clientset.Interface, pvName string) error {
  162. if c != nil && len(pvName) > 0 {
  163. framework.Logf("Deleting PersistentVolume %q", pvName)
  164. err := c.CoreV1().PersistentVolumes().Delete(context.TODO(), pvName, nil)
  165. if err != nil && !apierrors.IsNotFound(err) {
  166. return fmt.Errorf("PV Delete API error: %v", err)
  167. }
  168. }
  169. return nil
  170. }
  171. // DeletePersistentVolumeClaim deletes the Claim.
  172. func DeletePersistentVolumeClaim(c clientset.Interface, pvcName string, ns string) error {
  173. if c != nil && len(pvcName) > 0 {
  174. framework.Logf("Deleting PersistentVolumeClaim %q", pvcName)
  175. err := c.CoreV1().PersistentVolumeClaims(ns).Delete(context.TODO(), pvcName, nil)
  176. if err != nil && !apierrors.IsNotFound(err) {
  177. return fmt.Errorf("PVC Delete API error: %v", err)
  178. }
  179. }
  180. return nil
  181. }
  182. // DeletePVCandValidatePV deletes the PVC and waits for the PV to enter its expected phase. Validate that the PV
  183. // has been reclaimed (assumption here about reclaimPolicy). Caller tells this func which
  184. // phase value to expect for the pv bound to the to-be-deleted claim.
  185. func DeletePVCandValidatePV(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume, expectPVPhase v1.PersistentVolumePhase) error {
  186. pvname := pvc.Spec.VolumeName
  187. framework.Logf("Deleting PVC %v to trigger reclamation of PV %v", pvc.Name, pvname)
  188. err := DeletePersistentVolumeClaim(c, pvc.Name, ns)
  189. if err != nil {
  190. return err
  191. }
  192. // Wait for the PV's phase to return to be `expectPVPhase`
  193. framework.Logf("Waiting for reclaim process to complete.")
  194. err = WaitForPersistentVolumePhase(expectPVPhase, c, pv.Name, framework.Poll, PVReclaimingTimeout)
  195. if err != nil {
  196. return fmt.Errorf("pv %q phase did not become %v: %v", pv.Name, expectPVPhase, err)
  197. }
  198. // examine the pv's ClaimRef and UID and compare to expected values
  199. pv, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{})
  200. if err != nil {
  201. return fmt.Errorf("PV Get API error: %v", err)
  202. }
  203. cr := pv.Spec.ClaimRef
  204. if expectPVPhase == v1.VolumeAvailable {
  205. if cr != nil && len(cr.UID) > 0 {
  206. return fmt.Errorf("PV is 'Available' but ClaimRef.UID is not empty")
  207. }
  208. } else if expectPVPhase == v1.VolumeBound {
  209. if cr == nil {
  210. return fmt.Errorf("PV is 'Bound' but ClaimRef is nil")
  211. }
  212. if len(cr.UID) == 0 {
  213. return fmt.Errorf("PV is 'Bound' but ClaimRef.UID is empty")
  214. }
  215. }
  216. framework.Logf("PV %v now in %q phase", pv.Name, expectPVPhase)
  217. return nil
  218. }
  219. // DeletePVCandValidatePVGroup wraps deletePVCandValidatePV() by calling the function in a loop over the PV map. Only bound PVs
  220. // are deleted. Validates that the claim was deleted and the PV is in the expected Phase (Released,
  221. // Available, Bound).
  222. // Note: if there are more claims than pvs then some of the remaining claims may bind to just made
  223. // available pvs.
  224. func DeletePVCandValidatePVGroup(c clientset.Interface, ns string, pvols PVMap, claims PVCMap, expectPVPhase v1.PersistentVolumePhase) error {
  225. var boundPVs, deletedPVCs int
  226. for pvName := range pvols {
  227. pv, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
  228. if err != nil {
  229. return fmt.Errorf("PV Get API error: %v", err)
  230. }
  231. cr := pv.Spec.ClaimRef
  232. // if pv is bound then delete the pvc it is bound to
  233. if cr != nil && len(cr.Name) > 0 {
  234. boundPVs++
  235. // Assert bound PVC is tracked in this test. Failing this might
  236. // indicate external PVCs interfering with the test.
  237. pvcKey := makePvcKey(ns, cr.Name)
  238. if _, found := claims[pvcKey]; !found {
  239. return fmt.Errorf("internal: claims map is missing pvc %q", pvcKey)
  240. }
  241. // get the pvc for the delete call below
  242. pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Get(context.TODO(), cr.Name, metav1.GetOptions{})
  243. if err == nil {
  244. if err = DeletePVCandValidatePV(c, ns, pvc, pv, expectPVPhase); err != nil {
  245. return err
  246. }
  247. } else if !apierrors.IsNotFound(err) {
  248. return fmt.Errorf("PVC Get API error: %v", err)
  249. }
  250. // delete pvckey from map even if apierrors.IsNotFound above is true and thus the
  251. // claim was not actually deleted here
  252. delete(claims, pvcKey)
  253. deletedPVCs++
  254. }
  255. }
  256. if boundPVs != deletedPVCs {
  257. return fmt.Errorf("expect number of bound PVs (%v) to equal number of deleted PVCs (%v)", boundPVs, deletedPVCs)
  258. }
  259. return nil
  260. }
  261. // create the PV resource. Fails test on error.
  262. func createPV(c clientset.Interface, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
  263. pv, err := c.CoreV1().PersistentVolumes().Create(context.TODO(), pv, metav1.CreateOptions{})
  264. if err != nil {
  265. return nil, fmt.Errorf("PV Create API error: %v", err)
  266. }
  267. return pv, nil
  268. }
  269. // CreatePV creates the PV resource. Fails test on error.
  270. func CreatePV(c clientset.Interface, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
  271. return createPV(c, pv)
  272. }
  273. // CreatePVC creates the PVC resource. Fails test on error.
  274. func CreatePVC(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim) (*v1.PersistentVolumeClaim, error) {
  275. pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Create(context.TODO(), pvc, metav1.CreateOptions{})
  276. if err != nil {
  277. return nil, fmt.Errorf("PVC Create API error: %v", err)
  278. }
  279. return pvc, nil
  280. }
  281. // CreatePVCPV creates a PVC followed by the PV based on the passed in nfs-server ip and
  282. // namespace. If the "preBind" bool is true then pre-bind the PV to the PVC
  283. // via the PV's ClaimRef. Return the pv and pvc to reflect the created objects.
  284. // Note: in the pre-bind case the real PVC name, which is generated, is not
  285. // known until after the PVC is instantiated. This is why the pvc is created
  286. // before the pv.
  287. func CreatePVCPV(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConfig PersistentVolumeClaimConfig, ns string, preBind bool) (*v1.PersistentVolume, *v1.PersistentVolumeClaim, error) {
  288. // make the pvc spec
  289. pvc := MakePersistentVolumeClaim(pvcConfig, ns)
  290. preBindMsg := ""
  291. if preBind {
  292. preBindMsg = " pre-bound"
  293. pvConfig.Prebind = pvc
  294. }
  295. // make the pv spec
  296. pv := MakePersistentVolume(pvConfig)
  297. ginkgo.By(fmt.Sprintf("Creating a PVC followed by a%s PV", preBindMsg))
  298. pvc, err := CreatePVC(c, ns, pvc)
  299. if err != nil {
  300. return nil, nil, err
  301. }
  302. // instantiate the pv, handle pre-binding by ClaimRef if needed
  303. if preBind {
  304. pv.Spec.ClaimRef.Name = pvc.Name
  305. }
  306. pv, err = createPV(c, pv)
  307. if err != nil {
  308. return nil, pvc, err
  309. }
  310. return pv, pvc, nil
  311. }
  312. // CreatePVPVC creates a PV followed by the PVC based on the passed in nfs-server ip and
  313. // namespace. If the "preBind" bool is true then pre-bind the PVC to the PV
  314. // via the PVC's VolumeName. Return the pv and pvc to reflect the created
  315. // objects.
  316. // Note: in the pre-bind case the real PV name, which is generated, is not
  317. // known until after the PV is instantiated. This is why the pv is created
  318. // before the pvc.
  319. func CreatePVPVC(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConfig PersistentVolumeClaimConfig, ns string, preBind bool) (*v1.PersistentVolume, *v1.PersistentVolumeClaim, error) {
  320. preBindMsg := ""
  321. if preBind {
  322. preBindMsg = " pre-bound"
  323. }
  324. framework.Logf("Creating a PV followed by a%s PVC", preBindMsg)
  325. // make the pv and pvc definitions
  326. pv := MakePersistentVolume(pvConfig)
  327. pvc := MakePersistentVolumeClaim(pvcConfig, ns)
  328. // instantiate the pv
  329. pv, err := createPV(c, pv)
  330. if err != nil {
  331. return nil, nil, err
  332. }
  333. // instantiate the pvc, handle pre-binding by VolumeName if needed
  334. if preBind {
  335. pvc.Spec.VolumeName = pv.Name
  336. }
  337. pvc, err = CreatePVC(c, ns, pvc)
  338. if err != nil {
  339. return pv, nil, err
  340. }
  341. return pv, pvc, nil
  342. }
  343. // CreatePVsPVCs creates the desired number of PVs and PVCs and returns them in separate maps. If the
  344. // number of PVs != the number of PVCs then the min of those two counts is the number of
  345. // PVs expected to bind. If a Create error occurs, the returned maps may contain pv and pvc
  346. // entries for the resources that were successfully created. In other words, when the caller
  347. // sees an error returned, it needs to decide what to do about entries in the maps.
  348. // Note: when the test suite deletes the namespace orphaned pvcs and pods are deleted. However,
  349. // orphaned pvs are not deleted and will remain after the suite completes.
  350. func CreatePVsPVCs(numpvs, numpvcs int, c clientset.Interface, ns string, pvConfig PersistentVolumeConfig, pvcConfig PersistentVolumeClaimConfig) (PVMap, PVCMap, error) {
  351. pvMap := make(PVMap, numpvs)
  352. pvcMap := make(PVCMap, numpvcs)
  353. extraPVCs := 0
  354. extraPVs := numpvs - numpvcs
  355. if extraPVs < 0 {
  356. extraPVCs = -extraPVs
  357. extraPVs = 0
  358. }
  359. pvsToCreate := numpvs - extraPVs // want the min(numpvs, numpvcs)
  360. // create pvs and pvcs
  361. for i := 0; i < pvsToCreate; i++ {
  362. pv, pvc, err := CreatePVPVC(c, pvConfig, pvcConfig, ns, false)
  363. if err != nil {
  364. return pvMap, pvcMap, err
  365. }
  366. pvMap[pv.Name] = pvval{}
  367. pvcMap[makePvcKey(ns, pvc.Name)] = pvcval{}
  368. }
  369. // create extra pvs or pvcs as needed
  370. for i := 0; i < extraPVs; i++ {
  371. pv := MakePersistentVolume(pvConfig)
  372. pv, err := createPV(c, pv)
  373. if err != nil {
  374. return pvMap, pvcMap, err
  375. }
  376. pvMap[pv.Name] = pvval{}
  377. }
  378. for i := 0; i < extraPVCs; i++ {
  379. pvc := MakePersistentVolumeClaim(pvcConfig, ns)
  380. pvc, err := CreatePVC(c, ns, pvc)
  381. if err != nil {
  382. return pvMap, pvcMap, err
  383. }
  384. pvcMap[makePvcKey(ns, pvc.Name)] = pvcval{}
  385. }
  386. return pvMap, pvcMap, nil
  387. }
  388. // WaitOnPVandPVC waits for the pv and pvc to bind to each other.
  389. func WaitOnPVandPVC(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) error {
  390. // Wait for newly created PVC to bind to the PV
  391. framework.Logf("Waiting for PV %v to bind to PVC %v", pv.Name, pvc.Name)
  392. err := WaitForPersistentVolumeClaimPhase(v1.ClaimBound, c, ns, pvc.Name, framework.Poll, ClaimBindingTimeout)
  393. if err != nil {
  394. return fmt.Errorf("PVC %q did not become Bound: %v", pvc.Name, err)
  395. }
  396. // Wait for PersistentVolume.Status.Phase to be Bound, which it should be
  397. // since the PVC is already bound.
  398. err = WaitForPersistentVolumePhase(v1.VolumeBound, c, pv.Name, framework.Poll, PVBindingTimeout)
  399. if err != nil {
  400. return fmt.Errorf("PV %q did not become Bound: %v", pv.Name, err)
  401. }
  402. // Re-get the pv and pvc objects
  403. pv, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{})
  404. if err != nil {
  405. return fmt.Errorf("PV Get API error: %v", err)
  406. }
  407. pvc, err = c.CoreV1().PersistentVolumeClaims(ns).Get(context.TODO(), pvc.Name, metav1.GetOptions{})
  408. if err != nil {
  409. return fmt.Errorf("PVC Get API error: %v", err)
  410. }
  411. // The pv and pvc are both bound, but to each other?
  412. // Check that the PersistentVolume.ClaimRef matches the PVC
  413. if pv.Spec.ClaimRef == nil {
  414. return fmt.Errorf("PV %q ClaimRef is nil", pv.Name)
  415. }
  416. if pv.Spec.ClaimRef.Name != pvc.Name {
  417. return fmt.Errorf("PV %q ClaimRef's name (%q) should be %q", pv.Name, pv.Spec.ClaimRef.Name, pvc.Name)
  418. }
  419. if pvc.Spec.VolumeName != pv.Name {
  420. return fmt.Errorf("PVC %q VolumeName (%q) should be %q", pvc.Name, pvc.Spec.VolumeName, pv.Name)
  421. }
  422. if pv.Spec.ClaimRef.UID != pvc.UID {
  423. return fmt.Errorf("PV %q ClaimRef's UID (%q) should be %q", pv.Name, pv.Spec.ClaimRef.UID, pvc.UID)
  424. }
  425. return nil
  426. }
  427. // WaitAndVerifyBinds searches for bound PVs and PVCs by examining pvols for non-nil claimRefs.
  428. // NOTE: Each iteration waits for a maximum of 3 minutes per PV and, if the PV is bound,
  429. // up to 3 minutes for the PVC. When the number of PVs != number of PVCs, this can lead
  430. // to situations where the maximum wait times are reached several times in succession,
  431. // extending test time. Thus, it is recommended to keep the delta between PVs and PVCs
  432. // small.
  433. func WaitAndVerifyBinds(c clientset.Interface, ns string, pvols PVMap, claims PVCMap, testExpected bool) error {
  434. var actualBinds int
  435. expectedBinds := len(pvols)
  436. if expectedBinds > len(claims) { // want the min of # pvs or #pvcs
  437. expectedBinds = len(claims)
  438. }
  439. for pvName := range pvols {
  440. err := WaitForPersistentVolumePhase(v1.VolumeBound, c, pvName, framework.Poll, PVBindingTimeout)
  441. if err != nil && len(pvols) > len(claims) {
  442. framework.Logf("WARN: pv %v is not bound after max wait", pvName)
  443. framework.Logf(" This may be ok since there are more pvs than pvcs")
  444. continue
  445. }
  446. if err != nil {
  447. return fmt.Errorf("PV %q did not become Bound: %v", pvName, err)
  448. }
  449. pv, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
  450. if err != nil {
  451. return fmt.Errorf("PV Get API error: %v", err)
  452. }
  453. cr := pv.Spec.ClaimRef
  454. if cr != nil && len(cr.Name) > 0 {
  455. // Assert bound pvc is a test resource. Failing assertion could
  456. // indicate non-test PVC interference or a bug in the test
  457. pvcKey := makePvcKey(ns, cr.Name)
  458. if _, found := claims[pvcKey]; !found {
  459. return fmt.Errorf("internal: claims map is missing pvc %q", pvcKey)
  460. }
  461. err := WaitForPersistentVolumeClaimPhase(v1.ClaimBound, c, ns, cr.Name, framework.Poll, ClaimBindingTimeout)
  462. if err != nil {
  463. return fmt.Errorf("PVC %q did not become Bound: %v", cr.Name, err)
  464. }
  465. actualBinds++
  466. }
  467. }
  468. if testExpected && actualBinds != expectedBinds {
  469. return fmt.Errorf("expect number of bound PVs (%v) to equal number of claims (%v)", actualBinds, expectedBinds)
  470. }
  471. return nil
  472. }
  473. // Return a pvckey struct.
  474. func makePvcKey(ns, name string) types.NamespacedName {
  475. return types.NamespacedName{Namespace: ns, Name: name}
  476. }
  477. // MakePersistentVolume returns a PV definition based on the nfs server IP. If the PVC is not nil
  478. // then the PV is defined with a ClaimRef which includes the PVC's namespace.
  479. // If the PVC is nil then the PV is not defined with a ClaimRef. If no reclaimPolicy
  480. // is assigned, assumes "Retain". Specs are expected to match the test's PVC.
  481. // Note: the passed-in claim does not have a name until it is created and thus the PV's
  482. // ClaimRef cannot be completely filled-in in this func. Therefore, the ClaimRef's name
  483. // is added later in CreatePVCPV.
  484. func MakePersistentVolume(pvConfig PersistentVolumeConfig) *v1.PersistentVolume {
  485. var claimRef *v1.ObjectReference
  486. if len(pvConfig.AccessModes) == 0 {
  487. pvConfig.AccessModes = append(pvConfig.AccessModes, v1.ReadWriteOnce)
  488. }
  489. if len(pvConfig.NamePrefix) == 0 {
  490. pvConfig.NamePrefix = "pv-"
  491. }
  492. if pvConfig.ReclaimPolicy == "" {
  493. pvConfig.ReclaimPolicy = v1.PersistentVolumeReclaimRetain
  494. }
  495. if len(pvConfig.Capacity) == 0 {
  496. pvConfig.Capacity = "2Gi"
  497. }
  498. if pvConfig.Prebind != nil {
  499. claimRef = &v1.ObjectReference{
  500. Kind: "PersistentVolumeClaim",
  501. APIVersion: "v1",
  502. Name: pvConfig.Prebind.Name,
  503. Namespace: pvConfig.Prebind.Namespace,
  504. UID: pvConfig.Prebind.UID,
  505. }
  506. }
  507. return &v1.PersistentVolume{
  508. ObjectMeta: metav1.ObjectMeta{
  509. GenerateName: pvConfig.NamePrefix,
  510. Labels: pvConfig.Labels,
  511. Annotations: map[string]string{
  512. util.VolumeGidAnnotationKey: "777",
  513. },
  514. },
  515. Spec: v1.PersistentVolumeSpec{
  516. PersistentVolumeReclaimPolicy: pvConfig.ReclaimPolicy,
  517. Capacity: v1.ResourceList{
  518. v1.ResourceStorage: resource.MustParse(pvConfig.Capacity),
  519. },
  520. PersistentVolumeSource: pvConfig.PVSource,
  521. AccessModes: pvConfig.AccessModes,
  522. ClaimRef: claimRef,
  523. StorageClassName: pvConfig.StorageClassName,
  524. NodeAffinity: pvConfig.NodeAffinity,
  525. VolumeMode: pvConfig.VolumeMode,
  526. },
  527. }
  528. }
  529. // MakePersistentVolumeClaim returns a PVC API Object based on the PersistentVolumeClaimConfig.
  530. func MakePersistentVolumeClaim(cfg PersistentVolumeClaimConfig, ns string) *v1.PersistentVolumeClaim {
  531. if len(cfg.AccessModes) == 0 {
  532. cfg.AccessModes = append(cfg.AccessModes, v1.ReadWriteOnce)
  533. }
  534. if len(cfg.ClaimSize) == 0 {
  535. cfg.ClaimSize = "2Gi"
  536. }
  537. if len(cfg.NamePrefix) == 0 {
  538. cfg.NamePrefix = "pvc-"
  539. }
  540. if cfg.VolumeMode != nil && *cfg.VolumeMode == "" {
  541. framework.Logf("Warning: Making PVC: VolumeMode specified as invalid empty string, treating as nil")
  542. cfg.VolumeMode = nil
  543. }
  544. return &v1.PersistentVolumeClaim{
  545. ObjectMeta: metav1.ObjectMeta{
  546. GenerateName: cfg.NamePrefix,
  547. Namespace: ns,
  548. Annotations: cfg.Annotations,
  549. },
  550. Spec: v1.PersistentVolumeClaimSpec{
  551. Selector: cfg.Selector,
  552. AccessModes: cfg.AccessModes,
  553. Resources: v1.ResourceRequirements{
  554. Requests: v1.ResourceList{
  555. v1.ResourceStorage: resource.MustParse(cfg.ClaimSize),
  556. },
  557. },
  558. StorageClassName: cfg.StorageClassName,
  559. VolumeMode: cfg.VolumeMode,
  560. },
  561. }
  562. }
  563. func createPDWithRetry(zone string) (string, error) {
  564. var err error
  565. var newDiskName string
  566. for start := time.Now(); time.Since(start) < pdRetryTimeout; time.Sleep(pdRetryPollTime) {
  567. newDiskName, err = createPD(zone)
  568. if err != nil {
  569. framework.Logf("Couldn't create a new PD, sleeping 5 seconds: %v", err)
  570. continue
  571. }
  572. framework.Logf("Successfully created a new PD: %q.", newDiskName)
  573. return newDiskName, nil
  574. }
  575. return "", err
  576. }
  577. // CreatePDWithRetry creates PD with retry.
  578. func CreatePDWithRetry() (string, error) {
  579. return createPDWithRetry("")
  580. }
  581. // CreatePDWithRetryAndZone creates PD on zone with retry.
  582. func CreatePDWithRetryAndZone(zone string) (string, error) {
  583. return createPDWithRetry(zone)
  584. }
  585. // DeletePDWithRetry deletes PD with retry.
  586. func DeletePDWithRetry(diskName string) error {
  587. var err error
  588. for start := time.Now(); time.Since(start) < pdRetryTimeout; time.Sleep(pdRetryPollTime) {
  589. err = deletePD(diskName)
  590. if err != nil {
  591. framework.Logf("Couldn't delete PD %q, sleeping %v: %v", diskName, pdRetryPollTime, err)
  592. continue
  593. }
  594. framework.Logf("Successfully deleted PD %q.", diskName)
  595. return nil
  596. }
  597. return fmt.Errorf("unable to delete PD %q: %v", diskName, err)
  598. }
  599. func createPD(zone string) (string, error) {
  600. if zone == "" {
  601. zone = framework.TestContext.CloudConfig.Zone
  602. }
  603. return framework.TestContext.CloudConfig.Provider.CreatePD(zone)
  604. }
  605. func deletePD(pdName string) error {
  606. return framework.TestContext.CloudConfig.Provider.DeletePD(pdName)
  607. }
  608. // WaitForPVClaimBoundPhase waits until all pvcs phase set to bound
  609. func WaitForPVClaimBoundPhase(client clientset.Interface, pvclaims []*v1.PersistentVolumeClaim, timeout time.Duration) ([]*v1.PersistentVolume, error) {
  610. persistentvolumes := make([]*v1.PersistentVolume, len(pvclaims))
  611. for index, claim := range pvclaims {
  612. err := WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, claim.Namespace, claim.Name, framework.Poll, timeout)
  613. if err != nil {
  614. return persistentvolumes, err
  615. }
  616. // Get new copy of the claim
  617. claim, err = client.CoreV1().PersistentVolumeClaims(claim.Namespace).Get(context.TODO(), claim.Name, metav1.GetOptions{})
  618. if err != nil {
  619. return persistentvolumes, fmt.Errorf("PVC Get API error: %v", err)
  620. }
  621. // Get the bounded PV
  622. persistentvolumes[index], err = client.CoreV1().PersistentVolumes().Get(context.TODO(), claim.Spec.VolumeName, metav1.GetOptions{})
  623. if err != nil {
  624. return persistentvolumes, fmt.Errorf("PV Get API error: %v", err)
  625. }
  626. }
  627. return persistentvolumes, nil
  628. }
  629. // WaitForPersistentVolumePhase waits for a PersistentVolume to be in a specific phase or until timeout occurs, whichever comes first.
  630. func WaitForPersistentVolumePhase(phase v1.PersistentVolumePhase, c clientset.Interface, pvName string, Poll, timeout time.Duration) error {
  631. framework.Logf("Waiting up to %v for PersistentVolume %s to have phase %s", timeout, pvName, phase)
  632. for start := time.Now(); time.Since(start) < timeout; time.Sleep(Poll) {
  633. pv, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
  634. if err != nil {
  635. framework.Logf("Get persistent volume %s in failed, ignoring for %v: %v", pvName, Poll, err)
  636. continue
  637. }
  638. if pv.Status.Phase == phase {
  639. framework.Logf("PersistentVolume %s found and phase=%s (%v)", pvName, phase, time.Since(start))
  640. return nil
  641. }
  642. framework.Logf("PersistentVolume %s found but phase is %s instead of %s.", pvName, pv.Status.Phase, phase)
  643. }
  644. return fmt.Errorf("PersistentVolume %s not in phase %s within %v", pvName, phase, timeout)
  645. }
  646. // WaitForPersistentVolumeClaimPhase waits for a PersistentVolumeClaim to be in a specific phase or until timeout occurs, whichever comes first.
  647. func WaitForPersistentVolumeClaimPhase(phase v1.PersistentVolumeClaimPhase, c clientset.Interface, ns string, pvcName string, Poll, timeout time.Duration) error {
  648. return WaitForPersistentVolumeClaimsPhase(phase, c, ns, []string{pvcName}, Poll, timeout, true)
  649. }
  650. // WaitForPersistentVolumeClaimsPhase waits for any (if matchAny is true) or all (if matchAny is false) PersistentVolumeClaims
  651. // to be in a specific phase or until timeout occurs, whichever comes first.
  652. func WaitForPersistentVolumeClaimsPhase(phase v1.PersistentVolumeClaimPhase, c clientset.Interface, ns string, pvcNames []string, Poll, timeout time.Duration, matchAny bool) error {
  653. if len(pvcNames) == 0 {
  654. return fmt.Errorf("Incorrect parameter: Need at least one PVC to track. Found 0")
  655. }
  656. framework.Logf("Waiting up to %v for PersistentVolumeClaims %v to have phase %s", timeout, pvcNames, phase)
  657. for start := time.Now(); time.Since(start) < timeout; time.Sleep(Poll) {
  658. phaseFoundInAllClaims := true
  659. for _, pvcName := range pvcNames {
  660. pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Get(context.TODO(), pvcName, metav1.GetOptions{})
  661. if err != nil {
  662. framework.Logf("Failed to get claim %q, retrying in %v. Error: %v", pvcName, Poll, err)
  663. continue
  664. }
  665. if pvc.Status.Phase == phase {
  666. framework.Logf("PersistentVolumeClaim %s found and phase=%s (%v)", pvcName, phase, time.Since(start))
  667. if matchAny {
  668. return nil
  669. }
  670. } else {
  671. framework.Logf("PersistentVolumeClaim %s found but phase is %s instead of %s.", pvcName, pvc.Status.Phase, phase)
  672. phaseFoundInAllClaims = false
  673. }
  674. }
  675. if phaseFoundInAllClaims {
  676. return nil
  677. }
  678. }
  679. return fmt.Errorf("PersistentVolumeClaims %v not all in phase %s within %v", pvcNames, phase, timeout)
  680. }
  681. // CreatePVSource creates a PV source.
  682. func CreatePVSource(zone string) (*v1.PersistentVolumeSource, error) {
  683. diskName, err := CreatePDWithRetryAndZone(zone)
  684. if err != nil {
  685. return nil, err
  686. }
  687. return framework.TestContext.CloudConfig.Provider.CreatePVSource(zone, diskName)
  688. }
  689. // DeletePVSource deletes a PV source.
  690. func DeletePVSource(pvSource *v1.PersistentVolumeSource) error {
  691. return framework.TestContext.CloudConfig.Provider.DeletePVSource(pvSource)
  692. }
  693. // GetDefaultStorageClassName returns default storageClass or return error
  694. func GetDefaultStorageClassName(c clientset.Interface) (string, error) {
  695. list, err := c.StorageV1().StorageClasses().List(context.TODO(), metav1.ListOptions{})
  696. if err != nil {
  697. return "", fmt.Errorf("Error listing storage classes: %v", err)
  698. }
  699. var scName string
  700. for _, sc := range list.Items {
  701. if storageutil.IsDefaultAnnotation(sc.ObjectMeta) {
  702. if len(scName) != 0 {
  703. return "", fmt.Errorf("Multiple default storage classes found: %q and %q", scName, sc.Name)
  704. }
  705. scName = sc.Name
  706. }
  707. }
  708. if len(scName) == 0 {
  709. return "", fmt.Errorf("No default storage class found")
  710. }
  711. framework.Logf("Default storage class: %q", scName)
  712. return scName, nil
  713. }
  714. // SkipIfNoDefaultStorageClass skips tests if no default SC can be found.
  715. func SkipIfNoDefaultStorageClass(c clientset.Interface) {
  716. _, err := GetDefaultStorageClassName(c)
  717. if err != nil {
  718. e2eskipper.Skipf("error finding default storageClass : %v", err)
  719. }
  720. }