driveroperations.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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.DynamicPV:
  41. // No need to create volume
  42. default:
  43. framework.Failf("Invalid volType specified: %v", volType)
  44. }
  45. return nil
  46. }
  47. // GetStorageClass constructs a new StorageClass instance
  48. // with a unique name that is based on namespace + suffix.
  49. func GetStorageClass(
  50. provisioner string,
  51. parameters map[string]string,
  52. bindingMode *storagev1.VolumeBindingMode,
  53. ns string,
  54. suffix string,
  55. ) *storagev1.StorageClass {
  56. if bindingMode == nil {
  57. defaultBindingMode := storagev1.VolumeBindingImmediate
  58. bindingMode = &defaultBindingMode
  59. }
  60. return &storagev1.StorageClass{
  61. TypeMeta: metav1.TypeMeta{
  62. Kind: "StorageClass",
  63. },
  64. ObjectMeta: metav1.ObjectMeta{
  65. // Name must be unique, so let's base it on namespace name and use GenerateName
  66. Name: names.SimpleNameGenerator.GenerateName(ns + "-" + suffix),
  67. },
  68. Provisioner: provisioner,
  69. Parameters: parameters,
  70. VolumeBindingMode: bindingMode,
  71. }
  72. }
  73. // GetSnapshotClass constructs a new SnapshotClass instance
  74. // with a unique name that is based on namespace + suffix.
  75. func GetSnapshotClass(
  76. snapshotter string,
  77. parameters map[string]string,
  78. ns string,
  79. suffix string,
  80. ) *unstructured.Unstructured {
  81. snapshotClass := &unstructured.Unstructured{
  82. Object: map[string]interface{}{
  83. "kind": "VolumeSnapshotClass",
  84. "apiVersion": snapshotAPIVersion,
  85. "metadata": map[string]interface{}{
  86. // Name must be unique, so let's base it on namespace name
  87. "name": ns + "-" + suffix,
  88. },
  89. "snapshotter": snapshotter,
  90. "parameters": parameters,
  91. },
  92. }
  93. return snapshotClass
  94. }