diff --git a/cmd/metal-api/internal/metal/size.go b/cmd/metal-api/internal/metal/size.go index 62b4a194d..8d0f3852a 100644 --- a/cmd/metal-api/internal/metal/size.go +++ b/cmd/metal-api/internal/metal/size.go @@ -142,10 +142,8 @@ func (s *Size) Validate(partitions PartitionMap, projects map[string]*mdmv1.Proj } } - if s.Reservations != nil { - if err := s.Reservations.Validate(partitions, projects); err != nil { - return fmt.Errorf("invalid size reservation: %w", err) - } + if err := s.Reservations.Validate(partitions, projects); err != nil { + return fmt.Errorf("invalid size reservation: %w", err) } return nil @@ -162,30 +160,44 @@ func (s *Size) Overlaps(ss *Sizes) *Size { return nil } -func (rs Reservations) ForPartition(partitionID string) Reservations { +func (rs *Reservations) ForPartition(partitionID string) Reservations { + if rs == nil { + return nil + } + var result Reservations - for _, r := range rs { + for _, r := range *rs { r := r if slices.Contains(r.PartitionIDs, partitionID) { result = append(result, r) } } + return result } -func (rs Reservations) ForProject(projectID string) Reservations { +func (rs *Reservations) ForProject(projectID string) Reservations { + if rs == nil { + return nil + } + var result Reservations - for _, r := range rs { + for _, r := range *rs { r := r if r.ProjectID == projectID { result = append(result, r) } } + return result } -func (rs Reservations) Validate(partitions PartitionMap, projects map[string]*mdmv1.Project) error { - for _, r := range rs { +func (rs *Reservations) Validate(partitions PartitionMap, projects map[string]*mdmv1.Project) error { + if rs == nil { + return nil + } + + for _, r := range *rs { if r.Amount <= 0 { return fmt.Errorf("amount must be a positive integer") } diff --git a/cmd/metal-api/internal/service/partition-service.go b/cmd/metal-api/internal/service/partition-service.go index 5fd1c7398..615dc95b8 100644 --- a/cmd/metal-api/internal/service/partition-service.go +++ b/cmd/metal-api/internal/service/partition-service.go @@ -457,13 +457,11 @@ func (r *partitionResource) calcPartitionCapacity(pcr *v1.PartitionCapacityReque size := sizesByID[cap.Size] - if size.Reservations != nil { - for _, reservation := range size.Reservations.ForPartition(pc.ID) { - reservation := reservation + for _, reservation := range size.Reservations.ForPartition(pc.ID) { + reservation := reservation - cap.Reservations += reservation.Amount - cap.UsedReservations += min(len(machinesByProject[reservation.ProjectID]), reservation.Amount) - } + cap.Reservations += reservation.Amount + cap.UsedReservations += min(len(machinesByProject[reservation.ProjectID]), reservation.Amount) } } diff --git a/cmd/metal-api/internal/service/project-service.go b/cmd/metal-api/internal/service/project-service.go index 36edec520..8116d7a88 100644 --- a/cmd/metal-api/internal/service/project-service.go +++ b/cmd/metal-api/internal/service/project-service.go @@ -255,9 +255,7 @@ func (r *projectResource) deleteProject(request *restful.Request, response *rest for _, size := range sizes { size := size - if size.Reservations != nil { - sizeReservations = size.Reservations.ForProject(id) - } + sizeReservations = size.Reservations.ForProject(id) } if len(sizeReservations) > 0 {