diff --git a/pkg/gameservers/portallocator.go b/pkg/gameservers/portallocator.go index b072e99079..9f619ee4aa 100644 --- a/pkg/gameservers/portallocator.go +++ b/pkg/gameservers/portallocator.go @@ -120,6 +120,9 @@ func (pa *PortAllocator) Allocate(gs *agonesv1.GameServer) *agonesv1.GameServer // Also the return gives an escape from the double loop findOpenPorts := func(amount int) []pn { var ports []pn + if amount <= 0 { + return ports + } for _, n := range pa.portAllocations { for p, taken := range n { if !taken { diff --git a/pkg/gameservers/portallocator_test.go b/pkg/gameservers/portallocator_test.go index ab0a2ba861..1ea3cec8ab 100644 --- a/pkg/gameservers/portallocator_test.go +++ b/pkg/gameservers/portallocator_test.go @@ -236,6 +236,32 @@ func TestPortAllocatorAllocate(t *testing.T) { ports = append(ports, gs.Spec.Ports[0].HostPort) } }) + + t.Run("portPolicy as an empty string", func(t *testing.T) { + m := agtesting.NewMocks() + pa := NewPortAllocator(10, 50, m.KubeInformerFactory, m.AgonesInformerFactory) + nodeWatch := watch.NewFake() + m.KubeClient.AddWatchReactor("nodes", k8stesting.DefaultWatchReactor(nodeWatch, nil)) + + stop, cancel := agtesting.StartInformers(m, pa.nodeSynced) + defer cancel() + + // Make sure the add's don't corrupt the sync + // (no longer an issue, but leave this here for posterity) + nodeWatch.Add(&n1) + nodeWatch.Add(&n2) + assert.True(t, cache.WaitForCacheSync(stop, pa.nodeSynced)) + + err := pa.syncAll() + require.NoError(t, err) + + // single port empty + fd := fixture.DeepCopy() + fd.Spec.Ports[0].PortPolicy = "" + gs := pa.Allocate(fd) + assert.NotNil(t, gs) + assert.Equal(t, 0, countTotalAllocatedPorts(pa)) + }) } func TestPortAllocatorMultithreadAllocate(t *testing.T) {