forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (133 loc) · 5.63 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
// 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.
// sidecar for the game server that the sdk connects to
package main
import (
"fmt"
"net"
"strings"
"time"
"agones.dev/agones/pkg"
"agones.dev/agones/pkg/client/clientset/versioned"
"agones.dev/agones/pkg/gameservers"
"agones.dev/agones/pkg/sdk"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
port = 59357
// specifically env vars
gameServerNameEnv = "GAMESERVER_NAME"
podNamespaceEnv = "POD_NAMESPACE"
// Flags (that can also be env vars)
localFlag = "local"
addressFlag = "address"
healthDisabledFlag = "health-disabled"
healthTimeoutFlag = "health-timeout"
healthInitialDelayFlag = "health-initial-delay"
healthFailureThresholdFlag = "health-failure-threshold"
)
var (
logger = runtime.NewLoggerWithSource("main")
)
func main() {
viper.SetDefault(localFlag, false)
viper.SetDefault(addressFlag, "localhost")
viper.SetDefault(healthDisabledFlag, false)
viper.SetDefault(healthTimeoutFlag, 5)
viper.SetDefault(healthInitialDelayFlag, 5)
viper.SetDefault(healthFailureThresholdFlag, 3)
pflag.Bool(localFlag, viper.GetBool(localFlag),
"Set this, or LOCAL env, to 'true' to run this binary in local development mode. Defaults to 'false'")
pflag.String(addressFlag, viper.GetString(addressFlag), "The address to bind the server port to. Defaults to 'localhost")
pflag.Bool(healthDisabledFlag, viper.GetBool(healthDisabledFlag),
"Set this, or HEALTH_ENABLED env, to 'true' to enable health checking on the GameServer. Defaults to 'true'")
pflag.Int64(healthTimeoutFlag, viper.GetInt64(healthTimeoutFlag),
"Set this or HEALTH_TIMEOUT env to the number of seconds that the health check times out at. Defaults to 5")
pflag.Int64(healthInitialDelayFlag, viper.GetInt64(healthInitialDelayFlag),
"Set this or HEALTH_INITIAL_DELAY env to the number of seconds that the health will wait before starting. Defaults to 5")
pflag.Int64(healthFailureThresholdFlag, viper.GetInt64(healthFailureThresholdFlag),
"Set this or HEALTH_FAILURE_THRESHOLD env to the number of times the health check needs to fail to be deemed unhealthy. Defaults to 3")
pflag.Parse()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
runtime.Must(viper.BindEnv(localFlag))
runtime.Must(viper.BindEnv(gameServerNameEnv))
runtime.Must(viper.BindEnv(podNamespaceEnv))
runtime.Must(viper.BindEnv(healthDisabledFlag))
runtime.Must(viper.BindEnv(healthTimeoutFlag))
runtime.Must(viper.BindEnv(healthInitialDelayFlag))
runtime.Must(viper.BindEnv(healthFailureThresholdFlag))
runtime.Must(viper.BindPFlags(pflag.CommandLine))
isLocal := viper.GetBool(localFlag)
address := viper.GetString(addressFlag)
healthDisabled := viper.GetBool(healthDisabledFlag)
healthTimeout := time.Duration(viper.GetInt64(healthTimeoutFlag)) * time.Second
healthInitialDelay := time.Duration(viper.GetInt64(healthInitialDelayFlag)) * time.Second
healthFailureThreshold := viper.GetInt64(healthFailureThresholdFlag)
logger.WithField(localFlag, isLocal).WithField("version", pkg.Version).
WithField("port", port).WithField(addressFlag, address).
WithField(healthDisabledFlag, healthDisabled).WithField(healthTimeoutFlag, healthTimeout).
WithField(healthFailureThresholdFlag, healthFailureThreshold).
WithField(healthInitialDelayFlag, healthInitialDelay).Info("Starting sdk sidecar")
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
logger.WithField("port", port).WithField("address", address).Fatalf("Could not listen on port")
}
stop := signals.NewStopChannel()
grpcServer := grpc.NewServer()
if isLocal {
sdk.RegisterSDKServer(grpcServer, &gameservers.LocalSDKServer{})
} else {
config, err := rest.InClusterConfig()
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
logger.WithError(err).Fatal("Could not create the kubernetes clientset")
}
agonesClient, err := versioned.NewForConfig(config)
if err != nil {
logger.WithError(err).Fatalf("Could not create the agones api clientset")
}
var s *gameservers.SDKServer
s, err = gameservers.NewSDKServer(viper.GetString(gameServerNameEnv), viper.GetString(podNamespaceEnv),
healthDisabled, healthTimeout, healthFailureThreshold, healthInitialDelay, kubeClient, agonesClient)
if err != nil {
logger.WithError(err).Fatalf("Could not start sidecar")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go s.Run(ctx.Done())
sdk.RegisterSDKServer(grpcServer, s)
}
go func() {
err = grpcServer.Serve(lis)
if err != nil {
logger.WithError(err).Fatal("Could not serve grpc server")
}
}()
<-stop
logger.Info("shutting down grpc server")
// don't graceful stop, because if we get a kill signal
// then the gameserver is being shut down, and we no longer
// care about running RPC calls.
grpcServer.Stop()
}