-
Notifications
You must be signed in to change notification settings - Fork 813
/
main.go
614 lines (548 loc) · 27.1 KB
/
main.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
// Copyright 2018 Google LLC 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.
// Controller for gameservers
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"agones.dev/agones/pkg"
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
"agones.dev/agones/pkg/client/clientset/versioned"
"agones.dev/agones/pkg/client/informers/externalversions"
"agones.dev/agones/pkg/cloudproduct"
"agones.dev/agones/pkg/fleetautoscalers"
"agones.dev/agones/pkg/fleets"
"agones.dev/agones/pkg/gameservers"
"agones.dev/agones/pkg/gameserversets"
"agones.dev/agones/pkg/metrics"
"agones.dev/agones/pkg/portallocator"
"agones.dev/agones/pkg/util/httpserver"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
"github.com/google/uuid"
"github.com/heptiolabs/healthcheck"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/natefinch/lumberjack.v2"
corev1 "k8s.io/api/core/v1"
extclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
const (
enableStackdriverMetricsFlag = "stackdriver-exporter"
stackdriverLabels = "stackdriver-labels"
enablePrometheusMetricsFlag = "prometheus-exporter"
projectIDFlag = "gcp-project-id"
sidecarImageFlag = "sidecar-image"
sidecarCPURequestFlag = "sidecar-cpu-request"
sidecarCPULimitFlag = "sidecar-cpu-limit"
sidecarMemoryRequestFlag = "sidecar-memory-request"
sidecarMemoryLimitFlag = "sidecar-memory-limit"
sidecarRunAsUserFlag = "sidecar-run-as-user"
sdkServerAccountFlag = "sdk-service-account"
pullSidecarFlag = "always-pull-sidecar"
minPortFlag = "min-port"
maxPortFlag = "max-port"
additionalPortRangesFlag = "additional-port-ranges"
certFileFlag = "cert-file"
keyFileFlag = "key-file"
numWorkersFlag = "num-workers"
apiServerSustainedQPSFlag = "api-server-qps"
apiServerBurstQPSFlag = "api-server-qps-burst"
logDirFlag = "log-dir"
logLevelFlag = "log-level"
logSizeLimitMBFlag = "log-size-limit-mb"
kubeconfigFlag = "kubeconfig"
allocationBatchWaitTime = "allocation-batch-wait-time"
defaultResync = 30 * time.Second
podNamespace = "pod-namespace"
leaderElectionFlag = "leader-election"
maxCreationParallelismFlag = "max-creation-parallelism"
maxGameServerCreationsPerBatchFlag = "max-game-server-creations-per-batch"
maxDeletionParallelismFlag = "max-deletion-parallelism"
maxGameServerDeletionsPerBatchFlag = "max-game-server-deletions-per-batch"
maxPodPendingCountFlag = "max-pod-pending-count"
)
var (
logger = runtime.NewLoggerWithSource("main")
)
func setupLogging(logDir string, logSizeLimitMB int) {
logFileName := filepath.Join(logDir, "agones-controller-"+time.Now().Format("20060102_150405")+".log")
const maxLogSizeMB = 100
maxBackups := (logSizeLimitMB - maxLogSizeMB) / maxLogSizeMB
logger.WithField("filename", logFileName).WithField("numbackups", maxBackups).Info("logging to file")
logrus.SetOutput(
io.MultiWriter(
logrus.StandardLogger().Out,
&lumberjack.Logger{
Filename: logFileName,
MaxSize: maxLogSizeMB,
MaxBackups: maxBackups,
},
),
)
}
// main starts the operator for the gameserver CRD
func main() {
ctx, cancel := signals.NewSigKillContext()
ctlConf := parseEnvFlags()
if ctlConf.LogDir != "" {
setupLogging(ctlConf.LogDir, ctlConf.LogSizeLimitMB)
}
logger.WithField("logLevel", ctlConf.LogLevel).Info("Setting LogLevel configuration")
level, err := logrus.ParseLevel(strings.ToLower(ctlConf.LogLevel))
if err == nil {
runtime.SetLevel(level)
} else {
logger.WithError(err).Info("Specified wrong Logging.SdkServer. Setting default loglevel - Info")
runtime.SetLevel(logrus.InfoLevel)
}
logger.WithField("version", pkg.Version).WithField("featureGates", runtime.EncodeFeatures()).
WithField("ctlConf", ctlConf).Info("starting gameServer operator...")
if errs := ctlConf.validate(); len(errs) != 0 {
for _, err := range errs {
logger.Error(err)
}
logger.Fatal("Could not create controller from environment or flags")
}
// if the kubeconfig fails InClusterBuildConfig will try in cluster config
clientConf, err := runtime.InClusterBuildConfig(logger, ctlConf.KubeConfig)
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
clientConf.QPS = float32(ctlConf.APIServerSustainedQPS)
clientConf.Burst = ctlConf.APIServerBurstQPS
kubeClient, err := kubernetes.NewForConfig(clientConf)
if err != nil {
logger.WithError(err).Fatal("Could not create the kubernetes clientset")
}
extClient, err := extclientset.NewForConfig(clientConf)
if err != nil {
logger.WithError(err).Fatal("Could not create the api extension clientset")
}
agonesClient, err := versioned.NewForConfig(clientConf)
if err != nil {
logger.WithError(err).Fatal("Could not create the agones api clientset")
}
controllerHooks, err := cloudproduct.NewFromFlag(ctx, kubeClient)
if err != nil {
logger.WithError(err).Fatal("Could not initialize cloud product")
}
agonesInformerFactory := externalversions.NewSharedInformerFactory(agonesClient, defaultResync)
kubeInformerFactory := informers.NewSharedInformerFactory(kubeClient, defaultResync)
server := &httpserver.Server{Logger: logger}
var rs []runner
var health healthcheck.Handler
metricsConf := metrics.Config{
Stackdriver: ctlConf.Stackdriver,
PrometheusMetrics: ctlConf.PrometheusMetrics,
GCPProjectID: ctlConf.GCPProjectID,
StackdriverLabels: ctlConf.StackdriverLabels,
}
health, closer := metrics.SetupMetrics(metricsConf, server)
defer closer()
// If we are using Prometheus only exporter we can make reporting more often,
// every 1 seconds, if we are using Stackdriver we would use 60 seconds reporting period,
// which is a requirements of Stackdriver, otherwise most of time series would be invalid for Stackdriver
metrics.SetReportingPeriod(ctlConf.PrometheusMetrics, ctlConf.Stackdriver)
// Add metrics controller only if we configure one of metrics exporters
if ctlConf.PrometheusMetrics || ctlConf.Stackdriver {
rs = append(rs, metrics.NewController(kubeClient, agonesClient, kubeInformerFactory, agonesInformerFactory))
}
server.Handle("/", health)
gsCounter := gameservers.NewPerNodeCounter(kubeInformerFactory, agonesInformerFactory)
gsController := gameservers.NewController(controllerHooks, health,
ctlConf.PortRanges, ctlConf.SidecarImage, ctlConf.AlwaysPullSidecar,
ctlConf.SidecarCPURequest, ctlConf.SidecarCPULimit,
ctlConf.SidecarMemoryRequest, ctlConf.SidecarMemoryLimit, ctlConf.SidecarRunAsUser, ctlConf.SdkServiceAccount,
kubeClient, kubeInformerFactory, extClient, agonesClient, agonesInformerFactory)
gsSetController := gameserversets.NewController(health, gsCounter,
kubeClient, extClient, agonesClient, agonesInformerFactory, ctlConf.MaxCreationParallelism, ctlConf.MaxDeletionParallelism, ctlConf.MaxGameServerCreationsPerBatch, ctlConf.MaxGameServerDeletionsPerBatch, ctlConf.MaxPodPendingCount)
fleetController := fleets.NewController(health, kubeClient, extClient, agonesClient, agonesInformerFactory)
fasController := fleetautoscalers.NewController(health,
kubeClient, extClient, agonesClient, agonesInformerFactory, gsCounter)
rs = append(rs,
gsCounter, gsController, gsSetController, fleetController, fasController)
runRunner := func(r runner) {
if err := r.Run(ctx, ctlConf.NumWorkers); err != nil {
logger.WithError(err).Fatalf("could not start runner! %T", r)
}
}
// Server has to be started earlier because it contains the health check.
// This allows the controller to not fail health check during install when there is replication
go runRunner(server)
whenLeader(ctx, cancel, logger, ctlConf.LeaderElection, kubeClient, ctlConf.PodNamespace, func(ctx context.Context) {
kubeInformerFactory.Start(ctx.Done())
agonesInformerFactory.Start(ctx.Done())
for _, r := range rs {
go runRunner(r)
}
<-ctx.Done()
logger.Info("Shut down agones controllers")
})
}
func parseEnvFlags() config {
exec, err := os.Executable()
if err != nil {
logger.WithError(err).Fatal("Could not get executable path")
}
base := filepath.Dir(exec)
viper.SetDefault(sidecarImageFlag, "us-docker.pkg.dev/agones-images/release/agones-sdk:"+pkg.Version)
viper.SetDefault(sidecarCPURequestFlag, "0")
viper.SetDefault(sidecarCPULimitFlag, "0")
viper.SetDefault(sidecarMemoryRequestFlag, "0")
viper.SetDefault(sidecarMemoryLimitFlag, "0")
viper.SetDefault(sidecarRunAsUserFlag, "1000")
viper.SetDefault(pullSidecarFlag, false)
viper.SetDefault(sdkServerAccountFlag, "agones-sdk")
viper.SetDefault(certFileFlag, filepath.Join(base, "certs", "server.crt"))
viper.SetDefault(keyFileFlag, filepath.Join(base, "certs", "server.key"))
viper.SetDefault(enablePrometheusMetricsFlag, true)
viper.SetDefault(enableStackdriverMetricsFlag, false)
viper.SetDefault(stackdriverLabels, "")
viper.SetDefault(allocationBatchWaitTime, 500*time.Millisecond)
viper.SetDefault(podNamespace, "agones-system")
viper.SetDefault(leaderElectionFlag, false)
viper.SetDefault(projectIDFlag, "")
viper.SetDefault(numWorkersFlag, 64)
viper.SetDefault(apiServerSustainedQPSFlag, 100)
viper.SetDefault(apiServerBurstQPSFlag, 200)
viper.SetDefault(logDirFlag, "")
viper.SetDefault(logLevelFlag, "Info")
viper.SetDefault(logSizeLimitMBFlag, 10000) // 10 GB, will be split into 100 MB chunks
viper.SetDefault(maxCreationParallelismFlag, 16)
viper.SetDefault(maxGameServerCreationsPerBatchFlag, 64)
viper.SetDefault(maxDeletionParallelismFlag, 64)
viper.SetDefault(maxGameServerDeletionsPerBatchFlag, 64)
viper.SetDefault(maxPodPendingCountFlag, 5000)
pflag.String(sidecarImageFlag, viper.GetString(sidecarImageFlag), "Flag to overwrite the GameServer sidecar image that is used. Can also use SIDECAR env variable")
pflag.String(sidecarCPULimitFlag, viper.GetString(sidecarCPULimitFlag), "Flag to overwrite the GameServer sidecar container's cpu limit. Can also use SIDECAR_CPU_LIMIT env variable")
pflag.String(sidecarCPURequestFlag, viper.GetString(sidecarCPURequestFlag), "Flag to overwrite the GameServer sidecar container's cpu request. Can also use SIDECAR_CPU_REQUEST env variable")
pflag.String(sidecarMemoryLimitFlag, viper.GetString(sidecarMemoryLimitFlag), "Flag to overwrite the GameServer sidecar container's memory limit. Can also use SIDECAR_MEMORY_LIMIT env variable")
pflag.String(sidecarMemoryRequestFlag, viper.GetString(sidecarMemoryRequestFlag), "Flag to overwrite the GameServer sidecar container's memory request. Can also use SIDECAR_MEMORY_REQUEST env variable")
pflag.Int32(sidecarRunAsUserFlag, viper.GetInt32(sidecarRunAsUserFlag), "Flag to indicate the GameServer sidecar container's UID. Can also use SIDECAR_RUN_AS_USER env variable")
pflag.Bool(pullSidecarFlag, viper.GetBool(pullSidecarFlag), "For development purposes, set the sidecar image to have a ImagePullPolicy of Always. Can also use ALWAYS_PULL_SIDECAR env variable")
pflag.String(sdkServerAccountFlag, viper.GetString(sdkServerAccountFlag), "Overwrite what service account default for GameServer Pods. Defaults to Can also use SDK_SERVICE_ACCOUNT")
pflag.Int32(minPortFlag, 0, "Required. The minimum port that that a GameServer can be allocated to. Can also use MIN_PORT env variable.")
pflag.Int32(maxPortFlag, 0, "Required. The maximum port that that a GameServer can be allocated to. Can also use MAX_PORT env variable.")
pflag.String(additionalPortRangesFlag, viper.GetString(additionalPortRangesFlag), `Optional. Named set of port ranges in JSON object format: '{"game": [5000, 6000]}'. Can Also use ADDITIONAL_PORT_RANGES env variable.`)
pflag.String(keyFileFlag, viper.GetString(keyFileFlag), "Optional. Path to the key file")
pflag.String(certFileFlag, viper.GetString(certFileFlag), "Optional. Path to the crt file")
pflag.String(kubeconfigFlag, viper.GetString(kubeconfigFlag), "Optional. kubeconfig to run the controller out of the cluster. Only use it for debugging as webhook won't works.")
pflag.Bool(enablePrometheusMetricsFlag, viper.GetBool(enablePrometheusMetricsFlag), "Flag to activate metrics of Agones. Can also use PROMETHEUS_EXPORTER env variable.")
pflag.Bool(enableStackdriverMetricsFlag, viper.GetBool(enableStackdriverMetricsFlag), "Flag to activate stackdriver monitoring metrics for Agones. Can also use STACKDRIVER_EXPORTER env variable.")
pflag.String(stackdriverLabels, viper.GetString(stackdriverLabels), "A set of default labels to add to all stackdriver metrics generated. By default metadata are automatically added using Kubernetes API and GCP metadata enpoint.")
pflag.String(projectIDFlag, viper.GetString(projectIDFlag), "GCP ProjectID used for Stackdriver, if not specified ProjectID from Application Default Credentials would be used. Can also use GCP_PROJECT_ID env variable.")
pflag.Int32(numWorkersFlag, 64, "Number of controller workers per resource type")
pflag.Int32(apiServerSustainedQPSFlag, 100, "Maximum sustained queries per second to send to the API server")
pflag.Int32(apiServerBurstQPSFlag, 200, "Maximum burst queries per second to send to the API server")
pflag.String(logDirFlag, viper.GetString(logDirFlag), "If set, store logs in a given directory.")
pflag.Int32(logSizeLimitMBFlag, 1000, "Log file size limit in MB")
pflag.Int32(maxCreationParallelismFlag, viper.GetInt32(maxCreationParallelismFlag), "Maximum number of parallelizing creation calls in GSS controller")
pflag.Int32(maxGameServerCreationsPerBatchFlag, viper.GetInt32(maxGameServerCreationsPerBatchFlag), "Maximum number of GameServer creation calls per batch")
pflag.Int32(maxDeletionParallelismFlag, viper.GetInt32(maxDeletionParallelismFlag), "Maximum number of parallelizing deletion calls in GSS controller")
pflag.Int32(maxGameServerDeletionsPerBatchFlag, viper.GetInt32(maxGameServerDeletionsPerBatchFlag), "Maximum number of GameServers deletion calls per batch")
pflag.Int32(maxPodPendingCountFlag, viper.GetInt32(maxPodPendingCountFlag), "Maximum number of pending pods per game server set")
pflag.String(logLevelFlag, viper.GetString(logLevelFlag), "Agones Log level")
pflag.Duration(allocationBatchWaitTime, viper.GetDuration(allocationBatchWaitTime), "Flag to configure the waiting period between allocations batches")
pflag.String(podNamespace, viper.GetString(podNamespace), "namespace of current pod")
pflag.Bool(leaderElectionFlag, viper.GetBool(leaderElectionFlag), "Flag to enable/disable leader election for controller pod")
cloudproduct.BindFlags()
runtime.FeaturesBindFlags()
pflag.Parse()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
runtime.Must(viper.BindEnv(sidecarImageFlag))
runtime.Must(viper.BindEnv(sidecarCPULimitFlag))
runtime.Must(viper.BindEnv(sidecarCPURequestFlag))
runtime.Must(viper.BindEnv(sidecarMemoryLimitFlag))
runtime.Must(viper.BindEnv(sidecarMemoryRequestFlag))
runtime.Must(viper.BindEnv(sidecarRunAsUserFlag))
runtime.Must(viper.BindEnv(pullSidecarFlag))
runtime.Must(viper.BindEnv(sdkServerAccountFlag))
runtime.Must(viper.BindEnv(minPortFlag))
runtime.Must(viper.BindEnv(maxPortFlag))
runtime.Must(viper.BindEnv(additionalPortRangesFlag))
runtime.Must(viper.BindEnv(keyFileFlag))
runtime.Must(viper.BindEnv(certFileFlag))
runtime.Must(viper.BindEnv(kubeconfigFlag))
runtime.Must(viper.BindEnv(enablePrometheusMetricsFlag))
runtime.Must(viper.BindEnv(enableStackdriverMetricsFlag))
runtime.Must(viper.BindEnv(stackdriverLabels))
runtime.Must(viper.BindEnv(projectIDFlag))
runtime.Must(viper.BindEnv(numWorkersFlag))
runtime.Must(viper.BindEnv(apiServerSustainedQPSFlag))
runtime.Must(viper.BindEnv(apiServerBurstQPSFlag))
runtime.Must(viper.BindEnv(logLevelFlag))
runtime.Must(viper.BindEnv(logDirFlag))
runtime.Must(viper.BindEnv(logSizeLimitMBFlag))
runtime.Must(viper.BindEnv(maxCreationParallelismFlag))
runtime.Must(viper.BindEnv(maxGameServerCreationsPerBatchFlag))
runtime.Must(viper.BindEnv(maxDeletionParallelismFlag))
runtime.Must(viper.BindEnv(maxGameServerDeletionsPerBatchFlag))
runtime.Must(viper.BindEnv(maxPodPendingCountFlag))
runtime.Must(viper.BindEnv(allocationBatchWaitTime))
runtime.Must(viper.BindEnv(podNamespace))
runtime.Must(viper.BindEnv(leaderElectionFlag))
runtime.Must(viper.BindPFlags(pflag.CommandLine))
runtime.Must(cloudproduct.BindEnv())
runtime.Must(runtime.FeaturesBindEnv())
runtime.Must(runtime.ParseFeaturesFromEnv())
requestCPU, err := resource.ParseQuantity(viper.GetString(sidecarCPURequestFlag))
if err != nil {
logger.WithError(err).Fatalf("could not parse %s", sidecarCPURequestFlag)
}
limitCPU, err := resource.ParseQuantity(viper.GetString(sidecarCPULimitFlag))
if err != nil {
logger.WithError(err).Fatalf("could not parse %s", sidecarCPULimitFlag)
}
requestMemory, err := resource.ParseQuantity(viper.GetString(sidecarMemoryRequestFlag))
if err != nil {
logger.WithError(err).Fatalf("could not parse %s", sidecarMemoryRequestFlag)
}
limitMemory, err := resource.ParseQuantity(viper.GetString(sidecarMemoryLimitFlag))
if err != nil {
logger.WithError(err).Fatalf("could not parse %s", sidecarMemoryLimitFlag)
}
portRanges, err := parsePortRanges(viper.GetString(additionalPortRangesFlag))
if err != nil {
logger.WithError(err).Fatalf("could not parse %s", additionalPortRangesFlag)
}
portRanges[agonesv1.DefaultPortRange] = portallocator.PortRange{
MinPort: int32(viper.GetInt64(minPortFlag)),
MaxPort: int32(viper.GetInt64(maxPortFlag)),
}
return config{
PortRanges: portRanges,
SidecarImage: viper.GetString(sidecarImageFlag),
SidecarCPURequest: requestCPU,
SidecarCPULimit: limitCPU,
SidecarMemoryRequest: requestMemory,
SidecarMemoryLimit: limitMemory,
SidecarRunAsUser: int(viper.GetInt32(sidecarRunAsUserFlag)),
SdkServiceAccount: viper.GetString(sdkServerAccountFlag),
AlwaysPullSidecar: viper.GetBool(pullSidecarFlag),
KeyFile: viper.GetString(keyFileFlag),
CertFile: viper.GetString(certFileFlag),
KubeConfig: viper.GetString(kubeconfigFlag),
PrometheusMetrics: viper.GetBool(enablePrometheusMetricsFlag),
Stackdriver: viper.GetBool(enableStackdriverMetricsFlag),
GCPProjectID: viper.GetString(projectIDFlag),
NumWorkers: int(viper.GetInt32(numWorkersFlag)),
APIServerSustainedQPS: int(viper.GetInt32(apiServerSustainedQPSFlag)),
APIServerBurstQPS: int(viper.GetInt32(apiServerBurstQPSFlag)),
LogDir: viper.GetString(logDirFlag),
LogLevel: viper.GetString(logLevelFlag),
LogSizeLimitMB: int(viper.GetInt32(logSizeLimitMBFlag)),
MaxGameServerCreationsPerBatch: int(viper.GetInt32(maxGameServerCreationsPerBatchFlag)),
MaxCreationParallelism: int(viper.GetInt32(maxCreationParallelismFlag)),
MaxGameServerDeletionsPerBatch: int(viper.GetInt32(maxGameServerDeletionsPerBatchFlag)),
MaxDeletionParallelism: int(viper.GetInt32(maxDeletionParallelismFlag)),
MaxPodPendingCount: int(viper.GetInt32(maxPodPendingCountFlag)),
StackdriverLabels: viper.GetString(stackdriverLabels),
AllocationBatchWaitTime: viper.GetDuration(allocationBatchWaitTime),
PodNamespace: viper.GetString(podNamespace),
LeaderElection: viper.GetBool(leaderElectionFlag),
}
}
func parsePortRanges(s string) (map[string]portallocator.PortRange, error) {
if s == "" || !runtime.FeatureEnabled(runtime.FeaturePortRanges) {
return map[string]portallocator.PortRange{}, nil
}
prs := map[string][]int32{}
if err := json.Unmarshal([]byte(s), &prs); err != nil {
return nil, fmt.Errorf("invlaid additional port range format: %w", err)
}
portRanges := map[string]portallocator.PortRange{}
for k, v := range prs {
if len(v) != 2 {
return nil, fmt.Errorf("invalid port range ports for %s: requires both min and max port", k)
}
portRanges[k] = portallocator.PortRange{
MinPort: v[0],
MaxPort: v[1],
}
}
return portRanges, nil
}
// config stores all required configuration to create a game server controller.
type config struct {
PortRanges map[string]portallocator.PortRange
SidecarImage string
SidecarCPURequest resource.Quantity
SidecarCPULimit resource.Quantity
SidecarMemoryRequest resource.Quantity
SidecarMemoryLimit resource.Quantity
SidecarRunAsUser int
SdkServiceAccount string
AlwaysPullSidecar bool
PrometheusMetrics bool
Stackdriver bool
StackdriverLabels string
KeyFile string
CertFile string
KubeConfig string
GCPProjectID string
NumWorkers int
APIServerSustainedQPS int
APIServerBurstQPS int
LogDir string
LogLevel string
LogSizeLimitMB int
MaxGameServerCreationsPerBatch int
MaxCreationParallelism int
MaxGameServerDeletionsPerBatch int
MaxDeletionParallelism int
MaxPodPendingCount int
AllocationBatchWaitTime time.Duration
PodNamespace string
LeaderElection bool
}
// validate ensures the ctlConfig data is valid.
func (c *config) validate() []error {
validationErrors := make([]error, 0)
portErrors := validatePorts(c.PortRanges)
validationErrors = append(validationErrors, portErrors...)
resourceErrors := validateResource(c.SidecarCPURequest, c.SidecarCPULimit, corev1.ResourceCPU)
validationErrors = append(validationErrors, resourceErrors...)
resourceErrors = validateResource(c.SidecarMemoryRequest, c.SidecarMemoryLimit, corev1.ResourceMemory)
validationErrors = append(validationErrors, resourceErrors...)
return validationErrors
}
// validateResource validates limit or Memory CPU resources used for containers in pods
// If a GameServer is invalid there will be > 0 values in
// the returned array
//
// Moved from agones.dev/agones/pkg/apis/agones/v1 (#3255)
func validateResource(request resource.Quantity, limit resource.Quantity, resourceName corev1.ResourceName) []error {
validationErrors := make([]error, 0)
if !limit.IsZero() && request.Cmp(limit) > 0 {
validationErrors = append(validationErrors, errors.Errorf("Request must be less than or equal to %s limit", resourceName))
}
if request.Cmp(resource.Quantity{}) < 0 {
validationErrors = append(validationErrors, errors.Errorf("Resource %s request value must be non negative", resourceName))
}
if limit.Cmp(resource.Quantity{}) < 0 {
validationErrors = append(validationErrors, errors.Errorf("Resource %s limit value must be non negative", resourceName))
}
return validationErrors
}
func validatePorts(portRanges map[string]portallocator.PortRange) []error {
validationErrors := make([]error, 0)
for k, r := range portRanges {
portErrors := validatePortRange(r.MinPort, r.MaxPort, k)
validationErrors = append(validationErrors, portErrors...)
}
if len(validationErrors) > 0 {
return validationErrors
}
keys := make([]string, 0, len(portRanges))
values := make([]portallocator.PortRange, 0, len(portRanges))
for k, v := range portRanges {
keys = append(keys, k)
values = append(values, v)
}
for i, pr := range values {
for j := i + 1; j < len(values); j++ {
if overlaps(values[j].MinPort, values[j].MaxPort, pr.MinPort, pr.MaxPort) {
switch {
case keys[j] == agonesv1.DefaultPortRange:
validationErrors = append(validationErrors, errors.Errorf("port range %s overlaps with min/max port", keys[i]))
case keys[i] == agonesv1.DefaultPortRange:
validationErrors = append(validationErrors, errors.Errorf("port range %s overlaps with min/max port", keys[j]))
default:
validationErrors = append(validationErrors, errors.Errorf("port range %s overlaps with min/max port of range %s", keys[i], keys[j]))
}
}
}
}
return validationErrors
}
func validatePortRange(minPort, maxPort int32, rangeName string) []error {
validationErrors := make([]error, 0)
var rangeCtx string
if rangeName != agonesv1.DefaultPortRange {
rangeCtx = " for port range " + rangeName
}
if minPort <= 0 || maxPort <= 0 {
validationErrors = append(validationErrors, errors.New("min Port and Max Port values are required"+rangeCtx))
}
if maxPort < minPort {
validationErrors = append(validationErrors, errors.New("max Port cannot be set less that the Min Port"+rangeCtx))
}
return validationErrors
}
func overlaps(minA, maxA, minB, maxB int32) bool {
return max(minA, minB) < min(maxA, maxB)
}
type runner interface {
Run(ctx context.Context, workers int) error
}
func whenLeader(ctx context.Context, cancel context.CancelFunc, logger *logrus.Entry, doLeaderElection bool, kubeClient *kubernetes.Clientset, namespace string, start func(_ context.Context)) {
if !doLeaderElection {
start(ctx)
return
}
id := uuid.New().String()
lock := &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: "agones-controller-lock",
Namespace: namespace,
},
Client: kubeClient.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: id,
},
}
logger.WithField("id", id).Info("Leader Election ID")
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
// IMPORTANT: you MUST ensure that any code you have that
// is protected by the lease must terminate **before**
// you call cancel. Otherwise, you could have a background
// loop still running and another process could
// get elected before your background loop finished, violating
// the stated goal of the lease.
ReleaseOnCancel: true,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: start,
OnStoppedLeading: func() {
logger.WithField("id", id).Info("Leader Lost")
cancel()
os.Exit(0)
},
OnNewLeader: func(identity string) {
if identity == id {
return
}
logger.WithField("id", id).Info("New Leader Elected")
},
},
})
}