driveroperations.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "fmt"
  16. storagev1 "k8s.io/api/storage/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  22. )
  23. // GetDriverNameWithFeatureTags returns driver name with feature tags
  24. // For example)
  25. // - [Driver: nfs]
  26. // - [Driver: rbd][Feature:Volumes]
  27. func GetDriverNameWithFeatureTags(driver TestDriver) string {
  28. dInfo := driver.GetDriverInfo()
  29. return fmt.Sprintf("[Driver: %s]%s", dInfo.Name, dInfo.FeatureTag)
  30. }
  31. // CreateVolume creates volume for test unless dynamicPV or CSI ephemeral inline volume test
  32. func CreateVolume(driver TestDriver, config *PerTestConfig, volType testpatterns.TestVolType) TestVolume {
  33. switch volType {
  34. case testpatterns.InlineVolume:
  35. fallthrough
  36. case testpatterns.PreprovisionedPV:
  37. if pDriver, ok := driver.(PreprovisionedVolumeTestDriver); ok {
  38. return pDriver.CreateVolume(config, volType)
  39. }
  40. case testpatterns.CSIInlineVolume:
  41. fallthrough
  42. case testpatterns.DynamicPV:
  43. // No need to create volume
  44. default:
  45. framework.Failf("Invalid volType specified: %v", volType)
  46. }
  47. return nil
  48. }
  49. // GetStorageClass constructs a new StorageClass instance
  50. // with a unique name that is based on namespace + suffix.
  51. func GetStorageClass(
  52. provisioner string,
  53. parameters map[string]string,
  54. bindingMode *storagev1.VolumeBindingMode,
  55. ns string,
  56. suffix string,
  57. ) *storagev1.StorageClass {
  58. if bindingMode == nil {
  59. defaultBindingMode := storagev1.VolumeBindingImmediate
  60. bindingMode = &defaultBindingMode
  61. }
  62. return &storagev1.StorageClass{
  63. TypeMeta: metav1.TypeMeta{
  64. Kind: "StorageClass",
  65. },
  66. ObjectMeta: metav1.ObjectMeta{
  67. // Name must be unique, so let's base it on namespace name and use GenerateName
  68. Name: names.SimpleNameGenerator.GenerateName(ns + "-" + suffix),
  69. },
  70. Provisioner: provisioner,
  71. Parameters: parameters,
  72. VolumeBindingMode: bindingMode,
  73. }
  74. }
  75. // GetSnapshotClass constructs a new SnapshotClass instance
  76. // with a unique name that is based on namespace + suffix.
  77. func GetSnapshotClass(
  78. snapshotter string,
  79. parameters map[string]string,
  80. ns string,
  81. suffix string,
  82. ) *unstructured.Unstructured {
  83. snapshotClass := &unstructured.Unstructured{
  84. Object: map[string]interface{}{
  85. "kind": "VolumeSnapshotClass",
  86. "apiVersion": snapshotAPIVersion,
  87. "metadata": map[string]interface{}{
  88. // Name must be unique, so let's base it on namespace name
  89. "name": ns + "-" + suffix,
  90. },
  91. "driver": snapshotter,
  92. "parameters": parameters,
  93. "deletionPolicy": "Delete",
  94. },
  95. }
  96. return snapshotClass
  97. }