This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
826 lines (719 loc) · 28.9 KB
/
controller.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// Copyright 2018 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 gameservers
import (
"encoding/json"
"fmt"
"sync"
"time"
"agones.dev/agones/pkg/apis/stable"
"agones.dev/agones/pkg/apis/stable/v1alpha1"
"agones.dev/agones/pkg/client/clientset/versioned"
getterv1alpha1 "agones.dev/agones/pkg/client/clientset/versioned/typed/stable/v1alpha1"
"agones.dev/agones/pkg/client/informers/externalversions"
listerv1alpha1 "agones.dev/agones/pkg/client/listers/stable/v1alpha1"
"agones.dev/agones/pkg/util/crd"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/webhooks"
"agones.dev/agones/pkg/util/workerqueue"
"github.com/heptiolabs/healthcheck"
"github.com/mattbaird/jsonpatch"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
admv1beta1 "k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
extclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
corelisterv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
)
var (
errPodNotFound = errors.New("A Pod for this GameServer Was Not Found")
)
// Controller is a the main GameServer crd controller
type Controller struct {
logger *logrus.Entry
sidecarImage string
alwaysPullSidecarImage bool
sidecarCPURequest resource.Quantity
sidecarCPULimit resource.Quantity
sdkServiceAccount string
crdGetter v1beta1.CustomResourceDefinitionInterface
podGetter typedcorev1.PodsGetter
podLister corelisterv1.PodLister
podSynced cache.InformerSynced
gameServerGetter getterv1alpha1.GameServersGetter
gameServerLister listerv1alpha1.GameServerLister
gameServerSynced cache.InformerSynced
nodeLister corelisterv1.NodeLister
portAllocator *PortAllocator
healthController *HealthController
workerqueue *workerqueue.WorkerQueue
creationWorkerQueue *workerqueue.WorkerQueue // handles creation only
deletionWorkerQueue *workerqueue.WorkerQueue // handles deletion only
stop <-chan struct {
}
recorder record.EventRecorder
}
// NewController returns a new gameserver crd controller
func NewController(
wh *webhooks.WebHook,
health healthcheck.Handler,
minPort, maxPort int32,
sidecarImage string,
alwaysPullSidecarImage bool,
sidecarCPURequest resource.Quantity,
sidecarCPULimit resource.Quantity,
sdkServiceAccount string,
kubeClient kubernetes.Interface,
kubeInformerFactory informers.SharedInformerFactory,
extClient extclientset.Interface,
agonesClient versioned.Interface,
agonesInformerFactory externalversions.SharedInformerFactory) *Controller {
pods := kubeInformerFactory.Core().V1().Pods()
gameServers := agonesInformerFactory.Stable().V1alpha1().GameServers()
gsInformer := gameServers.Informer()
c := &Controller{
sidecarImage: sidecarImage,
sidecarCPULimit: sidecarCPULimit,
sidecarCPURequest: sidecarCPURequest,
alwaysPullSidecarImage: alwaysPullSidecarImage,
sdkServiceAccount: sdkServiceAccount,
crdGetter: extClient.ApiextensionsV1beta1().CustomResourceDefinitions(),
podGetter: kubeClient.CoreV1(),
podLister: pods.Lister(),
podSynced: pods.Informer().HasSynced,
gameServerGetter: agonesClient.StableV1alpha1(),
gameServerLister: gameServers.Lister(),
gameServerSynced: gsInformer.HasSynced,
nodeLister: kubeInformerFactory.Core().V1().Nodes().Lister(),
portAllocator: NewPortAllocator(minPort, maxPort, kubeInformerFactory, agonesInformerFactory),
healthController: NewHealthController(kubeClient, agonesClient, kubeInformerFactory, agonesInformerFactory),
}
c.logger = runtime.NewLoggerWithType(c)
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(c.logger.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
c.recorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "gameserver-controller"})
c.workerqueue = workerqueue.NewWorkerQueueWithRateLimiter(c.syncGameServer, c.logger, stable.GroupName+".GameServerController", fastRateLimiter())
c.creationWorkerQueue = workerqueue.NewWorkerQueueWithRateLimiter(c.syncGameServer, c.logger, stable.GroupName+".GameServerControllerCreation", fastRateLimiter())
c.deletionWorkerQueue = workerqueue.NewWorkerQueueWithRateLimiter(c.syncGameServer, c.logger, stable.GroupName+".GameServerControllerDeletion", fastRateLimiter())
health.AddLivenessCheck("gameserver-workerqueue", healthcheck.Check(c.workerqueue.Healthy))
health.AddLivenessCheck("gameserver-creation-workerqueue", healthcheck.Check(c.creationWorkerQueue.Healthy))
health.AddLivenessCheck("gameserver-deletion-workerqueue", healthcheck.Check(c.deletionWorkerQueue.Healthy))
wh.AddHandler("/mutate", v1alpha1.Kind("GameServer"), admv1beta1.Create, c.creationMutationHandler)
wh.AddHandler("/validate", v1alpha1.Kind("GameServer"), admv1beta1.Create, c.creationValidationHandler)
gsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueGameServerBasedOnState,
UpdateFunc: func(oldObj, newObj interface{}) {
// no point in processing unless there is a State change
oldGs := oldObj.(*v1alpha1.GameServer)
newGs := newObj.(*v1alpha1.GameServer)
if oldGs.Status.State != newGs.Status.State || oldGs.ObjectMeta.DeletionTimestamp != newGs.ObjectMeta.DeletionTimestamp {
c.enqueueGameServerBasedOnState(newGs)
}
},
})
// track pod deletions, for when GameServers are deleted
pods.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, newObj interface{}) {
oldPod := oldObj.(*corev1.Pod)
if isGameServerPod(oldPod) {
newPod := newObj.(*corev1.Pod)
// node name has changed -- i.e. it has been scheduled
if oldPod.Spec.NodeName != newPod.Spec.NodeName {
owner := metav1.GetControllerOf(newPod)
c.workerqueue.Enqueue(cache.ExplicitKey(newPod.ObjectMeta.Namespace + "/" + owner.Name))
}
}
},
DeleteFunc: func(obj interface{}) {
// Could be a DeletedFinalStateUnknown, in which case, just ignore it
pod, ok := obj.(*corev1.Pod)
if ok && isGameServerPod(pod) {
owner := metav1.GetControllerOf(pod)
c.workerqueue.Enqueue(cache.ExplicitKey(pod.ObjectMeta.Namespace + "/" + owner.Name))
}
},
})
return c
}
func (c *Controller) enqueueGameServerBasedOnState(item interface{}) {
gs := item.(*v1alpha1.GameServer)
switch gs.Status.State {
case v1alpha1.GameServerStatePortAllocation,
v1alpha1.GameServerStateCreating:
c.creationWorkerQueue.Enqueue(gs)
case v1alpha1.GameServerStateShutdown:
c.deletionWorkerQueue.Enqueue(gs)
default:
c.workerqueue.Enqueue(gs)
}
}
// fastRateLimiter returns a fast rate limiter, without exponential back-off.
func fastRateLimiter() workqueue.RateLimiter {
const numFastRetries = 5
const fastDelay = 20 * time.Millisecond // first few retries up to 'numFastRetries' are fast
const slowDelay = 500 * time.Millisecond // subsequent retries are slow
return workqueue.NewItemFastSlowRateLimiter(fastDelay, slowDelay, numFastRetries)
}
// creationMutationHandler is the handler for the mutating webhook that sets the
// the default values on the GameServer
// Should only be called on gameserver create operations.
// nolint:dupl
func (c *Controller) creationMutationHandler(review admv1beta1.AdmissionReview) (admv1beta1.AdmissionReview, error) {
c.logger.WithField("review", review).Info("creationMutationHandler")
obj := review.Request.Object
gs := &v1alpha1.GameServer{}
err := json.Unmarshal(obj.Raw, gs)
if err != nil {
return review, errors.Wrapf(err, "error unmarshalling original GameServer json: %s", obj.Raw)
}
// This is the main logic of this function
// the rest is really just json plumbing
gs.ApplyDefaults()
newGS, err := json.Marshal(gs)
if err != nil {
return review, errors.Wrapf(err, "error marshalling default applied GameSever %s to json", gs.ObjectMeta.Name)
}
patch, err := jsonpatch.CreatePatch(obj.Raw, newGS)
if err != nil {
return review, errors.Wrapf(err, "error creating patch for GameServer %s", gs.ObjectMeta.Name)
}
json, err := json.Marshal(patch)
if err != nil {
return review, errors.Wrapf(err, "error creating json for patch for GameServer %s", gs.ObjectMeta.Name)
}
c.logger.WithField("gs", gs.ObjectMeta.Name).WithField("patch", string(json)).Infof("patch created!")
pt := admv1beta1.PatchTypeJSONPatch
review.Response.PatchType = &pt
review.Response.Patch = json
return review, nil
}
// creationValidationHandler that validates a GameServer when it is created
// Should only be called on gameserver create operations.
func (c *Controller) creationValidationHandler(review admv1beta1.AdmissionReview) (admv1beta1.AdmissionReview, error) {
c.logger.WithField("review", review).Info("creationValidationHandler")
obj := review.Request.Object
gs := &v1alpha1.GameServer{}
err := json.Unmarshal(obj.Raw, gs)
if err != nil {
return review, errors.Wrapf(err, "error unmarshalling original GameServer json: %s", obj.Raw)
}
ok, causes := gs.Validate()
if !ok {
review.Response.Allowed = false
details := metav1.StatusDetails{
Name: review.Request.Name,
Group: review.Request.Kind.Group,
Kind: review.Request.Kind.Kind,
Causes: causes,
}
review.Response.Result = &metav1.Status{
Status: metav1.StatusFailure,
Message: "GameServer configuration is invalid",
Reason: metav1.StatusReasonInvalid,
Details: &details,
}
c.logger.WithField("review", review).Info("Invalid GameServer")
return review, nil
}
return review, nil
}
// Run the GameServer controller. Will block until stop is closed.
// Runs threadiness number workers to process the rate limited queue
func (c *Controller) Run(workers int, stop <-chan struct{}) error {
c.stop = stop
err := crd.WaitForEstablishedCRD(c.crdGetter, "gameservers.stable.agones.dev", c.logger)
if err != nil {
return err
}
c.logger.Info("Wait for cache sync")
if !cache.WaitForCacheSync(stop, c.gameServerSynced) {
return errors.New("failed to wait for caches to sync")
}
// Run the Port Allocator
if err = c.portAllocator.Run(stop); err != nil {
return errors.Wrap(err, "error running the port allocator")
}
// Run the Health Controller
go c.healthController.Run(stop)
// start work queues
var wg sync.WaitGroup
startWorkQueue := func(wq *workerqueue.WorkerQueue) {
wg.Add(1)
go func() {
defer wg.Done()
wq.Run(workers, stop)
}()
}
startWorkQueue(c.workerqueue)
startWorkQueue(c.creationWorkerQueue)
startWorkQueue(c.deletionWorkerQueue)
wg.Wait()
return nil
}
// syncGameServer synchronises the Pods for the GameServers.
// and reacts to status changes that can occur through the client SDK
func (c *Controller) syncGameServer(key string) error {
c.logger.WithField("key", key).Info("Synchronising")
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
// don't return an error, as we don't want this retried
runtime.HandleError(c.logger.WithField("key", key), errors.Wrapf(err, "invalid resource key"))
return nil
}
gs, err := c.gameServerLister.GameServers(namespace).Get(name)
if err != nil {
if k8serrors.IsNotFound(err) {
c.logger.WithField("key", key).Info("GameServer is no longer available for syncing")
return nil
}
return errors.Wrapf(err, "error retrieving GameServer %s from namespace %s", name, namespace)
}
if gs, err = c.syncGameServerDeletionTimestamp(gs); err != nil {
return err
}
if gs, err = c.syncGameServerPortAllocationState(gs); err != nil {
return err
}
if gs, err = c.syncGameServerCreatingState(gs); err != nil {
return err
}
if gs, err = c.syncGameServerStartingState(gs); err != nil {
return err
}
if gs, err = c.syncGameServerRequestReadyState(gs); err != nil {
return err
}
if gs, err = c.syncDevelopmentGameServer(gs); err != nil {
return err
}
if err = c.syncGameServerShutdownState(gs); err != nil {
return err
}
return nil
}
// syncGameServerDeletionTimestamp if the deletion timestamp is non-zero
// then do one of two things:
// - if the GameServer has Pods running, delete them
// - if there no pods, remove the finalizer
func (c *Controller) syncGameServerDeletionTimestamp(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
if gs.ObjectMeta.DeletionTimestamp.IsZero() {
return gs, nil
}
c.logger.WithField("gs", gs).Info("Syncing with Deletion Timestamp")
pods, err := c.listGameServerPods(gs)
if err != nil {
return gs, err
}
if len(pods) > 0 {
c.logger.WithField("pods", pods).WithField("gsName", gs.ObjectMeta.Name).Info("Found pods, deleting")
for _, p := range pods {
err = c.podGetter.Pods(p.ObjectMeta.Namespace).Delete(p.ObjectMeta.Name, nil)
if err != nil {
return gs, errors.Wrapf(err, "error deleting pod for GameServer %s, %s", gs.ObjectMeta.Name, p.ObjectMeta.Name)
}
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), fmt.Sprintf("Deleting Pod %s", p.ObjectMeta.Name))
}
return gs, nil
}
gsCopy := gs.DeepCopy()
// remove the finalizer for this controller
var fin []string
for _, f := range gsCopy.ObjectMeta.Finalizers {
if f != stable.GroupName {
fin = append(fin, f)
}
}
gsCopy.ObjectMeta.Finalizers = fin
c.logger.WithField("gs", gsCopy).Infof("No pods found, removing finalizer %s", stable.GroupName)
gs, err = c.gameServerGetter.GameServers(gsCopy.ObjectMeta.Namespace).Update(gsCopy)
return gs, errors.Wrapf(err, "error removing finalizer for GameServer %s", gsCopy.ObjectMeta.Name)
}
// syncGameServerPortAllocationState gives a port to a dynamically allocating GameServer
func (c *Controller) syncGameServerPortAllocationState(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
if !(gs.Status.State == v1alpha1.GameServerStatePortAllocation && gs.ObjectMeta.DeletionTimestamp.IsZero()) {
return gs, nil
}
gsCopy := c.portAllocator.Allocate(gs.DeepCopy())
gsCopy.Status.State = v1alpha1.GameServerStateCreating
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "Port allocated")
c.logger.WithField("gs", gsCopy).Info("Syncing Port Allocation GameServerState")
gs, err := c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(gsCopy)
if err != nil {
// if the GameServer doesn't get updated with the port data, then put the port
// back in the pool, as it will get retried on the next pass
c.portAllocator.DeAllocate(gsCopy)
return gs, errors.Wrapf(err, "error updating GameServer %s to default values", gs.Name)
}
return gs, nil
}
// syncGameServerCreatingState checks if the GameServer is in the Creating state, and if so
// creates a Pod for the GameServer and moves the state to Starting
func (c *Controller) syncGameServerCreatingState(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
if !(gs.Status.State == v1alpha1.GameServerStateCreating && gs.ObjectMeta.DeletionTimestamp.IsZero()) {
return gs, nil
}
if _, isDev := gs.GetDevAddress(); isDev {
return gs, nil
}
c.logger.WithField("gs", gs).Info("Syncing Create State")
// Wait for pod cache sync, so that we don't end up with multiple pods for a GameServer
if !(cache.WaitForCacheSync(c.stop, c.podSynced)) {
return nil, errors.New("could not sync pod cache state")
}
// Maybe something went wrong, and the pod was created, but the state was never moved to Starting, so let's check
ret, err := c.listGameServerPods(gs)
if err != nil {
return nil, err
}
if len(ret) == 0 {
gs, err = c.createGameServerPod(gs)
if err != nil || gs.Status.State == v1alpha1.GameServerStateError {
return gs, err
}
}
gsCopy := gs.DeepCopy()
gsCopy.Status.State = v1alpha1.GameServerStateStarting
gs, err = c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(gsCopy)
if err != nil {
return gs, errors.Wrapf(err, "error updating GameServer %s to Starting state", gs.Name)
}
return gs, nil
}
// syncDevelopmentGameServer manages advances a development gameserver to Ready status and registers its address and ports.
func (c *Controller) syncDevelopmentGameServer(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
// do not sync if the server is deleting.
if !(gs.ObjectMeta.DeletionTimestamp.IsZero()) {
return gs, nil
}
// Get the development IP address
devIPAddress, isDevGs := gs.GetDevAddress()
if !isDevGs {
return gs, nil
}
if !(gs.Status.State == v1alpha1.GameServerStateReady) {
c.logger.WithField("gs", gs).Infof("%s is a development game server and will not be managed by Agones.", gs.Name)
}
gsCopy := gs.DeepCopy()
ports := []v1alpha1.GameServerStatusPort{}
for _, p := range gs.Spec.Ports {
ports = append(ports, p.Status())
}
// TODO: Use UpdateStatus() when it's available.
gsCopy.Status.State = v1alpha1.GameServerStateReady
gsCopy.Status.Ports = ports
gsCopy.Status.Address = devIPAddress
gsCopy.Status.NodeName = devIPAddress
gs, err := c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(gsCopy)
if err != nil {
return gs, errors.Wrapf(err, "error updating GameServer %s to %v status", gs.Name, gs.Status)
}
return gs, nil
}
// createGameServerPod creates the backing Pod for a given GameServer
func (c *Controller) createGameServerPod(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
sidecar := c.sidecar(gs)
var pod *corev1.Pod
pod, err := gs.Pod(sidecar)
// this shouldn't happen, but if it does.
if err != nil {
c.logger.WithField("gameserver", gs).WithError(err).Error("error creating pod from Game Server")
gs, err = c.moveToErrorState(gs, err.Error())
return gs, err
}
// apply the sdk service account
if pod.Spec.ServiceAccountName == "" {
pod.Spec.ServiceAccountName = c.sdkServiceAccount
}
c.addGameServerHealthCheck(gs, pod)
c.logger.WithField("pod", pod).Info("creating Pod for GameServer")
pod, err = c.podGetter.Pods(gs.ObjectMeta.Namespace).Create(pod)
if k8serrors.IsAlreadyExists(err) {
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "Pod already exists, reused")
return gs, nil
}
if err != nil {
if k8serrors.IsInvalid(err) {
c.logger.WithField("pod", pod).WithField("gameserver", gs).Errorf("Pod created is invalid")
gs, err = c.moveToErrorState(gs, err.Error())
return gs, err
}
return gs, errors.Wrapf(err, "error creating Pod for GameServer %s", gs.Name)
}
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State),
fmt.Sprintf("Pod %s created", pod.ObjectMeta.Name))
return gs, nil
}
// sidecar creates the sidecar container for a given GameServer
func (c *Controller) sidecar(gs *v1alpha1.GameServer) corev1.Container {
sidecar := corev1.Container{
Name: "agones-gameserver-sidecar",
Image: c.sidecarImage,
Env: []corev1.EnvVar{
{
Name: "GAMESERVER_NAME",
Value: gs.ObjectMeta.Name,
},
{
Name: "POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
},
Resources: corev1.ResourceRequirements{},
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.FromInt(8080),
},
},
InitialDelaySeconds: 3,
PeriodSeconds: 3,
},
}
if !c.sidecarCPURequest.IsZero() {
sidecar.Resources.Requests = corev1.ResourceList{corev1.ResourceCPU: c.sidecarCPURequest}
}
if !c.sidecarCPULimit.IsZero() {
sidecar.Resources.Limits = corev1.ResourceList{corev1.ResourceCPU: c.sidecarCPULimit}
}
if c.alwaysPullSidecarImage {
sidecar.ImagePullPolicy = corev1.PullAlways
}
return sidecar
}
// addGameServerHealthCheck adds the http health check to the GameServer container
func (c *Controller) addGameServerHealthCheck(gs *v1alpha1.GameServer, pod *corev1.Pod) {
if !gs.Spec.Health.Disabled {
for i, c := range pod.Spec.Containers {
if c.Name == gs.Spec.Container {
if c.LivenessProbe == nil {
c.LivenessProbe = &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/gshealthz",
Port: intstr.FromInt(8080),
},
},
InitialDelaySeconds: gs.Spec.Health.InitialDelaySeconds,
PeriodSeconds: gs.Spec.Health.PeriodSeconds,
FailureThreshold: gs.Spec.Health.FailureThreshold,
}
pod.Spec.Containers[i] = c
}
break
}
}
}
}
// syncGameServerStartingState looks for a pod that has been scheduled for this GameServer
// and then sets the Status > Address and Ports values.
func (c *Controller) syncGameServerStartingState(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
if !(gs.Status.State == v1alpha1.GameServerStateStarting && gs.ObjectMeta.DeletionTimestamp.IsZero()) {
return gs, nil
}
if _, isDev := gs.GetDevAddress(); isDev {
return gs, nil
}
c.logger.WithField("gs", gs).Info("Syncing Starting GameServerState")
// there should be a pod (although it may not have a scheduled container),
// so if there is an error of any kind, then move this to queue backoff
pod, err := c.gameServerPod(gs)
if err != nil {
return nil, err
}
gsCopy := gs.DeepCopy()
// if we can't get the address, then go into queue backoff
gsCopy, err = c.applyGameServerAddressAndPort(gsCopy, pod)
if err != nil {
return gs, err
}
gsCopy.Status.State = v1alpha1.GameServerStateScheduled
gs, err = c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(gsCopy)
if err != nil {
return gs, errors.Wrapf(err, "error updating GameServer %s to Scheduled state", gs.Name)
}
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "Address and port populated")
return gs, nil
}
// applyGameServerAddressAndPort gets the backing Pod for the GamesServer,
// and sets the allocated Address and Port values to it and returns it.
func (c *Controller) applyGameServerAddressAndPort(gs *v1alpha1.GameServer, pod *corev1.Pod) (*v1alpha1.GameServer, error) {
addr, err := c.address(pod)
if err != nil {
return gs, errors.Wrapf(err, "error getting external address for GameServer %s", gs.ObjectMeta.Name)
}
gs.Status.Address = addr
gs.Status.NodeName = pod.Spec.NodeName
// HostPort is always going to be populated, even when dynamic
// This will be a double up of information, but it will be easier to read
gs.Status.Ports = make([]v1alpha1.GameServerStatusPort, len(gs.Spec.Ports))
for i, p := range gs.Spec.Ports {
gs.Status.Ports[i] = p.Status()
}
return gs, nil
}
// syncGameServerRequestReadyState checks if the Game Server is Requesting to be ready,
// and then adds the IP and Port information to the Status and marks the GameServer
// as Ready
func (c *Controller) syncGameServerRequestReadyState(gs *v1alpha1.GameServer) (*v1alpha1.GameServer, error) {
if !(gs.Status.State == v1alpha1.GameServerStateRequestReady && gs.ObjectMeta.DeletionTimestamp.IsZero()) ||
gs.Status.State == v1alpha1.GameServerStateUnhealthy {
return gs, nil
}
if _, isDev := gs.GetDevAddress(); isDev {
return gs, nil
}
c.logger.WithField("gs", gs).Info("Syncing RequestReady State")
gsCopy := gs.DeepCopy()
// if the address hasn't been populated, and the Ready request comes
// before the controller has had a chance to do it, then
// do it here instead
addressPopulated := false
if gs.Status.NodeName == "" {
addressPopulated = true
pod, err := c.gameServerPod(gs)
// errPodNotFound should never happen, and if it does -- something bad happened,
// so go into workerqueue backoff.
if err != nil {
return nil, err
}
gsCopy, err = c.applyGameServerAddressAndPort(gsCopy, pod)
if err != nil {
return gs, err
}
}
gsCopy.Status.State = v1alpha1.GameServerStateReady
gs, err := c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(gsCopy)
if err != nil {
return gs, errors.Wrapf(err, "error setting Ready, Port and address on GameServer %s Status", gs.ObjectMeta.Name)
}
if addressPopulated {
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "Address and port populated")
}
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "SDK.Ready() executed")
return gs, nil
}
// syncGameServerShutdownState deletes the GameServer (and therefore the backing Pod) if it is in shutdown state
func (c *Controller) syncGameServerShutdownState(gs *v1alpha1.GameServer) error {
if !(gs.Status.State == v1alpha1.GameServerStateShutdown && gs.ObjectMeta.DeletionTimestamp.IsZero()) {
return nil
}
c.logger.WithField("gs", gs).Info("Syncing Shutdown State")
// be explicit about where to delete.
p := metav1.DeletePropagationBackground
err := c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Delete(gs.ObjectMeta.Name, &metav1.DeleteOptions{PropagationPolicy: &p})
if err != nil {
return errors.Wrapf(err, "error deleting Game Server %s", gs.ObjectMeta.Name)
}
c.recorder.Event(gs, corev1.EventTypeNormal, string(gs.Status.State), "Deletion started")
return nil
}
// moveToErrorState moves the GameServer to the error state
func (c *Controller) moveToErrorState(gs *v1alpha1.GameServer, msg string) (*v1alpha1.GameServer, error) {
copy := gs.DeepCopy()
copy.Status.State = v1alpha1.GameServerStateError
gs, err := c.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(copy)
if err != nil {
return gs, errors.Wrapf(err, "error moving GameServer %s to Error State", gs.ObjectMeta.Name)
}
c.recorder.Event(gs, corev1.EventTypeWarning, string(gs.Status.State), msg)
return gs, nil
}
// gameServerPod returns the Pod for this Game Server, or an error if there are none,
// or it cannot be determined (there are more than one, which should not happen)
func (c *Controller) gameServerPod(gs *v1alpha1.GameServer) (*corev1.Pod, error) {
// If the game server is a dev server we do not create a pod for it, return an empty pod.
if _, isDev := gs.GetDevAddress(); isDev {
return &corev1.Pod{}, nil
}
pods, err := c.listGameServerPods(gs)
if err != nil {
return nil, err
}
len := len(pods)
if len == 0 {
return nil, errPodNotFound
}
if len > 1 {
return nil, errors.Errorf("Found %d pods for Game Server %s", len, gs.ObjectMeta.Name)
}
return pods[0], nil
}
// listGameServerPods returns all the Pods that the GameServer created.
// This should only ever be one.
func (c *Controller) listGameServerPods(gs *v1alpha1.GameServer) ([]*corev1.Pod, error) {
pods, err := c.podLister.List(labels.SelectorFromSet(labels.Set{v1alpha1.GameServerPodLabel: gs.ObjectMeta.Name}))
if err != nil {
return pods, errors.Wrapf(err, "error checking if pod exists for GameServer %s", gs.Name)
}
// there is a small chance that the GameServer name is not unique, and a Pod for a previous
// GameServer is has yet to Terminate so check its controller, just to be sure.
var result []*corev1.Pod
for _, p := range pods {
if metav1.IsControlledBy(p, gs) {
result = append(result, p)
}
}
return result, nil
}
// address returns the IP that the given Pod is being run on
// This should be the externalIP, but if the externalIP is
// not set, it will fall back to the internalIP with a warning.
// (basically because minikube only has an internalIP)
func (c *Controller) address(pod *corev1.Pod) (string, error) {
node, err := c.nodeLister.Get(pod.Spec.NodeName)
if err != nil {
return "", errors.Wrapf(err, "error retrieving node %s for Pod %s", pod.Spec.NodeName, pod.ObjectMeta.Name)
}
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeExternalIP {
return a.Address, nil
}
}
// minikube only has an InternalIP on a Node, so we'll fall back to that.
c.logger.WithField("node", node.ObjectMeta.Name).Warn("Could not find ExternalIP. Falling back to Internal")
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeInternalIP {
return a.Address, nil
}
}
return "", errors.Errorf("Could not find an address for Node: %s", node.ObjectMeta.Name)
}
// isGameServerPod returns if this Pod is a Pod that comes from a GameServer
func isGameServerPod(pod *corev1.Pod) bool {
if v1alpha1.GameServerRolePodSelector.Matches(labels.Set(pod.ObjectMeta.Labels)) {
owner := metav1.GetControllerOf(pod)
return owner != nil && owner.Kind == "GameServer"
}
return false
}