radosnamespace.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2022 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 client
  14. import (
  15. "encoding/json"
  16. "syscall"
  17. "github.com/pkg/errors"
  18. "github.com/rook/rook/pkg/clusterd"
  19. "github.com/rook/rook/pkg/util/exec"
  20. )
  21. // CreateRadosNamespace create a rados namespace in a pool.
  22. // poolName is the name of the ceph block pool, the same as the CephBlockPool CR name.
  23. func CreateRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, namespaceName string) error {
  24. logger.Infof("creating rados namespace %s/%s in k8s namespace %q", poolName, namespaceName, clusterInfo.Namespace)
  25. // rbd namespace create pool-name/namespace-name
  26. args := []string{"namespace", "create", "--pool", poolName, "--namespace", namespaceName}
  27. cmd := NewRBDCommand(context, clusterInfo, args)
  28. cmd.JsonOutput = false
  29. output, err := cmd.Run()
  30. if err != nil {
  31. code, ok := exec.ExitStatus(err)
  32. if ok && code == int(syscall.EEXIST) {
  33. logger.Debugf("rados namespace %s/%s in k8s namespace %q already exists", poolName, namespaceName, clusterInfo.Namespace)
  34. return nil
  35. }
  36. return errors.Wrapf(err, "failed to create rados namespace %s/%s. %s", poolName, namespaceName, output)
  37. }
  38. logger.Infof("successfully created rados namespace %s/%s in k8s namespace %q", poolName, namespaceName, clusterInfo.Namespace)
  39. return nil
  40. }
  41. func getRadosNamespaceStatistics(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, namespaceName string) (*PoolStatistics, error) {
  42. var poolStats PoolStatistics
  43. args := []string{"pool", "stats", "--pool", poolName, "--namespace", namespaceName}
  44. cmd := NewRBDCommand(context, clusterInfo, args)
  45. cmd.JsonOutput = true
  46. output, err := cmd.Run()
  47. if err != nil {
  48. code, ok := exec.ExitStatus(err)
  49. if ok && code == int(syscall.ENOENT) {
  50. return &poolStats, nil
  51. }
  52. return nil, errors.Wrapf(err, "failed to get pool stats. %s", string(output))
  53. }
  54. if err := json.Unmarshal(output, &poolStats); err != nil {
  55. return nil, errors.Wrap(err, "failed to unmarshal pool stats response")
  56. }
  57. return &poolStats, nil
  58. }
  59. func checkForImagesInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, namespaceName string) error {
  60. logger.Debugf("checking any images/snapshots present in pool %s/%s in k8s namespace %q", poolName, namespaceName, clusterInfo.Namespace)
  61. stats, err := getRadosNamespaceStatistics(context, clusterInfo, poolName, namespaceName)
  62. if err != nil {
  63. return errors.Wrapf(err, "failed to list images/snapshots in pool %s/%s", poolName, namespaceName)
  64. }
  65. if stats.Images.Count == 0 && stats.Images.SnapCount == 0 {
  66. logger.Infof("no images/snapshots present in pool %s/%s in k8s namespace %q", poolName, namespaceName, clusterInfo.Namespace)
  67. return nil
  68. }
  69. return errors.Errorf("pool %s/%s contains %d images and %d snapshots", poolName, namespaceName, stats.Images.Count, stats.Images.SnapCount)
  70. }
  71. // DeleteRadosNamespace delete a rados namespace.
  72. func DeleteRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, namespaceName string) error {
  73. err := checkForImagesInRadosNamespace(context, clusterInfo, poolName, namespaceName)
  74. if err != nil {
  75. return errors.Wrapf(err, "failed to check if pool %s/%s has rbd images", poolName, namespaceName)
  76. }
  77. logger.Infof("deleting rados namespace %s/%s in k8s namespace %q", poolName, namespaceName, clusterInfo.Namespace)
  78. args := []string{"namespace", "remove", "--pool", poolName, "--namespace", namespaceName}
  79. cmd := NewRBDCommand(context, clusterInfo, args)
  80. cmd.JsonOutput = false
  81. output, err := cmd.Run()
  82. if err != nil {
  83. code, ok := exec.ExitStatus(err)
  84. if !ok || code != int(syscall.ENOENT) {
  85. return errors.Wrapf(err, "failed to delete rados namespace %s/%s. %s", poolName, namespaceName, output)
  86. }
  87. }
  88. logger.Infof("successfully deleted rados namespace %s/%s in k8s namespace %q", poolName, namespaceName, clusterInfo.Namespace)
  89. return nil
  90. }