Skip to content

Commit

Permalink
Fix size overlapping not checked symmetrically (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jun 4, 2024
1 parent dfbc4a0 commit 818f4d3
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 73 deletions.
6 changes: 5 additions & 1 deletion cmd/metal-api/internal/grpc/boot-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ func TestBootService_Register(t *testing.T) {
Uuid: tt.uuid,
Hardware: &v1.MachineHardware{
Memory: uint64(tt.memory),

Disks: []*v1.MachineBlockDevice{
{
Size: 1000000000000,
},
},
Cpus: []*v1.MachineCPU{
{
Model: "Intel Xeon Silver",
Expand Down
30 changes: 23 additions & 7 deletions cmd/metal-api/internal/metal/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func countDisk(disk BlockDevice) (model string, count uint64) {
return disk.Name, disk.Size
}

func countMemory(size uint64) (model string, count uint64) {
return "", size
}

// Sizes is a list of sizes.
type Sizes []Size

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

switch constraintType {
case StorageConstraint:
Expand All @@ -126,10 +127,9 @@ func (hw *MachineHardware) matches(constraints []Constraint, constraintType Cons
case CoreConstraint:
return exhaustiveMatch(filtered, hw.MetalCPUs, countCPU)
case MemoryConstraint:
// Noop because we do not have different Memory types
return true
return exhaustiveMatch(filtered, []uint64{hw.Memory}, countMemory)
default:
return true
return false
}
}

Expand Down Expand Up @@ -168,15 +168,17 @@ nextsize:
}

func (s *Size) overlaps(so *Size) bool {
if len(lo.FromPtr(so).Constraints) == 0 {
if len(lo.FromPtr(so).Constraints) == 0 || len(lo.FromPtr(s).Constraints) == 0 {
return false
}

srcTypes := lo.GroupBy(s.Constraints, func(item Constraint) ConstraintType {
return item.Type
})
destTypes := lo.GroupBy(so.Constraints, func(item Constraint) ConstraintType {
return item.Type
})

for t, srcConstraints := range srcTypes {
constraints, ok := destTypes[t]
if !ok {
Expand All @@ -191,6 +193,20 @@ func (s *Size) overlaps(so *Size) bool {
}
}

for t, destConstraints := range destTypes {
constraints, ok := srcTypes[t]
if !ok {
return false
}
for _, sc := range destConstraints {
for _, c := range constraints {
if !c.overlaps(sc) {
return false
}
}
}
}

return true
}

Expand Down
Loading

0 comments on commit 818f4d3

Please sign in to comment.