provisioners.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2018 The Rook Authors. All rights reserved.
  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 installer
  14. import (
  15. "fmt"
  16. "os"
  17. "github.com/rook/rook/tests/framework/utils"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. )
  20. const (
  21. hostPathStorageClassName = "hostpath"
  22. )
  23. // ************************************************************************************************
  24. // HostPath provisioner functions
  25. // ************************************************************************************************
  26. func CreateHostPathPVs(k8shelper *utils.K8sHelper, count int, readOnly bool, pvcSize string) error {
  27. logger.Info("creating test PVs")
  28. pv := `
  29. ---
  30. apiVersion: v1
  31. kind: PersistentVolume
  32. metadata:
  33. name: test-hostpath-%d
  34. labels:
  35. hostpath: test
  36. spec:
  37. storageClassName: %s
  38. capacity:
  39. storage: %s
  40. accessModes:
  41. - %s
  42. persistentVolumeReclaimPolicy: Delete
  43. volumeMode: Filesystem
  44. local:
  45. path: "%s"
  46. nodeAffinity:
  47. required:
  48. nodeSelectorTerms:
  49. - matchExpressions:
  50. - key: kubernetes.io/hostname
  51. operator: Exists
  52. `
  53. storageClass := `
  54. ---
  55. kind: StorageClass
  56. apiVersion: storage.k8s.io/v1
  57. metadata:
  58. name: %s
  59. annotations:
  60. storageclass.kubernetes.io/is-default-class: "true"
  61. provisioner: kubernetes.io/no-provisioner
  62. volumeBindingMode: WaitForFirstConsumer
  63. reclaimPolicy: Delete
  64. `
  65. accessMode := "ReadWriteMany"
  66. if readOnly {
  67. accessMode = "ReadWriteOnce"
  68. }
  69. yamlToCreate := fmt.Sprintf(storageClass, hostPathStorageClassName)
  70. for i := 0; i < count; i++ {
  71. tempDir, err := os.MkdirTemp("", "example")
  72. if err != nil {
  73. return fmt.Errorf("failed to create temp dir. %v", err)
  74. }
  75. logger.Infof("created temp dir: %s", tempDir)
  76. yamlToCreate += fmt.Sprintf(pv, i, hostPathStorageClassName, pvcSize, accessMode, tempDir)
  77. }
  78. out, err := k8shelper.KubectlWithStdin(yamlToCreate, createFromStdinArgs...)
  79. if err != nil && !errors.IsAlreadyExists(err) {
  80. return fmt.Errorf("failed to create hostpath provisioner StorageClass: %+v. %s", err, out)
  81. }
  82. present, err := k8shelper.IsStorageClassPresent(hostPathStorageClassName)
  83. if !present {
  84. logger.Errorf("storageClass %s not found: %+v", hostPathStorageClassName, err)
  85. k8shelper.PrintStorageClasses(true /*detailed*/)
  86. return err
  87. }
  88. return nil
  89. }
  90. func DeleteHostPathPVs(k8shelper *utils.K8sHelper) error {
  91. logger.Info("deleting hostpath PVs")
  92. args := []string{"delete", "pv", "-l", "hostpath=test"}
  93. _, err := k8shelper.Kubectl(args...)
  94. if err != nil {
  95. return fmt.Errorf("failed to delete test PVs. %v", err)
  96. }
  97. args = []string{"delete", "sc", hostPathStorageClassName}
  98. out, err := k8shelper.Kubectl(args...)
  99. if err != nil && !utils.IsKubectlErrorNotFound(out, err) {
  100. return fmt.Errorf("failed to delete hostpath StorageClass: %v. %s", err, out)
  101. }
  102. return nil
  103. }