Skip to content
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
30 changes: 26 additions & 4 deletions internal/incusplacement/starlarkexec/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ type MemberSnapshot struct {
}

type InstanceSnapshot struct {
Name string
Name string
// Project is the Incus project that owns the instance. Empty means the
// project passed to get_instances / get_instances_count, so existing
// fleet-worker fixtures stay in the queried project.
Project string
MemoryLimitMiB int
}

Expand Down Expand Up @@ -162,8 +166,9 @@ func Execute(script string, request PlacementRequest, cluster ClusterSnapshot) (
if err != nil {
return nil, err
}
instances := make([]api.Instance, 0, len(member.Instances))
for _, instance := range member.Instances {
filtered := instancesForProject(member, project)
instances := make([]api.Instance, 0, len(filtered))
for _, instance := range filtered {
instances = append(instances, api.Instance{
Name: instance.Name, Project: project, Location: location,
ExpandedConfig: api.ConfigMap{
Expand All @@ -185,7 +190,7 @@ func Execute(script string, request PlacementRequest, cluster ClusterSnapshot) (
if err != nil {
return nil, err
}
count := len(member.Instances)
count := len(instancesForProject(member, project))
if includePending {
count = member.PendingCount
}
Expand Down Expand Up @@ -269,3 +274,20 @@ func lookupMember(members map[string]MemberSnapshot, name string) (MemberSnapsho
}
return member, nil
}

func instancesForProject(member MemberSnapshot, project string) []InstanceSnapshot {
if project == "" {
return append([]InstanceSnapshot(nil), member.Instances...)
}
matched := make([]InstanceSnapshot, 0, len(member.Instances))
for _, instance := range member.Instances {
instanceProject := instance.Project
if instanceProject == "" {
instanceProject = project
}
if instanceProject == project {
matched = append(matched, instance)
}
}
return matched
}
17 changes: 17 additions & 0 deletions internal/incusplacement/starlarkexec/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ func TestWrongProjectIsANoOp(t *testing.T) {
}
}

func TestForeignProjectInstancesDoNotConsumeFleetRAM(t *testing.T) {
script := renderExample(t)
cluster := fourEmptyMembers()
cluster.Members[0].Instances = []InstanceSnapshot{
{Name: "other-1", Project: "not-fleet", MemoryLimitMiB: 8192},
{Name: "other-2", Project: "not-fleet", MemoryLimitMiB: 8192},
}
cluster.Members[0].PendingCount = 0
outcome, err := Execute(script, eightGiBRequest(), cluster)
if err != nil {
t.Fatal(err)
}
if outcome.Failed || outcome.Target != "gha-runner-1" {
t.Fatalf("other-project occupancy filled gha-fleet RAM: %#v", outcome)
}
}

func renderExample(t *testing.T) string {
t.Helper()
_, current, _, ok := runtime.Caller(0)
Expand Down
Loading