-
Notifications
You must be signed in to change notification settings - Fork 829
/
Copy pathsdkserver.go
302 lines (265 loc) · 9.67 KB
/
sdkserver.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
// 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 (
"fmt"
"io"
"net/http"
"sync"
"time"
"agones.dev/agones/pkg/apis/stable"
stablev1alpha1 "agones.dev/agones/pkg/apis/stable/v1alpha1"
"agones.dev/agones/pkg/client/clientset/versioned"
typedv1alpha1 "agones.dev/agones/pkg/client/clientset/versioned/typed/stable/v1alpha1"
"agones.dev/agones/pkg/sdk"
"agones.dev/agones/pkg/util/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
)
var _ sdk.SDKServer = &SDKServer{}
// SDKServer is a gRPC server, that is meant to be a sidecar
// for a GameServer that will update the game server status on SDK requests
type SDKServer struct {
gameServerName string
namespace string
gameServerGetter typedv1alpha1.GameServersGetter
queue workqueue.RateLimitingInterface
server *http.Server
clock clock.Clock
healthDisabled bool
healthTimeout time.Duration
healthFailureThreshold int64
healthMutex sync.RWMutex
healthLastUpdated time.Time
healthFailureCount int64
recorder record.EventRecorder
}
// NewSDKServer creates a SDKServer that sets up an
// InClusterConfig for Kubernetes
func NewSDKServer(gameServerName, namespace string,
healthDisabled bool, healthTimeout time.Duration, healthFailureThreshold int64, healthInitialDelay time.Duration,
kubeClient kubernetes.Interface,
agonesClient versioned.Interface) (*SDKServer, error) {
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("ok"))
if err != nil {
logrus.WithError(err).Error("could not send ok response on healthz")
w.WriteHeader(http.StatusInternalServerError)
}
})
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logrus.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "gameserver-sidecar"})
s := &SDKServer{
gameServerName: gameServerName,
namespace: namespace,
gameServerGetter: agonesClient.StableV1alpha1(),
server: &http.Server{
Addr: ":8080",
Handler: mux,
},
clock: clock.RealClock{},
healthDisabled: healthDisabled,
healthFailureThreshold: healthFailureThreshold,
healthTimeout: healthTimeout,
healthMutex: sync.RWMutex{},
healthFailureCount: 0,
recorder: recorder,
}
mux.HandleFunc("/gshealthz", func(w http.ResponseWriter, r *http.Request) {
if s.healthy() {
_, err := w.Write([]byte("ok"))
if err != nil {
logrus.WithError(err).Error("could not send ok response on gshealthz")
w.WriteHeader(http.StatusInternalServerError)
}
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
s.initHealthLastUpdated(healthInitialDelay)
s.queue = s.newWorkQueue()
logrus.WithField("gameServerName", s.gameServerName).WithField("namespace", s.namespace).Info("created GameServer sidecar")
return s, nil
}
// initHealthLastUpdated adds the initial delay to now, then it will always be after `now`
// until the delay passes
func (s *SDKServer) initHealthLastUpdated(healthInitialDelay time.Duration) {
s.healthLastUpdated = s.clock.Now().UTC().Add(healthInitialDelay)
}
func (s *SDKServer) newWorkQueue() workqueue.RateLimitingInterface {
return workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(),
fmt.Sprintf("%s/%s/%s", stable.GroupName, s.namespace, s.gameServerName))
}
// Run processes the rate limited queue.
// Will block until stop is closed
func (s *SDKServer) Run(stop <-chan struct{}) {
defer s.queue.ShutDown()
logrus.Info("Starting SDKServer http health check...")
go func() {
if err := s.server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
logrus.WithError(err).Info("health check: http server closed")
} else {
err := errors.Wrap(err, "Could not listen on :8080")
runtime.HandleError(logrus.WithError(err), err)
}
}
}()
defer s.server.Close() // nolint: errcheck
if !s.healthDisabled {
logrus.Info("Starting GameServer health checking")
go wait.Until(s.runHealth, s.healthTimeout, stop)
}
logrus.Info("Starting worker")
go wait.Until(s.runWorker, time.Second, stop)
<-stop
logrus.Info("Shut down workers and health checking")
}
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the
// workqueue.
func (s *SDKServer) runWorker() {
for s.processNextWorkItem() {
}
}
func (s *SDKServer) processNextWorkItem() bool {
obj, quit := s.queue.Get()
if quit {
return false
}
defer s.queue.Done(obj)
logrus.WithField("obj", obj).Info("Processing obj")
var state stablev1alpha1.State
var ok bool
if state, ok = obj.(stablev1alpha1.State); !ok {
runtime.HandleError(logrus.WithField("obj", obj), errors.Errorf("expected State in queue, but got %T", obj))
// this is a bad entry, we don't want to reprocess
s.queue.Forget(obj)
return true
}
if err := s.updateState(state); err != nil {
// we don't forget here, because we want this to be retried via the queue
runtime.HandleError(logrus.WithField("obj", obj), err)
s.queue.AddRateLimited(obj)
return true
}
s.queue.Forget(obj)
return true
}
// updateState sets the GameServer Status's state to the state
// that has been passed through
func (s *SDKServer) updateState(state stablev1alpha1.State) error {
logrus.WithField("state", state).Info("Updating state")
gameServers := s.gameServerGetter.GameServers(s.namespace)
gs, err := gameServers.Get(s.gameServerName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "could not retrieve GameServer %s/%s", s.namespace, s.gameServerName)
}
// if the state is currently unhealthy, you can't go back to Ready
if gs.Status.State == stablev1alpha1.Unhealthy {
logrus.Info("State already unhealthy. Skipping update.")
return nil
}
gs.Status.State = state
_, err = gameServers.Update(gs)
// state specific work here
if gs.Status.State == stablev1alpha1.Unhealthy {
s.recorder.Event(gs, corev1.EventTypeWarning, string(gs.Status.State), "No longer healthy")
}
return errors.Wrapf(err, "could not update GameServer %s/%s to state %s", s.namespace, s.gameServerName, state)
}
// Ready enters the RequestReady state change for this GameServer into
// the workqueue so it can be updated
func (s *SDKServer) Ready(ctx context.Context, e *sdk.Empty) (*sdk.Empty, error) {
logrus.Info("Received Ready request, adding to queue")
s.queue.AddRateLimited(stablev1alpha1.RequestReady)
return e, nil
}
// Shutdown enters the Shutdown state change for this GameServer into
// the workqueue so it can be updated
func (s *SDKServer) Shutdown(ctx context.Context, e *sdk.Empty) (*sdk.Empty, error) {
logrus.Info("Received Shutdown request, adding to queue")
s.queue.AddRateLimited(stablev1alpha1.Shutdown)
return e, nil
}
// Health receives each health ping, and tracks the last time the health
// check was received, to track if a GameServer is healthy
func (s *SDKServer) Health(stream sdk.SDK_HealthServer) error {
for {
_, err := stream.Recv()
if err == io.EOF {
logrus.Info("Health stream closed.")
return stream.SendAndClose(&sdk.Empty{})
}
if err != nil {
return errors.Wrap(err, "Error with Health check")
}
logrus.Info("Health Ping Received")
s.touchHealthLastUpdated()
}
}
// runHealth actively checks the health, and if not
// healthy will push the Unhealthy state into the queue so
// it can be updated
func (s *SDKServer) runHealth() {
s.checkHealth()
if !s.healthy() {
logrus.WithField("gameServerName", s.gameServerName).Info("being marked as not healthy")
s.queue.AddRateLimited(stablev1alpha1.Unhealthy)
}
}
// touchHealthLastUpdated sets the healthLastUpdated
// value to now in UTC
func (s *SDKServer) touchHealthLastUpdated() {
s.healthMutex.Lock()
defer s.healthMutex.Unlock()
s.healthLastUpdated = s.clock.Now().UTC()
s.healthFailureCount = 0
}
// checkHealth checks the healthLastUpdated value
// and if it is outside the timeout value, log and
// count a failure
func (s *SDKServer) checkHealth() {
timeout := s.healthLastUpdated.Add(s.healthTimeout)
if timeout.Before(s.clock.Now().UTC()) {
s.healthMutex.Lock()
defer s.healthMutex.Unlock()
s.healthFailureCount++
logrus.WithField("failureCount", s.healthFailureCount).Infof("GameServer Health Check failed")
}
}
// healthy returns if the GameServer is
// currently healthy or not based on the configured
// failure count vs failure threshold
func (s *SDKServer) healthy() bool {
if s.healthDisabled {
return true
}
s.healthMutex.RLock()
defer s.healthMutex.RUnlock()
return s.healthFailureCount < s.healthFailureThreshold
}