block.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "github.com/rook/rook/pkg/daemon/ceph/client"
  18. "github.com/rook/rook/tests/framework/installer"
  19. "github.com/rook/rook/tests/framework/utils"
  20. "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. )
  23. // BlockOperation is wrapper for k8s rook block operations
  24. type BlockOperation struct {
  25. k8sClient *utils.K8sHelper
  26. manifests installer.CephManifests
  27. }
  28. type BlockImage struct {
  29. Name string `json:"imageName"`
  30. PoolName string `json:"poolName"`
  31. Size uint64 `json:"size"`
  32. Device string `json:"device"`
  33. MountPoint string `json:"mountPoint"`
  34. }
  35. // CreateBlockOperation - Constructor to create BlockOperation - client to perform rook Block operations on k8s
  36. func CreateBlockOperation(k8shelp *utils.K8sHelper, manifests installer.CephManifests) *BlockOperation {
  37. return &BlockOperation{k8shelp, manifests}
  38. }
  39. // BlockCreate Function to create a Block using Rook
  40. // Input parameters -
  41. // manifest - pod definition that creates a PVC in k8s - YAML should describe the name and size of the PVC being created
  42. // size - not user for k8s implementation since it is described in the PVC YAML definition
  43. // Output - k8s create PVC operation output and/or error
  44. func (b *BlockOperation) Create(manifest string, size int) (string, error) {
  45. args := []string{"apply", "-f", "-"}
  46. result, err := b.k8sClient.KubectlWithStdin(manifest, args...)
  47. if err != nil {
  48. return "", fmt.Errorf("Unable to create block -- : %s", err)
  49. }
  50. return result, nil
  51. }
  52. func (b *BlockOperation) CreatePoolAndStorageClass(pvcNamespace, poolName, storageClassName, reclaimPolicy string) error {
  53. if err := b.k8sClient.ResourceOperation("apply", b.manifests.GetBlockPool(poolName, "1")); err != nil {
  54. return err
  55. }
  56. return b.k8sClient.ResourceOperation("apply", b.manifests.GetBlockStorageClass(poolName, storageClassName, reclaimPolicy))
  57. }
  58. func (b *BlockOperation) CreatePVC(namespace, claimName, storageClassName, mode, size string) error {
  59. return b.k8sClient.ResourceOperation("apply", installer.GetPVC(claimName, namespace, storageClassName, mode, size))
  60. }
  61. func (b *BlockOperation) CreatePod(podName, claimName, namespace, mountPoint string, readOnly bool) error {
  62. return b.k8sClient.ResourceOperation("apply", installer.GetPodWithVolume(podName, claimName, namespace, mountPoint, readOnly))
  63. }
  64. func (b *BlockOperation) CreateStorageClass(csi bool, poolName, storageClassName, reclaimPolicy, namespace string) error {
  65. return b.k8sClient.ResourceOperation("apply", b.manifests.GetBlockStorageClass(poolName, storageClassName, reclaimPolicy))
  66. }
  67. func (b *BlockOperation) DeletePVC(namespace, claimName string) error {
  68. ctx := context.TODO()
  69. logger.Infof("deleting pvc %q from namespace %q", claimName, namespace)
  70. return b.k8sClient.Clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(ctx, claimName, metav1.DeleteOptions{})
  71. }
  72. func (b *BlockOperation) CreatePVCRestore(namespace, claimName, snapshotName, storageClassName, mode, size string) error {
  73. return b.k8sClient.ResourceOperation("apply", installer.GetPVCRestore(claimName, snapshotName, namespace, storageClassName, mode, size))
  74. }
  75. func (b *BlockOperation) CreatePVCClone(namespace, cloneClaimName, parentClaimName, storageClassName, mode, size string) error {
  76. return b.k8sClient.ResourceOperation("apply", installer.GetPVCClone(cloneClaimName, parentClaimName, namespace, storageClassName, mode, size))
  77. }
  78. func (b *BlockOperation) CreateSnapshotClass(snapshotClassName, deletePolicy, namespace string) error {
  79. return b.k8sClient.ResourceOperation("apply", b.manifests.GetBlockSnapshotClass(snapshotClassName, deletePolicy))
  80. }
  81. func (b *BlockOperation) DeleteSnapshotClass(snapshotClassName, deletePolicy, namespace string) error {
  82. return b.k8sClient.ResourceOperation("delete", b.manifests.GetBlockSnapshotClass(snapshotClassName, deletePolicy))
  83. }
  84. func (b *BlockOperation) CreateSnapshot(snapshotName, claimName, snapshotClassName, namespace string) error {
  85. return b.k8sClient.ResourceOperation("apply", installer.GetSnapshot(snapshotName, claimName, snapshotClassName, namespace))
  86. }
  87. func (b *BlockOperation) DeleteSnapshot(snapshotName, claimName, snapshotClassName, namespace string) error {
  88. return b.k8sClient.ResourceOperation("delete", installer.GetSnapshot(snapshotName, claimName, snapshotClassName, namespace))
  89. }
  90. func (b *BlockOperation) DeleteStorageClass(storageClassName string) error {
  91. ctx := context.TODO()
  92. logger.Infof("deleting storage class %q", storageClassName)
  93. err := b.k8sClient.Clientset.StorageV1().StorageClasses().Delete(ctx, storageClassName, metav1.DeleteOptions{})
  94. if err != nil && !errors.IsNotFound(err) {
  95. return fmt.Errorf("failed to delete storage class %q. %v", storageClassName, err)
  96. }
  97. return nil
  98. }
  99. // BlockDelete Function to delete a Block using Rook
  100. // Input parameters -
  101. // manifest - pod definition where pvc is described - delete is run on the yaml definition
  102. // Output - k8s delete pvc operation output and/or error
  103. func (b *BlockOperation) DeleteBlock(manifest string) (string, error) {
  104. args := []string{"delete", "-f", "-"}
  105. result, err := b.k8sClient.KubectlWithStdin(manifest, args...)
  106. if err != nil {
  107. return "", fmt.Errorf("Unable to delete block -- : %s", err)
  108. }
  109. return result, nil
  110. }
  111. // List Function to list all the block images in all pools
  112. func (b *BlockOperation) ListAllImages(clusterInfo *client.ClusterInfo) ([]BlockImage, error) {
  113. // first list all the pools so that we can retrieve images from all pools
  114. pools, err := client.ListPoolSummaries(b.k8sClient.MakeContext(), clusterInfo)
  115. if err != nil {
  116. return nil, fmt.Errorf("failed to list pools: %+v", err)
  117. }
  118. // for each pool, get further details about all the images in the pool
  119. images := []BlockImage{}
  120. for _, p := range pools {
  121. cephImages, err := b.ListImagesInPool(clusterInfo, p.Name)
  122. if err != nil {
  123. return nil, fmt.Errorf("failed to get images from pool %s: %+v", p.Name, err)
  124. }
  125. images = append(images, cephImages...)
  126. }
  127. return images, nil
  128. }
  129. // List Function to list all the block images in a pool
  130. func (b *BlockOperation) ListImagesInPool(clusterInfo *client.ClusterInfo, poolName string) ([]BlockImage, error) {
  131. // for each pool, get further details about all the images in the pool
  132. images := []BlockImage{}
  133. cephImages, err := client.ListImages(b.k8sClient.MakeContext(), clusterInfo, poolName)
  134. if err != nil {
  135. return nil, fmt.Errorf("failed to get images from pool %s: %+v", poolName, err)
  136. }
  137. for _, image := range cephImages {
  138. // add the current image's details to the result set
  139. newImage := BlockImage{
  140. Name: image.Name,
  141. PoolName: poolName,
  142. Size: image.Size,
  143. }
  144. images = append(images, newImage)
  145. }
  146. return images, nil
  147. }
  148. // DeleteBlockImage Function to list all the blocks created/being managed by rook
  149. func (b *BlockOperation) DeleteBlockImage(clusterInfo *client.ClusterInfo, image BlockImage) error {
  150. context := b.k8sClient.MakeContext()
  151. return client.DeleteImage(context, clusterInfo, image.Name, image.PoolName)
  152. }
  153. // CreateClientPod starts a pod that should have a block PVC.
  154. func (b *BlockOperation) CreateClientPod(manifest string) error {
  155. return b.k8sClient.ResourceOperation("apply", manifest)
  156. }