pool.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "strconv"
  18. "time"
  19. cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
  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. "k8s.io/apimachinery/pkg/api/errors"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. )
  26. // PoolOperation is a wrapper for rook pool operations
  27. type PoolOperation struct {
  28. k8sh *utils.K8sHelper
  29. manifests installer.CephManifests
  30. }
  31. // CreatePoolOperation creates a new pool client
  32. func CreatePoolOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *PoolOperation {
  33. return &PoolOperation{k8sh, manifests}
  34. }
  35. func (p *PoolOperation) Create(name, namespace string, replicas int) error {
  36. return p.createOrUpdatePool(name, namespace, "apply", replicas)
  37. }
  38. func (p *PoolOperation) Update(name, namespace string, replicas int) error {
  39. return p.createOrUpdatePool(name, namespace, "apply", replicas)
  40. }
  41. func (p *PoolOperation) createOrUpdatePool(name, namespace, action string, replicas int) error {
  42. return p.k8sh.ResourceOperation(action, p.manifests.GetBlockPool(name, strconv.Itoa(replicas)))
  43. }
  44. func (p *PoolOperation) ListCephPools(clusterInfo *client.ClusterInfo) ([]client.CephStoragePoolSummary, error) {
  45. context := p.k8sh.MakeContext()
  46. pools, err := client.ListPoolSummaries(context, clusterInfo)
  47. if err != nil {
  48. return nil, fmt.Errorf("failed to list pools: %+v", err)
  49. }
  50. return pools, nil
  51. }
  52. func (p *PoolOperation) GetCephPoolDetails(clusterInfo *client.ClusterInfo, name string) (client.CephStoragePoolDetails, error) {
  53. context := p.k8sh.MakeContext()
  54. details, err := client.GetPoolDetails(context, clusterInfo, name)
  55. if err != nil {
  56. return client.CephStoragePoolDetails{}, fmt.Errorf("failed to get pool %s details: %+v", name, err)
  57. }
  58. return details, nil
  59. }
  60. func (p *PoolOperation) ListPoolCRDs(namespace string) ([]cephv1.CephBlockPool, error) {
  61. ctx := context.TODO()
  62. pools, err := p.k8sh.RookClientset.CephV1().CephBlockPools(namespace).List(ctx, metav1.ListOptions{})
  63. if err != nil {
  64. if errors.IsNotFound(err) {
  65. return nil, nil
  66. }
  67. return nil, err
  68. }
  69. return pools.Items, nil
  70. }
  71. func (p *PoolOperation) PoolCRDExists(namespace, name string) (bool, error) {
  72. ctx := context.TODO()
  73. _, err := p.k8sh.RookClientset.CephV1().CephBlockPools(namespace).Get(ctx, name, metav1.GetOptions{})
  74. if err != nil {
  75. if errors.IsNotFound(err) {
  76. return false, nil
  77. }
  78. return false, err
  79. }
  80. return true, nil
  81. }
  82. func (p *PoolOperation) CephPoolExists(namespace, name string) (bool, error) {
  83. clusterInfo := client.AdminTestClusterInfo(namespace)
  84. pools, err := p.ListCephPools(clusterInfo)
  85. if err != nil {
  86. return false, err
  87. }
  88. for _, pool := range pools {
  89. if pool.Name == name {
  90. return true, nil
  91. }
  92. }
  93. return false, nil
  94. }
  95. // DeletePool deletes a pool after deleting all the block images contained by the pool
  96. func (p *PoolOperation) DeletePool(blockClient *BlockOperation, clusterInfo *client.ClusterInfo, poolName string) error {
  97. ctx := context.TODO()
  98. // Delete all the images in a pool
  99. logger.Infof("listing images in pool %q", poolName)
  100. blockImagesList, _ := blockClient.ListImagesInPool(clusterInfo, poolName)
  101. for _, blockImage := range blockImagesList {
  102. logger.Infof("force deleting block image %q in pool %q", blockImage, poolName)
  103. max := 10
  104. // Wait and retry up to 10 times/seconds to delete RBD images
  105. for i := 0; i < max; i++ {
  106. err := blockClient.DeleteBlockImage(clusterInfo, blockImage)
  107. if err == nil {
  108. break
  109. }
  110. logger.Infof("failed deleting image %q from %q. %v", blockImage, poolName, err)
  111. time.Sleep(2 * time.Second)
  112. if i == max-1 {
  113. return fmt.Errorf("gave up waiting for image %q from %q to be deleted. %v", blockImage, poolName, err)
  114. }
  115. }
  116. }
  117. logger.Infof("deleting pool CR %q", poolName)
  118. err := p.k8sh.RookClientset.CephV1().CephBlockPools(clusterInfo.Namespace).Delete(ctx, poolName, metav1.DeleteOptions{})
  119. if err != nil {
  120. if errors.IsNotFound(err) {
  121. return nil
  122. }
  123. return fmt.Errorf("failed to delete pool CR. %v", err)
  124. }
  125. crdCheckerFunc := func() error {
  126. _, err := p.k8sh.RookClientset.CephV1().CephBlockPools(clusterInfo.Namespace).Get(ctx, poolName, metav1.GetOptions{})
  127. return err
  128. }
  129. return p.k8sh.WaitForCustomResourceDeletion(clusterInfo.Namespace, poolName, crdCheckerFunc)
  130. }