Skip to content

Commit

Permalink
Minor optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Jan 8, 2024
1 parent cad4b8c commit adc39d5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
32 changes: 22 additions & 10 deletions cmd/metal-api/internal/metal/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/metal-api/internal/service/partition-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/metal-api/internal/service/project-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit adc39d5

Please sign in to comment.