Skip to content

Commit c28848a

Browse files
authored
Fix findOpenPorts portAllocator function (agones-dev#1725)
* Fix findOpenPorts portAllocator function There could be the possible case when we set PortPolicy as an empty string on update of GameServer.
1 parent 3f44b58 commit c28848a

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎pkg/gameservers/portallocator.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ func (pa *PortAllocator) Allocate(gs *agonesv1.GameServer) *agonesv1.GameServer
120120
// Also the return gives an escape from the double loop
121121
findOpenPorts := func(amount int) []pn {
122122
var ports []pn
123+
if amount <= 0 {
124+
return ports
125+
}
123126
for _, n := range pa.portAllocations {
124127
for p, taken := range n {
125128
if !taken {

‎pkg/gameservers/portallocator_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ func TestPortAllocatorAllocate(t *testing.T) {
236236
ports = append(ports, gs.Spec.Ports[0].HostPort)
237237
}
238238
})
239+
240+
t.Run("portPolicy as an empty string", func(t *testing.T) {
241+
m := agtesting.NewMocks()
242+
pa := NewPortAllocator(10, 50, m.KubeInformerFactory, m.AgonesInformerFactory)
243+
nodeWatch := watch.NewFake()
244+
m.KubeClient.AddWatchReactor("nodes", k8stesting.DefaultWatchReactor(nodeWatch, nil))
245+
246+
stop, cancel := agtesting.StartInformers(m, pa.nodeSynced)
247+
defer cancel()
248+
249+
// Make sure the add's don't corrupt the sync
250+
// (no longer an issue, but leave this here for posterity)
251+
nodeWatch.Add(&n1)
252+
nodeWatch.Add(&n2)
253+
assert.True(t, cache.WaitForCacheSync(stop, pa.nodeSynced))
254+
255+
err := pa.syncAll()
256+
require.NoError(t, err)
257+
258+
// single port empty
259+
fd := fixture.DeepCopy()
260+
fd.Spec.Ports[0].PortPolicy = ""
261+
gs := pa.Allocate(fd)
262+
assert.NotNil(t, gs)
263+
assert.Equal(t, 0, countTotalAllocatedPorts(pa))
264+
})
239265
}
240266

241267
func TestPortAllocatorMultithreadAllocate(t *testing.T) {

0 commit comments

Comments
 (0)