From ba8181407fa2067c6ac663ccf27ba60d9ecf0d0f Mon Sep 17 00:00:00 2001 From: Mark Mandel Date: Sun, 17 Dec 2017 16:20:25 -0800 Subject: [PATCH] TODO and start of structure for dynamic port allocation for #14 --- gameservers/controller/portallocator.go | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 gameservers/controller/portallocator.go diff --git a/gameservers/controller/portallocator.go b/gameservers/controller/portallocator.go new file mode 100644 index 0000000000..dc403629ac --- /dev/null +++ b/gameservers/controller/portallocator.go @@ -0,0 +1,77 @@ +// Copyright 2017 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "sync" + + "k8s.io/client-go/informers" + corelisterv1 "k8s.io/client-go/listers/core/v1" + "k8s.io/client-go/tools/cache" +) + +// A set of port allocations for a node +type portAllocation map[int32]bool + +// PortAllocator manages the dynamic port +// allocation strategy. Only use exposed methods to ensure +// appropriate locking is taken. +// The PortAllocator does not currently support mixing static ports (or any pods with defined HostPort) +// within the dynamic port range other than the ones it coordinates. +type PortAllocator struct { + mutex sync.RWMutex + ports []portAllocation + minPort int32 + maxPort int32 + podLister corelisterv1.PodLister + podSynced cache.InformerSynced + nodeSynced cache.InformerSynced + stop <-chan struct{} +} + +// NewPortAllocator returns a new dynamic port +// allocator. minPort and maxPort are the top and bottom ports that can be allocated in the range for +// the game servers +func NewPortAllocator(minPort, maxPort int32, kubeInformerFactory informers.SharedInformerFactory, stop <-chan struct{}) *PortAllocator { + v1 := kubeInformerFactory.Core().V1() + pods := v1.Pods() + nodes := v1.Nodes() + pa := &PortAllocator{ + mutex: sync.RWMutex{}, + minPort: minPort, + maxPort: maxPort, + podLister: pods.Lister(), + podSynced: pods.Informer().HasSynced, + nodeSynced: nodes.Informer().HasSynced, + stop: stop, + } + + // TODO: add the portAllocation data structure + + // TODO: when a Pod gets deleted, turn off allocation + + // TODO: If a Node gets added, just add a new blank entry into the []PortSelections (again with locking) + + // TODO: If a Node gets deleted/set to being unscheduled, then we will need to reset the entire state. Make sure the Pod cache is synced before doing this. (it will be the same logic as when first started) + + return pa +} + +// TODO: need a function that looks at all the nodes, and all the Pods in those nodes - and sets up the []portAllocation +// Check any active pods in each node, and turn off their ports for any that have a hostPort. + +// TODO: write an allocate() (int32, error) - that returns a port, or an error if it cannot do so + +// TODO: write a race test to make sure there are no race conditions when allocating ports