Skip to content

Commit

Permalink
Exclude dead machines from free count. (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Sep 13, 2024
1 parent 732b6ca commit 70670a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
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 @@ -445,7 +445,7 @@ func (r *partitionResource) calcPartitionCapacity(pcr *v1.PartitionCapacityReque
switch {
case m.Allocation != nil:
cap.Allocated++
case m.Waiting && !m.PreAllocated && m.State.Value == metal.AvailableState:
case m.Waiting && !m.PreAllocated && m.State.Value == metal.AvailableState && ec.Liveliness == metal.MachineLivelinessAlive:
// the free machine count considers the same aspects as the query for electing the machine candidate!
cap.Free++
default:
Expand Down
54 changes: 40 additions & 14 deletions cmd/metal-api/internal/service/partition-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ func TestUpdatePartition(t *testing.T) {

func TestPartitionCapacity(t *testing.T) {
var (
mockMachines = func(mock *r.Mock, reservations []metal.Reservation, ms ...metal.Machine) {
mockMachines = func(mock *r.Mock, liveliness metal.MachineLiveliness, reservations []metal.Reservation, ms ...metal.Machine) {
var (
sizes metal.Sizes
events metal.ProvisioningEventContainers
partitions metal.Partitions
)

for _, m := range ms {
ec := metal.ProvisioningEventContainer{Base: metal.Base{ID: m.ID}, Liveliness: metal.MachineLivelinessAlive}
ec := metal.ProvisioningEventContainer{Base: metal.Base{ID: m.ID}, Liveliness: liveliness}
if m.Waiting {
ec.Events = append(ec.Events, metal.ProvisioningEvent{
Event: metal.ProvisioningEventWaiting,
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestPartitionCapacity(t *testing.T) {
name: "one allocated machine",
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "project-123")
mockMachines(mock, nil, m1)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1)
},
want: []*v1.PartitionCapacity{
{
Expand All @@ -350,7 +350,7 @@ func TestPartitionCapacity(t *testing.T) {
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "project-123")
m2 := machineTpl("2", "partition-a", "size-a", "project-123")
mockMachines(mock, nil, m1, m2)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1, m2)
},
want: []*v1.PartitionCapacity{
{
Expand All @@ -373,7 +373,7 @@ func TestPartitionCapacity(t *testing.T) {
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "project-123")
m1.IPMI.Address = ""
mockMachines(mock, nil, m1)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1)
},
want: []*v1.PartitionCapacity{
{
Expand All @@ -398,7 +398,7 @@ func TestPartitionCapacity(t *testing.T) {
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "")
m1.Waiting = true
mockMachines(mock, nil, m1)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1)
},
want: []*v1.PartitionCapacity{
{
Expand All @@ -416,13 +416,39 @@ func TestPartitionCapacity(t *testing.T) {
},
},
},
{
name: "one dead machine",
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "")
m1.Waiting = true

mockMachines(mock, metal.MachineLivelinessDead, nil, m1)
},
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: 1,
Waiting: 1,
Faulty: 1,
Unavailable: 1,
FaultyMachines: []string{"1"},
},
},
},
},
},
{
name: "one waiting, one allocated machine",
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "")
m1.Waiting = true
m2 := machineTpl("2", "partition-a", "size-a", "project-123")
mockMachines(mock, nil, m1, m2)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1, m2)
},
want: []*v1.PartitionCapacity{
{
Expand All @@ -448,7 +474,7 @@ func TestPartitionCapacity(t *testing.T) {
m1 := machineTpl("1", "partition-a", "size-a", "")
m1.Waiting = true
m1.State.Value = metal.AvailableState
mockMachines(mock, nil, m1)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1)
},
want: []*v1.PartitionCapacity{
{
Expand All @@ -471,7 +497,7 @@ func TestPartitionCapacity(t *testing.T) {
mockFn: func(mock *r.Mock) {
m1 := machineTpl("1", "partition-a", "size-a", "")
m1.Waiting = false
mockMachines(mock, nil, m1)
mockMachines(mock, metal.MachineLivelinessAlive, nil, m1)
},
want: []*v1.PartitionCapacity{
{
Expand Down Expand Up @@ -504,7 +530,7 @@ func TestPartitionCapacity(t *testing.T) {
},
}

mockMachines(mock, reservations, m1)
mockMachines(mock, metal.MachineLivelinessAlive, reservations, m1)
},
want: []*v1.PartitionCapacity{
{
Expand Down Expand Up @@ -543,7 +569,7 @@ func TestPartitionCapacity(t *testing.T) {
},
}

mockMachines(mock, reservations, m1)
mockMachines(mock, metal.MachineLivelinessAlive, reservations, m1)
},
want: []*v1.PartitionCapacity{
{
Expand Down Expand Up @@ -579,7 +605,7 @@ func TestPartitionCapacity(t *testing.T) {
},
}

mockMachines(mock, reservations, m1, m2, m3)
mockMachines(mock, metal.MachineLivelinessAlive, reservations, m1, m2, m3)
},
want: []*v1.PartitionCapacity{
{
Expand Down Expand Up @@ -617,7 +643,7 @@ func TestPartitionCapacity(t *testing.T) {
},
}

mockMachines(mock, reservations, m1, m2, m3)
mockMachines(mock, metal.MachineLivelinessAlive, reservations, m1, m2, m3)
},
want: []*v1.PartitionCapacity{
{
Expand Down Expand Up @@ -660,7 +686,7 @@ func TestPartitionCapacity(t *testing.T) {
},
}

mockMachines(mock, reservations, m1, m2, m3)
mockMachines(mock, metal.MachineLivelinessAlive, reservations, m1, m2, m3)
},
want: []*v1.PartitionCapacity{
{
Expand Down

0 comments on commit 70670a1

Please sign in to comment.