Skip to content

Commit dfbc4a0

Browse files
authored
Cleanup of now unused func with little refactoring. (#530)
1 parent 9a03d83 commit dfbc4a0

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

cmd/metal-api/internal/metal/machine.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -535,19 +535,6 @@ func exhaustiveMatch[V comparable](cs []Constraint, vs []V, countFn func(v V) (m
535535
return len(unmatched) == 0
536536
}
537537

538-
func (hw *MachineHardware) GPUModels() map[string]uint64 {
539-
models := make(map[string]uint64)
540-
for _, gpu := range hw.MetalGPUs {
541-
_, ok := models[gpu.Model]
542-
if !ok {
543-
models[gpu.Model] = 1
544-
} else {
545-
models[gpu.Model]++
546-
}
547-
}
548-
return models
549-
}
550-
551538
// ReadableSpec returns a human readable string for the hardware.
552539
func (hw *MachineHardware) ReadableSpec() string {
553540
diskCapacity, _ := capacityOf("*", hw.Disks, countDisk)

cmd/metal-api/internal/metal/size.go

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package metal
22

33
import (
4+
"errors"
45
"fmt"
56
"path/filepath"
67
"slices"
@@ -142,28 +143,28 @@ func (sz Sizes) FromHardware(hardware MachineHardware) (*Size, error) {
142143
nextsize:
143144
for _, s := range sz {
144145
for _, c := range s.Constraints {
145-
match := c.matches(hardware)
146-
if !match {
146+
if !c.matches(hardware) {
147147
continue nextsize
148148
}
149149
}
150150

151151
for _, ct := range allConstraintTypes {
152-
match := hardware.matches(s.Constraints, ct)
153-
if !match {
152+
if !hardware.matches(s.Constraints, ct) {
154153
continue nextsize
155154
}
156155
}
156+
157157
matchedSizes = append(matchedSizes, s)
158158
}
159159

160-
if len(matchedSizes) == 0 {
160+
switch len(matchedSizes) {
161+
case 0:
161162
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:
164166
return nil, fmt.Errorf("%d sizes found for hardware (%s)", len(matchedSizes), hardware.ReadableSpec())
165167
}
166-
return &matchedSizes[0], nil
167168
}
168169

169170
func (s *Size) overlaps(so *Size) bool {
@@ -210,44 +211,64 @@ func (c *Constraint) overlaps(other Constraint) bool {
210211
if c.Max < other.Min {
211212
return false
212213
}
214+
213215
return true
214216
}
215217

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+
}
223222

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+
}
232226

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")
236231
}
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")
241235
}
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+
)
242248

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))
245255
}
246256

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+
}
247264
}
248265

249266
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...))
251272
}
252273

253274
return nil

cmd/metal-api/internal/metal/size_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ func TestSize_Validate(t *testing.T) {
10431043
},
10441044
},
10451045
},
1046-
wantErrMessage: pointer.Pointer("size:\"broken-cpu-size\" type:\"cores\" max:2 is smaller than min:8"),
1046+
wantErrMessage: pointer.Pointer("size \"broken-cpu-size\" is invalid: constraint at index 0 is invalid: max is smaller than min"),
10471047
},
10481048
{
10491049
name: "memory min and max wrong",
@@ -1059,7 +1059,7 @@ func TestSize_Validate(t *testing.T) {
10591059
},
10601060
},
10611061
},
1062-
wantErrMessage: pointer.Pointer("size:\"broken-memory-size\" type:\"memory\" max:2 is smaller than min:8"),
1062+
wantErrMessage: pointer.Pointer("size \"broken-memory-size\" is invalid: constraint at index 0 is invalid: max is smaller than min"),
10631063
},
10641064
{
10651065
name: "storage is working",
@@ -1121,7 +1121,7 @@ func TestSize_Validate(t *testing.T) {
11211121
},
11221122
},
11231123
},
1124-
wantErrMessage: pointer.Pointer("size:\"cpu-size\" type:\"cores\" min:2 max:2 has duplicate constraint type"),
1124+
wantErrMessage: pointer.Pointer("size \"cpu-size\" is invalid: constraint at index 1 is invalid: type duplicates are not allowed for type \"cores\""),
11251125
},
11261126
{
11271127
name: "gpu size without identifier",
@@ -1137,7 +1137,7 @@ func TestSize_Validate(t *testing.T) {
11371137
},
11381138
},
11391139
},
1140-
wantErrMessage: pointer.Pointer("size:\"invalid-gpu-size\" type:\"gpu\" min:2 max:8 is a gpu size but has no identifier specified"),
1140+
wantErrMessage: pointer.Pointer("size \"invalid-gpu-size\" is invalid: constraint at index 0 is invalid: for gpu constraints an identifier is required"),
11411141
},
11421142
{
11431143
name: "storage with invalid identifier",
@@ -1154,7 +1154,7 @@ func TestSize_Validate(t *testing.T) {
11541154
},
11551155
},
11561156
},
1157-
wantErrMessage: pointer.Pointer("size:\"invalid-storage-size\" type:\"storage\" min:2 max:8 identifier:\"][\" identifier is malformed:syntax error in pattern"),
1157+
wantErrMessage: pointer.Pointer("size \"invalid-storage-size\" is invalid: constraint at index 0 is invalid: identifier is malformed: syntax error in pattern"),
11581158
},
11591159
{
11601160
name: "memory with identifier",
@@ -1171,7 +1171,7 @@ func TestSize_Validate(t *testing.T) {
11711171
},
11721172
},
11731173
},
1174-
wantErrMessage: pointer.Pointer("size:\"invalid-memory-size\" type:\"memory\" min:2 max:8 is a memory size but has a identifier specified"),
1174+
wantErrMessage: pointer.Pointer("size \"invalid-memory-size\" is invalid: constraint at index 0 is invalid: for memory constraints an identifier is not allowed"),
11751175
},
11761176
}
11771177
for _, tt := range tests {

0 commit comments

Comments
 (0)