This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
/
portregistry.go
184 lines (151 loc) · 4.84 KB
/
portregistry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package controllers
import (
"errors"
"fmt"
"math/rand"
dgsclientset "github.com/dgkanatsios/azuregameserversscalingkubernetes/pkg/client/clientset/versioned"
"github.com/dgkanatsios/azuregameserversscalingkubernetes/pkg/shared"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// PortRegistry implements a custom map for the port registry
type PortRegistry struct {
Ports map[int32]bool
Indexes []int32
NextFreePortIndex int32
Min int32 // Minimum Port
Max int32 // Maximum Port
portRequests chan struct{} // buffered channel to store port requests
portResponses chan int32 // buffered channel to store port responses (system returns the HostPorts)
}
// NewPortRegistry initializes the IndexedDictionary that holds the port registry.
func NewPortRegistry(dgsclientset dgsclientset.Interface, min, max int32, namespace string) (*PortRegistry, error) {
pr := &PortRegistry{
Ports: make(map[int32]bool, max-min+1),
Indexes: make([]int32, max-min+1),
Min: min,
Max: max,
portRequests: make(chan struct{}, 100),
portResponses: make(chan int32, 100),
}
dgsList, err := dgsclientset.AzuregamingV1alpha1().DedicatedGameServers(namespace).List(metav1.ListOptions{})
if err != nil {
log.Errorf("Error getting Dedicated Game Servers List: %v", err)
return nil, err
}
// gather ports for existing DGS
if len(dgsList.Items) > 0 {
for _, dgs := range dgsList.Items {
if len(dgs.Spec.Template.Containers) == 0 {
log.Errorf("DGS with name %s has no containers in its Pod Template: %#v", dgs.Name, dgs)
continue
}
if len(dgs.Spec.PortsToExpose) == 0 {
continue //no ports exported for this DGS
}
portsExposed := make([]int32, len(dgs.Spec.PortsToExpose))
portsExposedIndex := 0
for j := 0; j < len(dgs.Spec.Template.Containers); j++ {
for _, portInfo := range dgs.Spec.Template.Containers[j].Ports {
if portInfo.HostPort == 0 {
log.Errorf("HostPort for DGS %s and ContainerPort %d is zero, ignoring", dgs.Name, portInfo.ContainerPort)
continue
}
// if this port is to be exposed
if shared.SliceContains(dgs.Spec.PortsToExpose, portInfo.ContainerPort) {
portsExposed[portsExposedIndex] = portInfo.HostPort
portsExposedIndex++
}
}
}
pr.assignRegisteredPorts(portsExposed)
}
}
pr.assignUnregisteredPorts()
go pr.portProducer()
return pr, nil
}
func (pr *PortRegistry) displayRegistry() {
fmt.Printf("-------------------------------------\n")
fmt.Printf("Ports: %v\n", pr.Ports)
fmt.Printf("Indexes: %v\n", pr.Indexes)
fmt.Printf("NextIndex: %d\n", pr.NextFreePortIndex)
fmt.Printf("-------------------------------------\n")
}
// GetNewPort returns and registers a new port for the designated game server. Locks a mutex
func (pr *PortRegistry) GetNewPort() (int32, error) {
pr.portRequests <- struct{}{}
port := <-pr.portResponses
if port == -1 {
return -1, errors.New("Cannot register a new port. No available ports")
}
return port, nil
}
func (pr *PortRegistry) portProducer() {
for range pr.portRequests { //wait till a new request comes
initialIndex := pr.NextFreePortIndex
for {
if !pr.Ports[pr.Indexes[pr.NextFreePortIndex]] {
//we found a port
port := pr.Indexes[pr.NextFreePortIndex]
pr.Ports[port] = true
pr.increaseNextFreePortIndex()
//port is set
pr.portResponses <- port
break
}
pr.increaseNextFreePortIndex()
if initialIndex == pr.NextFreePortIndex {
//we did a full loop - no empty ports
pr.portResponses <- -1
break
}
}
}
}
// Stop stops port registry mechanism by closing requests and responses channels
func (pr *PortRegistry) Stop() {
close(pr.portRequests)
close(pr.portResponses)
}
// DeregisterServerPorts deregisters all ports
func (pr *PortRegistry) DeregisterServerPorts(ports []int32) {
for i := 0; i < len(ports); i++ {
pr.Ports[ports[i]] = false
}
}
func (pr *PortRegistry) assignRegisteredPorts(ports []int32) {
for i := 0; i < len(ports); i++ {
pr.Ports[ports[i]] = true
pr.Indexes[i] = ports[i]
pr.increaseNextFreePortIndex()
}
}
func (pr *PortRegistry) assignUnregisteredPorts() {
i := pr.NextFreePortIndex
for _, port := range pr.getPermutatedPorts() {
if _, ok := pr.Ports[port]; !ok {
pr.Ports[port] = false
pr.Indexes[i] = port
i++
}
}
}
func (pr *PortRegistry) increaseNextFreePortIndex() {
pr.NextFreePortIndex++
//reset the index if needed
if pr.NextFreePortIndex == pr.Max-pr.Min+1 {
pr.NextFreePortIndex = 0
}
}
func (pr *PortRegistry) getPermutatedPorts() []int32 {
ports := make([]int32, pr.Max-pr.Min+1)
for i := 0; i < len(ports); i++ {
ports[i] = int32(pr.Min)
}
perm := rand.Perm(int((pr.Max - pr.Min + 1)))
for i := 0; i < len(ports); i++ {
ports[i] += int32(perm[i])
}
return ports
}