Skip to content

Commit 818f4d3

Browse files
authored
Fix size overlapping not checked symmetrically (#532)
1 parent dfbc4a0 commit 818f4d3

File tree

5 files changed

+233
-73
lines changed

5 files changed

+233
-73
lines changed

cmd/metal-api/internal/grpc/boot-service_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ func TestBootService_Register(t *testing.T) {
119119
Uuid: tt.uuid,
120120
Hardware: &v1.MachineHardware{
121121
Memory: uint64(tt.memory),
122-
122+
Disks: []*v1.MachineBlockDevice{
123+
{
124+
Size: 1000000000000,
125+
},
126+
},
123127
Cpus: []*v1.MachineCPU{
124128
{
125129
Model: "Intel Xeon Silver",

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func countDisk(disk BlockDevice) (model string, count uint64) {
6161
return disk.Name, disk.Size
6262
}
6363

64+
func countMemory(size uint64) (model string, count uint64) {
65+
return "", size
66+
}
67+
6468
// Sizes is a list of sizes.
6569
type Sizes []Size
6670

@@ -114,9 +118,6 @@ func (c *Constraint) matches(hw MachineHardware) bool {
114118
// With this we ensure that hardware matches exhaustive against the constraints.
115119
func (hw *MachineHardware) matches(constraints []Constraint, constraintType ConstraintType) bool {
116120
filtered := lo.Filter(constraints, func(c Constraint, _ int) bool { return c.Type == constraintType })
117-
if len(filtered) == 0 {
118-
return true
119-
}
120121

121122
switch constraintType {
122123
case StorageConstraint:
@@ -126,10 +127,9 @@ func (hw *MachineHardware) matches(constraints []Constraint, constraintType Cons
126127
case CoreConstraint:
127128
return exhaustiveMatch(filtered, hw.MetalCPUs, countCPU)
128129
case MemoryConstraint:
129-
// Noop because we do not have different Memory types
130-
return true
130+
return exhaustiveMatch(filtered, []uint64{hw.Memory}, countMemory)
131131
default:
132-
return true
132+
return false
133133
}
134134
}
135135

@@ -168,15 +168,17 @@ nextsize:
168168
}
169169

170170
func (s *Size) overlaps(so *Size) bool {
171-
if len(lo.FromPtr(so).Constraints) == 0 {
171+
if len(lo.FromPtr(so).Constraints) == 0 || len(lo.FromPtr(s).Constraints) == 0 {
172172
return false
173173
}
174+
174175
srcTypes := lo.GroupBy(s.Constraints, func(item Constraint) ConstraintType {
175176
return item.Type
176177
})
177178
destTypes := lo.GroupBy(so.Constraints, func(item Constraint) ConstraintType {
178179
return item.Type
179180
})
181+
180182
for t, srcConstraints := range srcTypes {
181183
constraints, ok := destTypes[t]
182184
if !ok {
@@ -191,6 +193,20 @@ func (s *Size) overlaps(so *Size) bool {
191193
}
192194
}
193195

196+
for t, destConstraints := range destTypes {
197+
constraints, ok := srcTypes[t]
198+
if !ok {
199+
return false
200+
}
201+
for _, sc := range destConstraints {
202+
for _, c := range constraints {
203+
if !c.overlaps(sc) {
204+
return false
205+
}
206+
}
207+
}
208+
}
209+
194210
return true
195211
}
196212

0 commit comments

Comments
 (0)