-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO and start of structure for dynamic port allocation for #14
- Loading branch information
1 parent
dc22f79
commit ba81814
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |