feat: gate allocator roster publication

This commit is contained in:
Josh Creek
2026-09-01 09:55:09 +01:00
parent 8b5b5333c6
commit febc69bdef
5 changed files with 49 additions and 4 deletions
+15
View File
@@ -18,12 +18,27 @@ type Durable interface {
RecordProviderAllocation(context.Context, domain.Allocation, time.Time) (domain.Allocation, error)
}
type RosterPublisher interface {
PublishRoster(context.Context, domain.Assignment, []domain.SignedJoinAuthorisation, func([]byte, []byte) bool) error
}
type Service struct {
Provider Provider
Durable Durable
Roster RosterPublisher
Now func() time.Time
}
func (s Service) PublishRoster(ctx context.Context, assignment domain.Assignment, roster []domain.SignedJoinAuthorisation, verify func([]byte, []byte) bool) error {
if s.Roster == nil {
return errNotConfigured
}
if assignment.Allocation.State != domain.ServerAllocated || assignment.Endpoint == "" {
return domain.ErrManifestRejected
}
return s.Roster.PublishRoster(ctx, assignment, roster, verify)
}
func (s Service) Allocate(ctx context.Context, request domain.AllocationRequest, labels map[string]string) (agones.AllocatedServer, error) {
if s.Provider == nil || s.Durable == nil || s.Now == nil {
return agones.AllocatedServer{}, errNotConfigured
+23
View File
@@ -27,6 +27,16 @@ type durableSpy struct {
err error
}
type rosterSpy struct {
calls int
err error
}
func (r *rosterSpy) PublishRoster(_ context.Context, _ domain.Assignment, _ []domain.SignedJoinAuthorisation, _ func([]byte, []byte) bool) error {
r.calls++
return r.err
}
func (d *durableSpy) RecordProviderAllocation(_ context.Context, allocation domain.Allocation, _ time.Time) (domain.Allocation, error) {
d.calls++
d.allocation = allocation
@@ -52,3 +62,16 @@ func TestServiceDoesNotReturnProviderResultAfterDurableFailure(t *testing.T) {
t.Fatalf("result=%+v err=%v calls=%d", result, err, durable.calls)
}
}
func TestServicePublishesRosterOnlyForAllocatedAssignment(t *testing.T) {
roster := &rosterSpy{}
service := Service{Roster: roster}
assignment := domain.Assignment{Allocation: domain.Allocation{State: domain.ServerAllocated}, Endpoint: "127.0.0.1:7777"}
if err := service.PublishRoster(context.Background(), assignment, []domain.SignedJoinAuthorisation{{Signature: []byte("sig")}}, func([]byte, []byte) bool { return true }); err != nil || roster.calls != 1 {
t.Fatalf("publish err=%v calls=%d", err, roster.calls)
}
assignment.Allocation.State = domain.ServerReady
if err := service.PublishRoster(context.Background(), assignment, nil, nil); err != domain.ErrManifestRejected || roster.calls != 1 {
t.Fatalf("premature publish err=%v calls=%d", err, roster.calls)
}
}