nfs.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2019 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. "github.com/rook/rook/tests/framework/installer"
  18. "github.com/rook/rook/tests/framework/utils"
  19. "github.com/stretchr/testify/assert"
  20. "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. )
  23. // NFSOperation is a wrapper for k8s rook file operations
  24. type NFSOperation struct {
  25. k8sh *utils.K8sHelper
  26. manifests installer.CephManifests
  27. }
  28. // CreateNFSOperation Constructor to create NFSOperation - client to perform ceph nfs operations on k8s
  29. func CreateNFSOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *NFSOperation {
  30. return &NFSOperation{k8sh, manifests}
  31. }
  32. // Create creates a filesystem in Rook
  33. func (n *NFSOperation) Create(namespace, name string, daemonCount int) error {
  34. logger.Infof("creating the NFS pool")
  35. if err := n.k8sh.ResourceOperation("apply", n.manifests.GetNFSPool()); err != nil {
  36. return err
  37. }
  38. logger.Infof("creating the NFS daemons via CRD")
  39. if err := n.k8sh.ResourceOperation("apply", n.manifests.GetNFS(name, daemonCount)); err != nil {
  40. return err
  41. }
  42. logger.Infof("Make sure rook-ceph-nfs pod is running")
  43. err := n.k8sh.WaitForLabeledPodsToRun(fmt.Sprintf("ceph_nfs=%s", name), namespace)
  44. assert.Nil(n.k8sh.T(), err)
  45. assert.True(n.k8sh.T(), n.k8sh.CheckPodCountAndState("rook-ceph-nfs", namespace, daemonCount, "Running"),
  46. "Make sure all nfs daemon pods are in Running state")
  47. return nil
  48. }
  49. // Delete deletes a filesystem in Rook
  50. func (n *NFSOperation) Delete(namespace, name string) error {
  51. ctx := context.TODO()
  52. options := &metav1.DeleteOptions{}
  53. logger.Infof("Deleting nfs %s in namespace %s", name, namespace)
  54. err := n.k8sh.RookClientset.CephV1().CephNFSes(namespace).Delete(ctx, name, *options)
  55. if err != nil && !errors.IsNotFound(err) {
  56. return err
  57. }
  58. crdCheckerFunc := func() error {
  59. _, err := n.k8sh.RookClientset.CephV1().CephNFSes(namespace).Get(ctx, name, metav1.GetOptions{})
  60. return err
  61. }
  62. logger.Infof("Deleted nfs %s in namespace %s", name, namespace)
  63. err = n.k8sh.WaitForCustomResourceDeletion(namespace, name, crdCheckerFunc)
  64. if err != nil {
  65. return err
  66. }
  67. logger.Infof("Deleting .nfs pool in namespace %s", namespace)
  68. err = n.k8sh.RookClientset.CephV1().CephBlockPools(namespace).Delete(ctx, "dot-nfs", *options)
  69. if err != nil && !errors.IsNotFound(err) {
  70. return err
  71. }
  72. crdCheckerFunc = func() error {
  73. _, err := n.k8sh.RookClientset.CephV1().CephBlockPools(namespace).Get(ctx, "dot-nfs", metav1.GetOptions{})
  74. return err
  75. }
  76. logger.Infof("Deleted .nfs pool %s in namespace %s", name, namespace)
  77. err = n.k8sh.WaitForCustomResourceDeletion(namespace, name, crdCheckerFunc)
  78. if err != nil {
  79. return err
  80. }
  81. logger.Infof("Verified Deletion of nfs %s in namespace %s", name, namespace)
  82. return nil
  83. }
  84. // CreateStorageClass creates a storage class for NFS clients
  85. func (f *NFSOperation) CreateStorageClass(fsName, nfsClusterName, systemNamespace, namespace, storageClassName string) error {
  86. server := fmt.Sprintf("rook-ceph-nfs-%s-a.%s.svc.cluster.local", nfsClusterName, namespace)
  87. return f.k8sh.ResourceOperation("apply", f.manifests.GetNFSStorageClass(fsName, nfsClusterName, server, storageClassName))
  88. }
  89. // CreateSnapshotClass creates a Snapshot class for NFS clients
  90. func (f *NFSOperation) CreateSnapshotClass(fsName, snapshotClassName string) error {
  91. return f.k8sh.ResourceOperation("apply", f.manifests.GetNFSSnapshotClass(fsName, snapshotClassName))
  92. }
  93. // DeleteSnapshotClass deletes a Snapshot class for NFS clients
  94. func (f *NFSOperation) DeleteSnapshotClass(fsName, snapshotClassName string) error {
  95. return f.k8sh.ResourceOperation("delete", f.manifests.GetNFSSnapshotClass(fsName, snapshotClassName))
  96. }