filesystem.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2016 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 clients
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "time"
  19. "github.com/pkg/errors"
  20. "github.com/rook/rook/pkg/daemon/ceph/client"
  21. "github.com/rook/rook/tests/framework/installer"
  22. "github.com/rook/rook/tests/framework/utils"
  23. "github.com/stretchr/testify/assert"
  24. kerrors "k8s.io/apimachinery/pkg/api/errors"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. )
  27. // FilesystemOperation is a wrapper for k8s rook file operations
  28. type FilesystemOperation struct {
  29. k8sh *utils.K8sHelper
  30. manifests installer.CephManifests
  31. }
  32. // CreateFilesystemOperation Constructor to create FilesystemOperation - client to perform rook file system operations on k8s
  33. func CreateFilesystemOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *FilesystemOperation {
  34. return &FilesystemOperation{k8sh, manifests}
  35. }
  36. // Create creates a filesystem in Rook
  37. func (f *FilesystemOperation) Create(name, namespace string, activeCount int) error {
  38. logger.Infof("creating the filesystem via CRD")
  39. if err := f.k8sh.ResourceOperation("apply", f.manifests.GetFilesystem(name, activeCount)); err != nil {
  40. return err
  41. }
  42. logger.Infof("Make sure rook-ceph-mds pod is running")
  43. err := f.k8sh.WaitForLabeledPodsToRun(fmt.Sprintf("rook_file_system=%s", name), namespace)
  44. assert.Nil(f.k8sh.T(), err)
  45. assert.True(f.k8sh.T(), f.k8sh.CheckPodCountAndState("rook-ceph-mds", namespace, activeCount*2, "Running"),
  46. "Make sure there are four rook-ceph-mds pods present in Running state")
  47. return nil
  48. }
  49. // CreateStorageClass creates a storage class for CephFS clients
  50. func (f *FilesystemOperation) CreateStorageClass(fsName, systemNamespace, namespace, storageClassName string) error {
  51. return f.k8sh.ResourceOperation("apply", f.manifests.GetFileStorageClass(fsName, storageClassName))
  52. }
  53. // CreateSnapshotClass creates a snapshot class for CephFS clients
  54. func (f *FilesystemOperation) CreateSnapshotClass(snapshotClassName, reclaimPolicy, namespace string) error {
  55. return f.k8sh.ResourceOperation("apply", f.manifests.GetFileStorageSnapshotClass(snapshotClassName, reclaimPolicy))
  56. }
  57. // CreatePVCRestore creates a pvc from snapshot
  58. func (f *FilesystemOperation) CreatePVCRestore(namespace, claimName, snapshotName, storageClassName, mode, size string) error {
  59. return f.k8sh.ResourceOperation("apply", installer.GetPVCRestore(claimName, snapshotName, namespace, storageClassName, mode, size))
  60. }
  61. // CreatePVCClone creates a pvc from pvc
  62. func (f *FilesystemOperation) CreatePVCClone(namespace, cloneClaimName, parentClaimName, storageClassName, mode, size string) error {
  63. return f.k8sh.ResourceOperation("apply", installer.GetPVCClone(cloneClaimName, parentClaimName, namespace, storageClassName, mode, size))
  64. }
  65. // CreateSnapshot creates a snapshot from pvc
  66. func (f *FilesystemOperation) CreateSnapshot(snapshotName, claimName, snapshotClassName, namespace string) error {
  67. return f.k8sh.ResourceOperation("apply", installer.GetSnapshot(snapshotName, claimName, snapshotClassName, namespace))
  68. }
  69. // DeleteSnapshot deletes the snapshot
  70. func (f *FilesystemOperation) DeleteSnapshot(snapshotName, claimName, snapshotClassName, namespace string) error {
  71. return f.k8sh.ResourceOperation("delete", installer.GetSnapshot(snapshotName, claimName, snapshotClassName, namespace))
  72. }
  73. func (f *FilesystemOperation) DeletePVC(namespace, claimName string) error {
  74. ctx := context.TODO()
  75. logger.Infof("deleting pvc %q from namespace %q", claimName, namespace)
  76. return f.k8sh.Clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(ctx, claimName, metav1.DeleteOptions{})
  77. }
  78. func (f *FilesystemOperation) DeleteStorageClass(storageClassName string) error {
  79. ctx := context.TODO()
  80. logger.Infof("deleting storage class %q", storageClassName)
  81. err := f.k8sh.Clientset.StorageV1().StorageClasses().Delete(ctx, storageClassName, metav1.DeleteOptions{})
  82. if err != nil && !kerrors.IsNotFound(err) {
  83. return fmt.Errorf("failed to delete storage class %q. %v", storageClassName, err)
  84. }
  85. return nil
  86. }
  87. func (f *FilesystemOperation) CreatePVC(namespace, claimName, storageClassName, mode, size string) error {
  88. return f.k8sh.ResourceOperation("apply", installer.GetPVC(claimName, namespace, storageClassName, mode, size))
  89. }
  90. func (f *FilesystemOperation) CreatePod(podName, claimName, namespace, mountPoint string, readOnly bool) error {
  91. return f.k8sh.ResourceOperation("apply", installer.GetPodWithVolume(podName, claimName, namespace, mountPoint, readOnly))
  92. }
  93. func (f *FilesystemOperation) DeleteSnapshotClass(snapshotClassName, deletePolicy, namespace string) error {
  94. return f.k8sh.ResourceOperation("delete", f.manifests.GetFileStorageSnapshotClass(snapshotClassName, deletePolicy))
  95. }
  96. // ScaleDown scales down the number of active metadata servers of a filesystem in Rook
  97. func (f *FilesystemOperation) ScaleDown(name, namespace string) error {
  98. logger.Infof("scaling down the number of filesystem active metadata servers via CRD")
  99. if err := f.k8sh.ResourceOperation("apply", f.manifests.GetFilesystem(name, 1)); err != nil {
  100. return err
  101. }
  102. assert.True(f.k8sh.T(), f.k8sh.CheckPodCountAndState("rook-ceph-mds", namespace, 2, "Running"),
  103. "Make sure there are two rook-ceph-mds pods present in Running state")
  104. return nil
  105. }
  106. // Delete deletes a filesystem in Rook
  107. func (f *FilesystemOperation) Delete(name, namespace string) error {
  108. ctx := context.TODO()
  109. options := &metav1.DeleteOptions{}
  110. logger.Infof("Deleting subvolumegroup %s in namespace %s dependent of ceph filesystem %s", name+"-csi", namespace, name)
  111. err := f.k8sh.RookClientset.CephV1().CephFilesystemSubVolumeGroups(namespace).Delete(ctx, name+"-csi", *options)
  112. if err != nil && !kerrors.IsNotFound(err) {
  113. return err
  114. }
  115. logger.Infof("Deleting filesystem %s in namespace %s", name, namespace)
  116. err = f.k8sh.RookClientset.CephV1().CephFilesystems(namespace).Delete(ctx, name, *options)
  117. if err != nil && !kerrors.IsNotFound(err) {
  118. return err
  119. }
  120. crdCheckerFunc := func() error {
  121. _, err := f.k8sh.RookClientset.CephV1().CephFilesystems(namespace).Get(ctx, name, metav1.GetOptions{})
  122. return err
  123. }
  124. logger.Infof("Deleted filesystem %s in namespace %s", name, namespace)
  125. return f.k8sh.WaitForCustomResourceDeletion(namespace, name, crdCheckerFunc)
  126. }
  127. // List lists filesystems in Rook
  128. func (f *FilesystemOperation) List(namespace string) ([]client.CephFilesystem, error) {
  129. context := f.k8sh.MakeContext()
  130. clusterInfo := client.AdminTestClusterInfo(namespace)
  131. filesystems, err := client.ListFilesystems(context, clusterInfo)
  132. if err != nil {
  133. return nil, fmt.Errorf("failed to list pools: %+v", err)
  134. }
  135. return filesystems, nil
  136. }
  137. func (f *FilesystemOperation) CreateSubvolumeGroup(fsName, groupName string) error {
  138. namespace := f.manifests.Settings().Namespace
  139. logger.Infof("creating CephFilesystemSubVolumeGroup %q for CephFilesystem %q in namespace %q", groupName, fsName, namespace)
  140. err := f.k8sh.ResourceOperation("apply", f.manifests.GetFilesystemSubvolumeGroup(fsName, groupName))
  141. if err != nil {
  142. return err
  143. }
  144. err = f.k8sh.WaitForStatusPhase(namespace, "CephFilesystemSubVolumeGroup", groupName, "Ready", 30*time.Second)
  145. if err != nil {
  146. return err
  147. }
  148. svgs, err := f.k8sh.ExecToolboxWithRetry(3, namespace, "ceph", []string{"fs", "subvolumegroup", "ls", fsName})
  149. if err != nil {
  150. return errors.Wrapf(err, "failed to list raw ceph subvolumegroups for fs %q in namespace %q", fsName, namespace)
  151. }
  152. if !strings.Contains(svgs, fmt.Sprintf("%q", groupName)) {
  153. return errors.Errorf("cephfs %q in namespace %q does not contain subvolumegroup %q: %v", fsName, namespace, groupName, svgs)
  154. }
  155. return nil
  156. }
  157. func (f *FilesystemOperation) DeleteSubvolumeGroup(fsName, groupName string) error {
  158. namespace := f.manifests.Settings().Namespace
  159. ctx := context.TODO()
  160. logger.Infof("deleting CephFilesystemSubVolumeGroup %q in namespace %q", groupName, namespace)
  161. err := f.k8sh.RookClientset.CephV1().CephFilesystemSubVolumeGroups(namespace).Delete(ctx, groupName, metav1.DeleteOptions{})
  162. if err != nil {
  163. return err
  164. }
  165. err = f.k8sh.WaitForCustomResourceDeletion(namespace, groupName, func() error {
  166. _, err := f.k8sh.RookClientset.CephV1().CephFilesystemSubVolumeGroups(namespace).Get(ctx, groupName, metav1.GetOptions{})
  167. return err
  168. })
  169. if err != nil {
  170. return errors.Wrapf(err, "failed to fully delete CephFilesystemSubVolumeGroup %q in namespace %q", groupName, namespace)
  171. }
  172. svgs, err := f.k8sh.ExecToolboxWithRetry(3, namespace, "ceph", []string{"fs", "subvolumegroup", "ls", fsName})
  173. if err != nil {
  174. return errors.Wrapf(err, "failed to list raw ceph subvolumegroups for fs %q in namespace %q", fsName, namespace)
  175. }
  176. if strings.Contains(svgs, fmt.Sprintf("%q", groupName)) {
  177. return errors.Errorf("cephfs %q in namespace %q still contains subvolumegroup %q: %v", fsName, namespace, groupName, svgs)
  178. }
  179. return nil
  180. }