/
util.go
572 lines (520 loc) · 16.8 KB
/
util.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright 2021 The OCGI Authors.
//
// 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 gameservers
import (
"time"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
"github.com/ocgi/carrier/pkg/apis/carrier"
carrierv1alpha1 "github.com/ocgi/carrier/pkg/apis/carrier/v1alpha1"
"github.com/ocgi/carrier/pkg/util"
)
const (
// ToBeDeletedTaint is a taint used to make the node unschedulable.
ToBeDeletedTaint = "ToBeDeletedByClusterAutoscaler"
)
// ApplyDefaults applies default values to the GameServer if they are not already populated
func ApplyDefaults(gs *carrierv1alpha1.GameServer) {
if gs.Annotations == nil {
gs.Annotations = map[string]string{}
}
gs.Annotations[carrier.GroupName] = carrierv1alpha1.SchemeGroupVersion.String()
gs.Finalizers = append(gs.Finalizers, carrier.GroupName)
applySpecDefaults(gs)
}
// ApplyDefaults applies default values to the GameServerSpec if they are not already populated
func applySpecDefaults(gs *carrierv1alpha1.GameServer) {
gss := &gs.Spec
applyPortDefaults(gss)
applySchedulingDefaults(gss)
}
// applyPortDefaults applies default values for all ports
func applyPortDefaults(gss *carrierv1alpha1.GameServerSpec) {
for i, p := range gss.Ports {
// basic spec
if p.PortPolicy == "" {
gss.Ports[i].PortPolicy = carrierv1alpha1.LoadBalancer
}
if p.Protocol == "" {
gss.Ports[i].Protocol = "UDP"
}
}
}
// applySchedulingDefaults set default `MostAllocated`
func applySchedulingDefaults(gss *carrierv1alpha1.GameServerSpec) {
if gss.Scheduling == "" {
gss.Scheduling = carrierv1alpha1.MostAllocated
}
}
// IsDeletable returns false if the server is currently not deletable
func IsDeletable(gs *carrierv1alpha1.GameServer) bool {
if IsInPlaceUpdating(gs) {
return false
}
return deleteReady(gs)
}
// IsDeletableWithGates returns false if the server is currently not deletable and has deletableGates
func IsDeletableWithGates(gs *carrierv1alpha1.GameServer) bool {
return len(gs.Spec.DeletableGates) != 0 && IsDeletable(gs)
}
// deleteReady checks if deletable gates in condition are all `True`
func deleteReady(gs *carrierv1alpha1.GameServer) bool {
condMap := make(map[string]carrierv1alpha1.ConditionStatus, len(gs.Status.Conditions))
for _, condition := range gs.Status.Conditions {
condMap[string(condition.Type)] = condition.Status
}
for _, gate := range gs.Spec.DeletableGates {
if v, ok := condMap[gate]; !ok || v != carrierv1alpha1.ConditionTrue {
return false
}
}
return true
}
// IsDeletableExist checks if deletable gates exits
func IsDeletableExist(gs *carrierv1alpha1.GameServer) bool {
return len(gs.Spec.DeletableGates) != 0
}
// IsReadinessExist checks if readiness gates exits
func IsReadinessExist(gs *carrierv1alpha1.GameServer) bool {
return len(gs.Spec.ReadinessGates) != 0
}
// IsBeingDeleted returns true if the server is in the process of being deleted.
func IsBeingDeleted(gs *carrierv1alpha1.GameServer) bool {
return !gs.DeletionTimestamp.IsZero() || gs.Status.State == carrierv1alpha1.GameServerFailed ||
gs.Status.State == carrierv1alpha1.GameServerExited
}
// IsStopped returns true if the server is failed or exited
func IsStopped(gs *carrierv1alpha1.GameServer) bool {
return gs.Status.State == carrierv1alpha1.GameServerFailed ||
gs.Status.State == carrierv1alpha1.GameServerExited
}
// IsBeforeRunning returns if GameServer is not running.
func IsBeforeRunning(gs *carrierv1alpha1.GameServer) bool {
if gs.Status.State == "" || gs.Status.State == carrierv1alpha1.GameServerUnknown ||
gs.Status.State == carrierv1alpha1.GameServerStarting {
return true
}
return false
}
// IsReady returns true if the GameServer Status Condition are all OK
func IsReady(gs *carrierv1alpha1.GameServer) bool {
condMap := make(map[string]carrierv1alpha1.ConditionStatus, len(gs.Status.Conditions))
for _, condition := range gs.Status.Conditions {
condMap[string(condition.Type)] = condition.Status
}
for _, gate := range gs.Spec.ReadinessGates {
if v, ok := condMap[gate]; !ok || v != carrierv1alpha1.ConditionTrue {
return false
}
}
return true
}
// IsOutOfService checks if a GameServer is marked out of service, and a delete candidate
func IsOutOfService(gs *carrierv1alpha1.GameServer) bool {
for _, constraint := range gs.Spec.Constraints {
if constraint.Type != carrierv1alpha1.NotInService {
continue
}
if *constraint.Effective == true {
return true
}
}
return false
}
// IsInPlaceUpdating checks if a GameServer is inplace updating
func IsInPlaceUpdating(gs *carrierv1alpha1.GameServer) bool {
if len(gs.Annotations) == 0 {
return false
}
return gs.Annotations[util.GameServerInPlaceUpdatingAnnotation] == "true"
}
// IsDynamicPortAllocated checks if ports allocated
func IsDynamicPortAllocated(gs *carrierv1alpha1.GameServer) bool {
if len(gs.Annotations) == 0 {
return false
}
return gs.Annotations[util.GameServerDynamicPortAllocated] == "true"
}
// CanInPlaceUpdating checks if a GameServer can inplace updating
func CanInPlaceUpdating(gs *carrierv1alpha1.GameServer) bool {
if IsBeingDeleted(gs) {
return false
}
if IsBeforeRunning(gs) {
return true
}
return IsInPlaceUpdating(gs) && deleteReady(gs)
}
// SetInPlaceUpdatingStatus set if it is inplace updating
func SetInPlaceUpdatingStatus(gs *carrierv1alpha1.GameServer, status string) {
if gs.Annotations == nil {
gs.Annotations = make(map[string]string)
}
gs.Annotations[util.GameServerInPlaceUpdatingAnnotation] = status
}
// buildPod build pod according to GameServerSpec
func buildPod(gs *carrierv1alpha1.GameServer) (*corev1.Pod, error) {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: gs.Name,
Namespace: gs.Namespace,
},
Spec: *gs.Spec.Template.Spec.DeepCopy(),
}
podObjectMeta(gs, pod)
if len(findPorts(gs)) > 0 {
i, gsContainer, err := FindContainer(&gs.Spec, util.GameServerContainerName)
if err != nil {
return pod, err
}
klog.V(5).Infof("Found desired container %v", i)
for _, p := range gs.Spec.Ports {
if p.ContainerPort != nil {
cp := corev1.ContainerPort{
ContainerPort: *p.ContainerPort,
HostPort: *p.HostPort,
}
if p.Protocol == "TCPUDP" {
cp.Protocol = corev1.ProtocolTCP
cpUTP := cp.DeepCopy()
cpUTP.Protocol = corev1.ProtocolUDP
gsContainer.Ports = append(gsContainer.Ports, *cpUTP)
} else {
cp.Protocol = p.Protocol
}
gsContainer.Ports = append(gsContainer.Ports, cp)
}
if p.ContainerPortRange != nil && p.HostPortRange != nil {
for idx := p.ContainerPortRange.MinPort; idx <= p.ContainerPortRange.MaxPort; idx++ {
cp := corev1.ContainerPort{
ContainerPort: idx,
HostPort: p.HostPortRange.MinPort + (p.HostPortRange.MinPort - idx),
}
if p.Protocol == "TCPUDP" {
cp.Protocol = corev1.ProtocolTCP
cpUTP := cp.DeepCopy()
cpUTP.Protocol = corev1.ProtocolUDP
gsContainer.Ports = append(gsContainer.Ports, *cpUTP)
} else {
cp.Protocol = p.Protocol
}
gsContainer.Ports = append(gsContainer.Ports, cp)
}
}
}
klog.V(5).Infof("Final desired container %+v", gsContainer)
pod.Spec.Containers[i] = gsContainer
}
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
injectPodScheduling(gs, pod)
injectPodTolerations(pod)
return pod, nil
}
// podObjectMeta configures the pod ObjectMeta details
func podObjectMeta(gs *carrierv1alpha1.GameServer, pod *corev1.Pod) {
pod.Labels = util.Merge(pod.Labels, gs.Labels)
pod.Annotations = util.Merge(pod.Annotations, gs.Annotations)
pod.Labels[util.RoleLabelKey] = util.GameServerLabelRoleValue
pod.Labels[util.GameServerPodLabelKey] = gs.Name
ref := metav1.NewControllerRef(gs, carrierv1alpha1.SchemeGroupVersion.WithKind("GameServer"))
pod.OwnerReferences = append(pod.OwnerReferences, *ref)
// Add Carrier version into Pod Annotations
pod.Annotations[carrier.GroupName] = carrierv1alpha1.SchemeGroupVersion.Version
}
// injectPodScheduling helps inject podAffinity/PodAntiAffinity to podSpec if the policy is `Most/LeastAllocated`
func injectPodScheduling(gs *carrierv1alpha1.GameServer, pod *corev1.Pod) {
if gs.Spec.Scheduling == carrierv1alpha1.Default {
return
}
if gs.Spec.Scheduling == carrierv1alpha1.LeastAllocated {
if pod.Spec.Affinity == nil {
pod.Spec.Affinity = &corev1.Affinity{}
}
if pod.Spec.Affinity.PodAntiAffinity == nil {
pod.Spec.Affinity.PodAntiAffinity = &corev1.PodAntiAffinity{}
}
antiAffExection := pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution
term := corev1.WeightedPodAffinityTerm{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: "kubernetes.io/hostname",
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{util.RoleLabelKey: util.GameServerLabelRoleValue},
},
},
}
pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(antiAffExection,
term)
}
if gs.Spec.Scheduling == carrierv1alpha1.MostAllocated {
if pod.Spec.Affinity == nil {
pod.Spec.Affinity = &corev1.Affinity{}
}
if pod.Spec.Affinity.PodAffinity == nil {
pod.Spec.Affinity.PodAffinity = &corev1.PodAffinity{}
}
affExection := pod.Spec.Affinity.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution
term := corev1.WeightedPodAffinityTerm{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: "kubernetes.io/hostname",
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{util.RoleLabelKey: util.GameServerLabelRoleValue},
},
},
}
pod.Spec.Affinity.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(affExection, term)
return
}
}
// injectPodTolerations helps add tolerations to pod.
// tolerate: NotReady、Unreachable
func injectPodTolerations(pod *corev1.Pod) {
pod.Spec.Tolerations = append(pod.Spec.Tolerations, []corev1.Toleration{
{
Key: corev1.TaintNodeNotReady,
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
},
{
Key: corev1.TaintNodeUnreachable,
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
}}...)
}
// isGameServerPod returns if this Pod is a Pod that comes from a GameServer
func isGameServerPod(pod *corev1.Pod) bool {
return pod.Labels[util.GameServerPodLabelKey] != ""
}
// applyGameServerAddressAndPort applys pod ip and node name to GameServer's fields
func applyGameServerAddressAndPort(gs *carrierv1alpha1.GameServer, pod *corev1.Pod) {
gs.Status.Address = pod.Status.PodIP
gs.Status.NodeName = pod.Spec.NodeName
if isHostPortNetwork(&gs.Spec) {
var ingress []carrierv1alpha1.LoadBalancerIngress
for _, p := range gs.Spec.Ports {
ingress = append(ingress, carrierv1alpha1.LoadBalancerIngress{
IP: pod.Spec.NodeName,
Ports: []carrierv1alpha1.LoadBalancerPort{
{
ContainerPort: p.ContainerPort,
ExternalPort: p.HostPort,
ContainerPortRange: p.ContainerPortRange,
ExternalPortRange: p.HostPortRange,
Protocol: p.Protocol,
},
},
})
}
gs.Status.LoadBalancerStatus = &carrierv1alpha1.LoadBalancerStatus{
Ingress: ingress,
}
}
}
// isHostPortNetwork checks if pod runs as hostHost
func isHostPortNetwork(gss *carrierv1alpha1.GameServerSpec) bool {
spec := gss.Template.Spec
return spec.HostNetwork
}
// FindContainer returns the container specified by the name parameter. Returns the index and the value.
// Returns an error if not found.
func FindContainer(gss *carrierv1alpha1.GameServerSpec, name string) (int, corev1.Container, error) {
for i, c := range gss.Template.Spec.Containers {
if c.Name == name {
return i, c, nil
}
}
return -1, corev1.Container{}, errors.Errorf("Could not find a container named %s", name)
}
// checkNodeTaintByCA checks if node is marked as deletable.
// if true we should add constraints to gs spec.
func checkNodeTaintByCA(node *corev1.Node) bool {
if len(node.Spec.Taints) == 0 {
return false
}
for _, taint := range node.Spec.Taints {
if taint.Key == ToBeDeletedTaint {
return true
}
}
return false
}
// NotInServiceConstraint describe a constraint that gs should not be
// in service again.
func NotInServiceConstraint() carrierv1alpha1.Constraint {
effective := true
now := metav1.NewTime(time.Now())
return carrierv1alpha1.Constraint{
Type: carrierv1alpha1.NotInService,
Effective: &effective,
Message: "Carrier controller mark this game server as not in service",
TimeAdded: &now,
}
}
// IsLoadBalancerPortExist check if a GameServer requires Load Balancer.
func IsLoadBalancerPortExist(gs *carrierv1alpha1.GameServer) bool {
for _, port := range gs.Spec.Ports {
if port.PortPolicy == carrierv1alpha1.LoadBalancer {
return true
}
}
return false
}
// updatePodSpec update game server spec, include, image and resource.
func updatePodSpec(gs *carrierv1alpha1.GameServer, pod *corev1.Pod) {
var image string
var resources corev1.ResourceRequirements
if pod.Labels == nil {
pod.Labels = make(map[string]string)
}
pod.Labels[util.GameServerHash] = gs.Labels[util.GameServerHash]
for _, container := range gs.Spec.Template.Spec.Containers {
if container.Name != util.GameServerContainerName {
continue
}
image = container.Image
resources = container.Resources
}
for i, container := range pod.Spec.Containers {
if container.Name != util.GameServerContainerName {
continue
}
pod.Spec.Containers[i].Image = image
for name, quantity := range resources.Limits {
if pod.Spec.Containers[i].Resources.Limits == nil {
pod.Spec.Containers[i].Resources.Limits = corev1.ResourceList{}
}
pod.Spec.Containers[i].Resources.Limits[name] = quantity
}
for name, quantity := range resources.Requests {
if pod.Spec.Containers[i].Resources.Requests == nil {
pod.Spec.Containers[i].Resources.Requests = corev1.ResourceList{}
}
pod.Spec.Containers[i].Resources.Requests[name] = quantity
}
pod.Spec.Containers[i].Resources = resources
}
}
func getOwner(gs *carrierv1alpha1.GameServer) string {
if gs.Labels[util.SquadNameLabelKey] != "" {
return gs.Labels[util.SquadNameLabelKey]
}
if gs.Labels[util.GameServerSetLabelKey] != "" {
return gs.Labels[util.GameServerSetLabelKey]
}
return string(gs.UID)
}
func findPorts(gs *carrierv1alpha1.GameServer) []int {
var ports []int
if len(gs.Spec.Ports) == 0 {
return ports
}
for _, port := range gs.Spec.Ports {
if port.PortPolicy == carrierv1alpha1.LoadBalancer {
continue
}
if port.HostPort != nil {
ports = append(ports, int(*port.HostPort))
}
hpr := port.HostPortRange
if port.HostPortRange != nil {
for i := hpr.MinPort; i <= hpr.MaxPort; i++ {
ports = append(ports, int(i))
}
}
}
return ports
}
func findDynamicPortNumber(gs *carrierv1alpha1.GameServer) int {
count := 0
if len(gs.Spec.Ports) == 0 {
return 0
}
for _, port := range gs.Spec.Ports {
if port.PortPolicy != carrierv1alpha1.Dynamic {
continue
}
if port.ContainerPort != nil {
count++
}
}
return count
}
func findStaticPorts(gs *carrierv1alpha1.GameServer) []int {
var ports []int
if len(gs.Spec.Ports) == 0 {
return nil
}
for _, port := range gs.Spec.Ports {
if port.PortPolicy == carrierv1alpha1.Static {
continue
}
if port.ContainerPort != nil && port.HostPort != nil {
ports = append(ports, int(*port.HostPort))
}
}
return ports
}
const (
PortType = "Port"
PortRangeType = "PortRange"
)
func getAllocateType(gs *carrierv1alpha1.GameServer) string {
if len(gs.Spec.Ports) == 0 {
return ""
}
for _, port := range gs.Spec.Ports {
if port.PortPolicy != carrierv1alpha1.Dynamic {
continue
}
if port.ContainerPort != nil {
return PortType
}
if port.ContainerPortRange != nil {
return PortRangeType
}
}
return ""
}
func setHostPort(gs *carrierv1alpha1.GameServer, ports []int) {
for i, port := range gs.Spec.Ports {
if port.PortPolicy != carrierv1alpha1.Dynamic {
continue
}
if port.ContainerPort != nil {
gs.Spec.Ports[i].HostPort = new(int32)
*gs.Spec.Ports[i].HostPort = int32(ports[i])
}
}
}
func setHostPortRange(gs *carrierv1alpha1.GameServer, ports []int) {
for i, port := range gs.Spec.Ports {
if port.PortPolicy == carrierv1alpha1.LoadBalancer {
continue
}
if port.ContainerPortRange != nil {
gs.Spec.Ports[i].HostPortRange = &carrierv1alpha1.PortRange{
MinPort: int32(ports[0]),
MaxPort: int32(ports[len(ports)-1]),
}
}
}
}