Skip to content

Commit

Permalink
Fixes for size reservations. (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jan 9, 2024
1 parent 2bf3237 commit 451415e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cmd/metal-api/internal/metal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ func (ms Machines) WithSize(id string) Machines {
return res
}

func (ms Machines) WithPartition(id string) Machines {
var res Machines

for _, m := range ms {
m := m

if m.PartitionID != id {
continue
}

res = append(res, m)
}

return res
}

// MachineNetwork stores the Network details of the machine
type MachineNetwork struct {
NetworkID string `rethinkdb:"networkid" json:"networkid"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/metal-api/internal/service/partition-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (r *partitionResource) calcPartitionCapacity(pcr *v1.PartitionCapacityReque
reservation := reservation

cap.Reservations += reservation.Amount
cap.UsedReservations += min(len(machinesByProject[reservation.ProjectID]), reservation.Amount)
cap.UsedReservations += min(len(machinesByProject[reservation.ProjectID].WithSize(size.ID).WithPartition(pc.ID)), reservation.Amount)
}
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/metal-api/internal/service/size-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (r *sizeResource) webService() *restful.WebService {
Doc("get all size reservations").
Metadata(restfulspec.KeyOpenAPITags, tags).
Metadata(auditing.Exclude, true).
Reads(v1.EmptyBody{}).
Writes([]v1.SizeReservationResponse{}).
Returns(http.StatusOK, "OK", []v1.SizeReservationResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))
Expand Down Expand Up @@ -454,7 +455,7 @@ func (r *sizeResource) listSizeReservations(request *restful.Request, response *

for _, partitionID := range reservation.PartitionIDs {
project := pointer.SafeDeref(projectsByID[reservation.ProjectID])
allocations := len(machinesByProjectID[reservation.ProjectID].WithSize(size.ID))
allocations := len(machinesByProjectID[reservation.ProjectID].WithPartition(partitionID).WithSize(size.ID))

result = append(result, &v1.SizeReservationResponse{
SizeID: size.ID,
Expand Down
10 changes: 10 additions & 0 deletions spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -8939,6 +8939,16 @@
"application/json"
],
"operationId": "listSizeReservations",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1.EmptyBody"
}
}
],
"produces": [
"application/json"
],
Expand Down

0 comments on commit 451415e

Please sign in to comment.