|
1 | 1 | package metal |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "path/filepath" |
6 | 7 | "slices" |
@@ -142,28 +143,28 @@ func (sz Sizes) FromHardware(hardware MachineHardware) (*Size, error) { |
142 | 143 | nextsize: |
143 | 144 | for _, s := range sz { |
144 | 145 | for _, c := range s.Constraints { |
145 | | - match := c.matches(hardware) |
146 | | - if !match { |
| 146 | + if !c.matches(hardware) { |
147 | 147 | continue nextsize |
148 | 148 | } |
149 | 149 | } |
150 | 150 |
|
151 | 151 | for _, ct := range allConstraintTypes { |
152 | | - match := hardware.matches(s.Constraints, ct) |
153 | | - if !match { |
| 152 | + if !hardware.matches(s.Constraints, ct) { |
154 | 153 | continue nextsize |
155 | 154 | } |
156 | 155 | } |
| 156 | + |
157 | 157 | matchedSizes = append(matchedSizes, s) |
158 | 158 | } |
159 | 159 |
|
160 | | - if len(matchedSizes) == 0 { |
| 160 | + switch len(matchedSizes) { |
| 161 | + case 0: |
161 | 162 | return nil, NotFound("no size found for hardware (%s)", hardware.ReadableSpec()) |
162 | | - } |
163 | | - if len(matchedSizes) > 1 { |
| 163 | + case 1: |
| 164 | + return &matchedSizes[0], nil |
| 165 | + default: |
164 | 166 | return nil, fmt.Errorf("%d sizes found for hardware (%s)", len(matchedSizes), hardware.ReadableSpec()) |
165 | 167 | } |
166 | | - return &matchedSizes[0], nil |
167 | 168 | } |
168 | 169 |
|
169 | 170 | func (s *Size) overlaps(so *Size) bool { |
@@ -210,44 +211,64 @@ func (c *Constraint) overlaps(other Constraint) bool { |
210 | 211 | if c.Max < other.Min { |
211 | 212 | return false |
212 | 213 | } |
| 214 | + |
213 | 215 | return true |
214 | 216 | } |
215 | 217 |
|
216 | | -// Validate a size, returns error if a invalid size is passed |
217 | | -func (s *Size) Validate(partitions PartitionMap, projects map[string]*mdmv1.Project) error { |
218 | | - constraintTypes := map[ConstraintType]uint{} |
219 | | - for _, c := range s.Constraints { |
220 | | - if c.Max < c.Min { |
221 | | - return fmt.Errorf("size:%q type:%q max:%d is smaller than min:%d", s.ID, c.Type, c.Max, c.Min) |
222 | | - } |
| 218 | +func (c *Constraint) validate() error { |
| 219 | + if c.Max < c.Min { |
| 220 | + return fmt.Errorf("max is smaller than min") |
| 221 | + } |
223 | 222 |
|
224 | | - // CPU and Memory Constraints are not allowed more than once |
225 | | - constraintTypes[c.Type]++ |
226 | | - count := constraintTypes[c.Type] |
227 | | - if c.Type == CoreConstraint || c.Type == MemoryConstraint { |
228 | | - if count > 1 { |
229 | | - return fmt.Errorf("size:%q type:%q min:%d max:%d has duplicate constraint type", s.ID, c.Type, c.Min, c.Max) |
230 | | - } |
231 | | - } |
| 223 | + if _, err := filepath.Match(c.Identifier, ""); err != nil { |
| 224 | + return fmt.Errorf("identifier is malformed: %w", err) |
| 225 | + } |
232 | 226 |
|
233 | | - // Ensure GPU Constraints always have identifier specified |
234 | | - if c.Type == GPUConstraint && c.Identifier == "" { |
235 | | - return fmt.Errorf("size:%q type:%q min:%d max:%d is a gpu size but has no identifier specified", s.ID, c.Type, c.Min, c.Max) |
| 227 | + switch t := c.Type; t { |
| 228 | + case GPUConstraint: |
| 229 | + if c.Identifier == "" { |
| 230 | + return fmt.Errorf("for gpu constraints an identifier is required") |
236 | 231 | } |
237 | | - |
238 | | - // Ensure Memory Constraints do not have a identifier specified |
239 | | - if c.Type == MemoryConstraint && c.Identifier != "" { |
240 | | - return fmt.Errorf("size:%q type:%q min:%d max:%d is a memory size but has a identifier specified", s.ID, c.Type, c.Min, c.Max) |
| 232 | + case MemoryConstraint: |
| 233 | + if c.Identifier != "" { |
| 234 | + return fmt.Errorf("for memory constraints an identifier is not allowed") |
241 | 235 | } |
| 236 | + case CoreConstraint, StorageConstraint: |
| 237 | + } |
| 238 | + |
| 239 | + return nil |
| 240 | +} |
| 241 | + |
| 242 | +// Validate a size, returns error if a invalid size is passed |
| 243 | +func (s *Size) Validate(partitions PartitionMap, projects map[string]*mdmv1.Project) error { |
| 244 | + var ( |
| 245 | + errs []error |
| 246 | + typeCounts = map[ConstraintType]uint{} |
| 247 | + ) |
242 | 248 |
|
243 | | - if _, err := filepath.Match(c.Identifier, ""); err != nil { |
244 | | - return fmt.Errorf("size:%q type:%q min:%d max:%d identifier:%q identifier is malformed:%w", s.ID, c.Type, c.Min, c.Max, c.Identifier, err) |
| 249 | + for i, c := range s.Constraints { |
| 250 | + typeCounts[c.Type]++ |
| 251 | + |
| 252 | + err := c.validate() |
| 253 | + if err != nil { |
| 254 | + errs = append(errs, fmt.Errorf("constraint at index %d is invalid: %w", i, err)) |
245 | 255 | } |
246 | 256 |
|
| 257 | + switch t := c.Type; t { |
| 258 | + case GPUConstraint, StorageConstraint: |
| 259 | + case MemoryConstraint, CoreConstraint: |
| 260 | + if typeCounts[t] > 1 { |
| 261 | + errs = append(errs, fmt.Errorf("constraint at index %d is invalid: type duplicates are not allowed for type %q", i, t)) |
| 262 | + } |
| 263 | + } |
247 | 264 | } |
248 | 265 |
|
249 | 266 | if err := s.Reservations.Validate(partitions, projects); err != nil { |
250 | | - return fmt.Errorf("invalid size reservation: %w", err) |
| 267 | + errs = append(errs, fmt.Errorf("size reservations are invalid: %w", err)) |
| 268 | + } |
| 269 | + |
| 270 | + if len(errs) > 0 { |
| 271 | + return fmt.Errorf("size %q is invalid: %w", s.ID, errors.Join(errs...)) |
251 | 272 | } |
252 | 273 |
|
253 | 274 | return nil |
|
0 commit comments