pool.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2020 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 v1
  14. import (
  15. "github.com/pkg/errors"
  16. )
  17. func (p *PoolSpec) IsReplicated() bool {
  18. return p.Replicated.Size > 0
  19. }
  20. func (p *PoolSpec) IsErasureCoded() bool {
  21. return p.ErasureCoded.CodingChunks > 0 || p.ErasureCoded.DataChunks > 0
  22. }
  23. func (p *PoolSpec) IsHybridStoragePool() bool {
  24. return p.Replicated.HybridStorage != nil
  25. }
  26. func (p *PoolSpec) IsCompressionEnabled() bool {
  27. return p.CompressionMode != ""
  28. }
  29. func (p *ReplicatedSpec) IsTargetRatioEnabled() bool {
  30. return p.TargetSizeRatio != 0
  31. }
  32. // ValidateCephBlockPool validates specifically a CephBlockPool's spec (not just any NamedPoolSpec)
  33. func ValidateCephBlockPool(p *CephBlockPool) error {
  34. if p.Spec.Name == "device_health_metrics" || p.Spec.Name == ".mgr" || p.Spec.Name == ".nfs" {
  35. if p.Spec.IsErasureCoded() {
  36. return errors.Errorf("invalid CephBlockPool spec: ceph built-in pool %q cannot be erasure coded", p.Name)
  37. }
  38. }
  39. return validatePoolSpec(p.ToNamedPoolSpec())
  40. }
  41. // validate any NamedPoolSpec
  42. func validatePoolSpec(ps NamedPoolSpec) error {
  43. // Checks if either ErasureCoded or Replicated fields are set
  44. if ps.ErasureCoded.CodingChunks <= 0 && ps.ErasureCoded.DataChunks <= 0 && ps.Replicated.TargetSizeRatio <= 0 && ps.Replicated.Size <= 0 {
  45. return errors.New("invalid pool spec: either of erasurecoded or replicated fields should be set")
  46. }
  47. // Check if any of the ErasureCoded fields are populated. Then check if replicated is populated. Both can't be populated at same time.
  48. if ps.ErasureCoded.CodingChunks > 0 || ps.ErasureCoded.DataChunks > 0 || ps.ErasureCoded.Algorithm != "" {
  49. if ps.Replicated.Size > 0 || ps.Replicated.TargetSizeRatio > 0 {
  50. return errors.New("invalid pool spec: both erasurecoded and replicated fields cannot be set at the same time")
  51. }
  52. }
  53. if ps.Replicated.Size == 0 && ps.Replicated.TargetSizeRatio == 0 {
  54. // Check if datachunks is set and has value less than 2.
  55. if ps.ErasureCoded.DataChunks < 2 && ps.ErasureCoded.DataChunks != 0 {
  56. return errors.New("invalid pool spec: erasurecoded.datachunks needs minimum value of 2")
  57. }
  58. // Check if codingchunks is set and has value less than 1.
  59. if ps.ErasureCoded.CodingChunks < 1 && ps.ErasureCoded.CodingChunks != 0 {
  60. return errors.New("invalid pool spec: erasurecoded.codingchunks needs minimum value of 1")
  61. }
  62. }
  63. return nil
  64. }
  65. func (p *CephBlockPool) ToNamedPoolSpec() NamedPoolSpec {
  66. // If the name is not overridden in the pool spec.name, set it to the name of the pool CR
  67. name := p.Spec.Name
  68. if name == "" {
  69. // Set the name of the pool CR since a name override wasn't specified in the spec
  70. name = p.Name
  71. }
  72. return NamedPoolSpec{
  73. Name: name,
  74. PoolSpec: p.Spec.PoolSpec,
  75. }
  76. }
  77. func (p *CephBlockPool) GetStatusConditions() *[]Condition {
  78. return &p.Status.Conditions
  79. }
  80. // SnapshotSchedulesEnabled returns whether snapshot schedules are desired
  81. func (p *MirroringSpec) SnapshotSchedulesEnabled() bool {
  82. return len(p.SnapshotSchedules) > 0
  83. }