From 602e7011867e42d67252a03f8024d8c513b0a283 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 9 Jan 2024 14:56:10 +0100 Subject: [PATCH 1/2] Fixes for size reservations. --- cmd/metal-api/internal/metal/machine.go | 16 ++++++++++++++++ cmd/metal-api/internal/service/size-service.go | 3 ++- spec/metal-api.json | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cmd/metal-api/internal/metal/machine.go b/cmd/metal-api/internal/metal/machine.go index ecd246604..339e9cf85 100644 --- a/cmd/metal-api/internal/metal/machine.go +++ b/cmd/metal-api/internal/metal/machine.go @@ -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"` diff --git a/cmd/metal-api/internal/service/size-service.go b/cmd/metal-api/internal/service/size-service.go index b0a1736d0..8afb0beb5 100644 --- a/cmd/metal-api/internal/service/size-service.go +++ b/cmd/metal-api/internal/service/size-service.go @@ -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{})) @@ -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, diff --git a/spec/metal-api.json b/spec/metal-api.json index eba6d34a2..2f26cb4ca 100644 --- a/spec/metal-api.json +++ b/spec/metal-api.json @@ -8939,6 +8939,16 @@ "application/json" ], "operationId": "listSizeReservations", + "parameters": [ + { + "in": "body", + "name": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.EmptyBody" + } + } + ], "produces": [ "application/json" ], From fd1589d0fd7b47145f19b34b85f5a34f89f0d6be Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 9 Jan 2024 15:01:00 +0100 Subject: [PATCH 2/2] Filter. --- cmd/metal-api/internal/service/partition-service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/metal-api/internal/service/partition-service.go b/cmd/metal-api/internal/service/partition-service.go index 615dc95b8..ddd397f01 100644 --- a/cmd/metal-api/internal/service/partition-service.go +++ b/cmd/metal-api/internal/service/partition-service.go @@ -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) } }