storage.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2018 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 "fmt"
  15. type StoreType string
  16. const (
  17. // StoreTypeBlueStore is the bluestore backend storage for OSDs
  18. StoreTypeBlueStore StoreType = "bluestore"
  19. // StoreTypeBlueStoreRDR is the bluestore-rdr backed storage for OSDs
  20. StoreTypeBlueStoreRDR StoreType = "bluestore-rdr"
  21. )
  22. // AnyUseAllDevices gets whether to use all devices
  23. func (s *StorageScopeSpec) AnyUseAllDevices() bool {
  24. if s.Selection.GetUseAllDevices() {
  25. return true
  26. }
  27. for _, n := range s.Nodes {
  28. if n.Selection.GetUseAllDevices() {
  29. return true
  30. }
  31. }
  32. return false
  33. }
  34. // ClearUseAllDevices clears all devices
  35. func (s *StorageScopeSpec) ClearUseAllDevices() {
  36. clear := false
  37. s.Selection.UseAllDevices = &clear
  38. for i := range s.Nodes {
  39. s.Nodes[i].Selection.UseAllDevices = &clear
  40. }
  41. }
  42. // NodeExists returns true if the node exists in the storage spec. False otherwise.
  43. func (s *StorageScopeSpec) NodeExists(nodeName string) bool {
  44. for i := range s.Nodes {
  45. if s.Nodes[i].Name == nodeName {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. // Fully resolves the config of the given node name, taking into account cluster level and node level specified config.
  52. // In general, the more fine grained the configuration is specified, the more precedence it takes. Fully resolved
  53. // configuration for the node has the following order of precedence.
  54. // 1) Node (config defined on the node itself)
  55. // 2) Cluster (config defined on the cluster)
  56. // 3) Default values (if no config exists for the node or cluster)
  57. func (s *StorageScopeSpec) ResolveNode(nodeName string) *Node {
  58. // find the requested storage node first, if it exists
  59. var node *Node
  60. for i := range s.Nodes {
  61. if s.Nodes[i].Name == nodeName {
  62. node = &(s.Nodes[i])
  63. break
  64. }
  65. }
  66. if node == nil {
  67. // a node with the given name was not found
  68. return nil
  69. }
  70. if node.Config == nil {
  71. node.Config = map[string]string{}
  72. }
  73. // now resolve all properties that haven't already been set on the node
  74. s.resolveNodeSelection(node)
  75. s.resolveNodeConfig(node)
  76. return node
  77. }
  78. func (s *StorageScopeSpec) resolveNodeSelection(node *Node) {
  79. if node.Selection.UseAllDevices == nil {
  80. if s.Selection.UseAllDevices != nil {
  81. // the node does not have a value specified for use all devices, but the cluster does. Use the cluster's.
  82. node.Selection.UseAllDevices = s.Selection.UseAllDevices
  83. } else {
  84. // neither node nor cluster have a value set for use all devices, use the default value.
  85. node.Selection.UseAllDevices = newBool(false)
  86. }
  87. }
  88. resolveString(&(node.Selection.DeviceFilter), s.Selection.DeviceFilter, "")
  89. resolveString(&(node.Selection.DevicePathFilter), s.Selection.DevicePathFilter, "")
  90. if len(node.Selection.Devices) == 0 {
  91. node.Selection.Devices = s.Devices
  92. }
  93. if len(node.Selection.VolumeClaimTemplates) == 0 {
  94. node.Selection.VolumeClaimTemplates = s.VolumeClaimTemplates
  95. }
  96. }
  97. func (s *StorageScopeSpec) resolveNodeConfig(node *Node) {
  98. // check for any keys the parent scope has that the node does not
  99. for scopeKey, scopeVal := range s.Config {
  100. if _, ok := node.Config[scopeKey]; !ok {
  101. // the node's config does not have an entry that the parent scope does, add the parent's
  102. // value for that key to the node's config.
  103. node.Config[scopeKey] = scopeVal
  104. }
  105. }
  106. }
  107. // NodeWithNameExists returns true if the storage spec defines a node with the given name.
  108. func (s *StorageScopeSpec) NodeWithNameExists(name string) bool {
  109. for _, n := range s.Nodes {
  110. if name == n.Name {
  111. return true
  112. }
  113. }
  114. return false
  115. }
  116. // GetUseAllDevices return if all devices should be used.
  117. func (s *Selection) GetUseAllDevices() bool {
  118. return s.UseAllDevices != nil && *(s.UseAllDevices)
  119. }
  120. func resolveString(setting *string, parent, defaultVal string) {
  121. if *setting == "" {
  122. if parent != "" {
  123. *setting = parent
  124. } else {
  125. *setting = defaultVal
  126. }
  127. }
  128. }
  129. func newBool(val bool) *bool {
  130. return &val
  131. }
  132. // NodesByName implements an interface to sort nodes by name
  133. type NodesByName []Node
  134. func (s NodesByName) Len() int {
  135. return len(s)
  136. }
  137. func (s NodesByName) Swap(i, j int) {
  138. s[i], s[j] = s[j], s[i]
  139. }
  140. func (s NodesByName) Less(i, j int) bool {
  141. return s[i].Name < s[j].Name
  142. }
  143. // IsOnPVCEncrypted returns whether a Ceph Cluster on PVC will be encrypted
  144. func (s *StorageScopeSpec) IsOnPVCEncrypted() bool {
  145. for _, storageClassDeviceSet := range s.StorageClassDeviceSets {
  146. if storageClassDeviceSet.Encrypted {
  147. return true
  148. }
  149. }
  150. return false
  151. }
  152. // GetOSDStore returns osd backend store type provided in the cluster spec
  153. func (s *StorageScopeSpec) GetOSDStore() string {
  154. if s.Store.Type == "" {
  155. return string(StoreTypeBlueStore)
  156. }
  157. return s.Store.Type
  158. }
  159. // GetOSDStoreFlag returns osd backend store type prefixed with "--"
  160. func (s *StorageScopeSpec) GetOSDStoreFlag() string {
  161. if s.Store.Type == "" {
  162. return fmt.Sprintf("--%s", StoreTypeBlueStore)
  163. }
  164. return fmt.Sprintf("--%s", s.Store.Type)
  165. }