forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.go
164 lines (137 loc) · 4.34 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha2
// AnyUseAllDevices gets whether to use all devices
func (s *StorageScopeSpec) AnyUseAllDevices() bool {
if s.Selection.GetUseAllDevices() {
return true
}
for _, n := range s.Nodes {
if n.Selection.GetUseAllDevices() {
return true
}
}
return false
}
// ClearUseAllDevices clears all devices
func (s *StorageScopeSpec) ClearUseAllDevices() {
clear := false
s.Selection.UseAllDevices = &clear
for i := range s.Nodes {
s.Nodes[i].Selection.UseAllDevices = &clear
}
}
// Fully resolves the config of the given node name, taking into account cluster level and node level specified config.
// In general, the more fine grained the configuration is specified, the more precedence it takes. Fully resolved
// configuration for the node has the following order of precedence.
// 1) Node (config defined on the node itself)
// 2) Cluster (config defined on the cluster)
// 3) Default values (if no config exists for the node or cluster)
func (s *StorageScopeSpec) ResolveNode(nodeName string) *Node {
// find the requested storage node first, if it exists
var node *Node
for i := range s.Nodes {
if s.Nodes[i].Name == nodeName {
node = &(s.Nodes[i])
break
}
}
if node == nil {
// a node with the given name was not found
return nil
}
if node.Config == nil {
node.Config = map[string]string{}
}
// now resolve all properties that haven't already been set on the node
s.resolveNodeSelection(node)
s.resolveNodeConfig(node)
return node
}
func (s *StorageScopeSpec) resolveNodeSelection(node *Node) {
if node.Selection.UseAllDevices == nil {
if s.Selection.UseAllDevices != nil {
// the node does not have a value specified for use all devices, but the cluster does. Use the cluster's.
node.Selection.UseAllDevices = s.Selection.UseAllDevices
} else {
// neither node nor cluster have a value set for use all devices, use the default value.
node.Selection.UseAllDevices = newBool(false)
}
}
resolveString(&(node.Selection.DeviceFilter), s.Selection.DeviceFilter, "")
if len(node.Selection.Devices) == 0 {
node.Selection.Devices = s.Devices
}
if len(node.Selection.Directories) == 0 {
node.Selection.Directories = s.Directories
}
if len(node.Selection.VolumeClaimTemplates) == 0 {
node.Selection.VolumeClaimTemplates = s.VolumeClaimTemplates
}
}
func (s *StorageScopeSpec) resolveNodeConfig(node *Node) {
resolveString(&(node.Location), s.Location, "")
// check for any keys the parent scope has that the node does not
for scopeKey, scopeVal := range s.Config {
if _, ok := node.Config[scopeKey]; !ok {
// the node's config does not have an entry that the parent scope does, add the parent's
// value for that key to the node's config.
node.Config[scopeKey] = scopeVal
}
}
}
// NodeWithNameExists returns true if the storage spec defines a node with the given name.
func (s *StorageScopeSpec) NodeWithNameExists(name string) bool {
for _, n := range s.Nodes {
if name == n.Name {
return true
}
}
return false
}
// GetUseAllDevices return if all devices should be used.
func (s *Selection) GetUseAllDevices() bool {
return s.UseAllDevices != nil && *(s.UseAllDevices)
}
func resolveString(setting *string, parent, defaultVal string) {
if *setting == "" {
if parent != "" {
*setting = parent
} else {
*setting = defaultVal
}
}
}
func resolveInt(setting *int, parent, defaultVal int) {
if *setting == 0 {
if parent != 0 {
*setting = parent
} else {
*setting = defaultVal
}
}
}
func newBool(val bool) *bool {
return &val
}
// NodesByName implements an interface to sort nodes by name
type NodesByName []Node
func (s NodesByName) Len() int {
return len(s)
}
func (s NodesByName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s NodesByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
}