Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request param to calculate partition capacity for specific project #588

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/metal-api/internal/service/partition-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ func (r *partitionResource) calcPartitionCapacity(pcr *v1.PartitionCapacityReque

cap.Reservations += reservation.Amount
cap.UsedReservations += usedReservations

if pcr.Project != nil && *pcr.Project == reservation.ProjectID {
continue
}

cap.Free -= reservation.Amount - usedReservations
cap.Free = max(cap.Free, 0)
}
Expand Down
52 changes: 51 additions & 1 deletion cmd/metal-api/internal/service/partition-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func TestPartitionCapacity(t *testing.T) {

tests := []struct {
name string
pcr *v1.PartitionCapacityRequest
mockFn func(mock *r.Mock)
want []*v1.PartitionCapacity
}{
Expand Down Expand Up @@ -742,15 +743,64 @@ func TestPartitionCapacity(t *testing.T) {
},
},
},
{
name: "evaluate capacity for specific project",
pcr: &v1.PartitionCapacityRequest{
Project: pointer.Pointer("project-123"),
},
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "project-123")
m2 := machineTpl("2", "partition-a", "size-a", "")
m3 := machineTpl("3", "partition-a", "size-a", "")
m2.Waiting = true
m3.Waiting = true

reservations := []metal.SizeReservation{
{
SizeID: "size-a",
Amount: 3,
ProjectID: "project-123",
PartitionIDs: []string{"partition-a"},
},
}

mockMachines(mock, metal.MachineLivelinessAlive, reservations, m1, m2, m3)
},
want: []*v1.PartitionCapacity{
{
Common: v1.Common{
Identifiable: v1.Identifiable{ID: "partition-a"}, Describable: v1.Describable{Name: pointer.Pointer(""), Description: pointer.Pointer("")},
},
ServerCapacities: v1.ServerCapacities{
{
Size: "size-a",
Total: 3,
Allocated: 1,
Waiting: 2,
Free: 2,
Allocatable: 2,
Reservations: 3,
UsedReservations: 1,
PhonedHome: 1,
RemainingReservations: 2,
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
ds, mock = datastore.InitMockDB(t)
body = &v1.PartitionCapacityRequest{}
body = tt.pcr
ws = NewPartition(slog.Default(), ds, nil)
)

if body == nil {
body = &v1.PartitionCapacityRequest{}
}

if tt.mockFn != nil {
tt.mockFn(mock)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/metal-api/internal/service/v1/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ type PartitionResponse struct {
}

type PartitionCapacityRequest struct {
ID *string `json:"id" description:"the id of the partition" optional:"true"`
Size *string `json:"sizeid" description:"the size to filter for" optional:"true"`
ID *string `json:"id" description:"the id of the partition" optional:"true"`
Size *string `json:"sizeid" description:"the size to filter for" optional:"true"`
Project *string `json:"projectid" description:"if provided the machine reservations of this project will be respected in the free counts"`
}

type ServerCapacities []*ServerCapacity
Expand Down
9 changes: 8 additions & 1 deletion spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -4171,11 +4171,18 @@
"description": "the id of the partition",
"type": "string"
},
"projectid": {
"description": "if provided the machine reservations of this project will be respected in the free counts",
"type": "string"
},
"sizeid": {
"description": "the size to filter for",
"type": "string"
}
}
},
"required": [
"projectid"
]
},
"v1.PartitionCreateRequest": {
"properties": {
Expand Down
Loading