This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
546 lines (489 loc) · 18.3 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
package main
import (
"fmt"
"time"
"k8s.io/client-go/util/retry"
dgsv1alpha1 "github.com/dgkanatsios/azuregameserversscalingkubernetes/pkg/apis/azuregaming/v1alpha1"
dgsclientsetversioned "github.com/dgkanatsios/azuregameserversscalingkubernetes/pkg/client/clientset/versioned"
shared "github.com/dgkanatsios/azuregameserversscalingkubernetes/pkg/shared"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var client *kubernetes.Clientset
var dgsclient *dgsclientsetversioned.Clientset
const (
dgsColName string = "simplenodejsudp"
namespace string = "default"
loopTimes int = 20
delayInSecondsForLoopTest int = 5
delayInSeconds int = 1
)
func main() {
var err error
client, dgsclient, err = shared.GetClientSet()
if err != nil {
log.Errorf("Cannot initialize connection to cluster due to: %v", err)
}
// on this step, we do check if there are 5 healthy DGSs
log.Info("Step 1")
initialValidation() // check if there are 5 DGS
// we incrase the replicas to 10, so we should have 10 DGS and 10 Pods
// we also set ActivePlayers for all DGS to 2
log.Info("Step 2a")
// scale out
increaseReplicasToTen() // kubectl scale dgsc ... --replicas=10
validateClusterState(clusterState{totalPodCount: 10, healthyDGSCount: 10}) // check if there are 10 DGS and 10 pods
setAllActivePlayers(2) // set all ActivePlayers to two
// decrease from 10 to 7
// so our DGSCol should now contain 7 DGS, whereas 3 DGS should be out of the collection in MarkedForDeletion state
log.Info("Step 2b")
decreaseReplicasToSeven() // kubectl scale dgsc ... --replicas=7
validateClusterState(clusterState{totalPodCount: 10, healthyDGSCount: 7, markedForDeletionDGSCount: 3})
// make players leave from the MarkedForDeletionServers
// these DGSs should be deleted
log.Info("Step 3")
// make players leave
setActivePlayersOfMarkedForDeletionToZero() // set all ActivePlayers to zero
validateClusterState(clusterState{totalPodCount: 7, healthyDGSCount: 7, markedForDeletionDGSCount: 0})
// introduce some chaos doing random pod/DGS deletes
log.Info("Step 4a")
// some random deletes
deleteRandomDGS() // delete a random DGS
deleteRandomPod() // delete a random DGS-owned pod
deleteRandomDGS() // delete a random DGS
validateClusterState(clusterState{totalPodCount: 7, healthyDGSCount: 7})
// more chaos
log.Info("Step 4b")
deleteRandomPod() // delete a random DGS-owned pod
deleteRandomDGS() // delete a random DGS
deleteRandomPod() // delete a random DGS-owned pod
validateClusterState(clusterState{totalPodCount: 7, healthyDGSCount: 7})
// let's test some DGS failure - recall that DGSCol.FailBehavior is set to Remove. DGSMaxFailures is 2
log.Info("Step 5a")
setRandomHealthyDGSToFailed() // set a random's DGS GameState to Failed, this DGS should be removed from the collection
// healthyDGSCount should be 7 because another one will be created in the Failed one's place
validateClusterState(clusterState{
totalPodCount: 8,
healthyDGSCount: 7,
failedDGSInCollectionCount: 0,
failedDGSNotInCollectionCount: 1,
})
validateDGSTimesFailed(1) // check that DGS times failed is 1
// let's fail another one
// we're about to surpass the threshold of 2
log.Info("Step 5b")
setRandomHealthyDGSToFailed() // set a random's DGS GameState to Failed, this DGS should be removed from the collection
validateClusterState(clusterState{
totalPodCount: 9,
healthyDGSCount: 7,
failedDGSInCollectionCount: 0,
failedDGSNotInCollectionCount: 2,
})
// healthyDGSCount is 7 because another one will be created
validateDGSTimesFailed(2) // check that DGS times failed is 2
// another failure
// as we will surpass the MaxFailures threshold, this one failed DGS will not be removed from the collection
// and the DGSCOlState is NeedsIntervention
// no change in the number of Pods
log.Info("Step 5c")
setRandomHealthyDGSToFailed()
validateClusterState(clusterState{
totalPodCount: 9,
healthyDGSCount: 6,
failedDGSInCollectionCount: 1,
failedDGSNotInCollectionCount: 2,
dgsColHealth: dgsv1alpha1.DGSColNeedsIntervention,
})
validateDGSTimesFailed(2)
// another one
// no change, since we are in the NeedsIntervention state
// only one more DGS failed
log.Info("Step 5d")
setRandomHealthyDGSToFailed()
validateClusterState(clusterState{
totalPodCount: 9,
healthyDGSCount: 5,
failedDGSInCollectionCount: 2,
failedDGSNotInCollectionCount: 2,
dgsColHealth: dgsv1alpha1.DGSColNeedsIntervention,
})
validateDGSTimesFailed(2)
// reset the cluster, delete all DGS, set replicas to 5
// reset DGSTimesFailed to zero
// set the DGSFailBehavior to Delete
log.Info("Step 6a")
resetClusterState(5, dgsv1alpha1.Delete)
validateClusterState(clusterState{
totalPodCount: 5,
healthyDGSCount: 5,
failedDGSInCollectionCount: 0,
failedDGSNotInCollectionCount: 0,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
validateDGSTimesFailed(0)
// a random failure
// DGSTimesFailed will become 1
log.Info("Step 6b")
setRandomHealthyDGSToFailed()
validateClusterState(clusterState{
totalPodCount: 5,
healthyDGSCount: 5,
failedDGSInCollectionCount: 0,
failedDGSNotInCollectionCount: 0,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
validateDGSTimesFailed(1)
// another random failure
// DGSTimesFailed will become 2
log.Info("Step 6c")
setRandomHealthyDGSToFailed()
validateClusterState(clusterState{
totalPodCount: 5,
healthyDGSCount: 5,
failedDGSInCollectionCount: 0,
failedDGSNotInCollectionCount: 0,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
validateDGSTimesFailed(2)
// another random failure
// we surpassed the threshold
// DGSTimesFailed will stay 2
// state will become NeedsIntervention
log.Info("Step 6d")
setRandomHealthyDGSToFailed()
validateClusterState(clusterState{
totalPodCount: 5,
healthyDGSCount: 4,
failedDGSInCollectionCount: 1,
failedDGSNotInCollectionCount: 0,
dgsColHealth: dgsv1alpha1.DGSColNeedsIntervention,
})
validateDGSTimesFailed(2)
// another random failure
// state is NeedsIntervention
// DGSTimesFailed will stay 2
log.Info("Step 6e")
setRandomHealthyDGSToFailed()
validateClusterState(clusterState{
totalPodCount: 5,
healthyDGSCount: 3,
failedDGSInCollectionCount: 2,
failedDGSNotInCollectionCount: 0,
dgsColHealth: dgsv1alpha1.DGSColNeedsIntervention,
})
validateDGSTimesFailed(2)
// reset the cluster, delete all DGS, set replicas to 5
// reset DGSTimesFailed to zero
// set the DGSFailBehavior to Delete
// add DGS autoscaler - minimum Replicas 5, maximum 7
log.Info("Step 7a")
resetClusterState(5, dgsv1alpha1.Remove)
// HACK - on resetClusterState, DGSs are delete while the DGSCol is in NeedsIntervention - so no more DGS will be created
// so we delete a randomDGS after it in order to trigger the controller
deleteRandomDGS()
validateClusterState(clusterState{
totalPodCount: 5,
healthyDGSCount: 5,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
addAutoScalerDetails()
setAllActivePlayers(9)
// verify that autoscaler has kicked in and we have one more DGS
validateClusterState(clusterState{
totalPodCount: 6,
healthyDGSCount: 6,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
// set again 9 players for all DGS - 1 new DGS will be created
log.Info("Step 7b")
setAutoscalerLastScaleOperationDateTimeToZeroValue()
setAllActivePlayers(9)
// verify that autoscaler has kicked in and we have one more DGS
validateClusterState(clusterState{
totalPodCount: 7,
healthyDGSCount: 7,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
// set again 9 players for all DGS - no new DGS will be created since we are at the maximum of 7
log.Info("Step 7c")
setAutoscalerLastScaleOperationDateTimeToZeroValue()
setAllActivePlayers(9)
validateClusterState(clusterState{
totalPodCount: 7,
healthyDGSCount: 7,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
})
// set 5 players for all DGS -> 1 DGS less
log.Info("Step 7d")
setAutoscalerLastScaleOperationDateTimeToZeroValue()
setAllActivePlayers(5)
validateClusterState(clusterState{
totalPodCount: 7, // 7 pods: 6 in collection, 1 out
healthyDGSCount: 6,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
markedForDeletionDGSCount: 1,
})
// set again 5 players for all DGS -> 1 DGS less
log.Info("Step 7e")
setAutoscalerLastScaleOperationDateTimeToZeroValue()
setAllActivePlayers(5)
validateClusterState(clusterState{
totalPodCount: 7, // 7 pods: 5 in collection, 2 out
healthyDGSCount: 5,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
markedForDeletionDGSCount: 2,
})
// set 5 players for all DGS -> not going less than the minimum (5) replicas
log.Info("Step 7f")
setAutoscalerLastScaleOperationDateTimeToZeroValue()
setAllActivePlayers(5)
validateClusterState(clusterState{
totalPodCount: 7,
healthyDGSCount: 5,
dgsColHealth: dgsv1alpha1.DGSColHealthy,
markedForDeletionDGSCount: 2,
})
}
func handleError(err error) {
log.Panic(err)
}
func setAutoscalerLastScaleOperationDateTimeToZeroValue() {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgscol, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Get(dgsColName, metav1.GetOptions{})
if err != nil {
return err
}
dgscol.Spec.DGSActivePlayersAutoScalerDetails.LastScaleOperationDateTime = ""
_, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Update(dgscol)
if err != nil {
return err
}
return nil
})
if retryErr != nil {
handleError(retryErr)
}
}
func validateDGSTimesFailed(times int32) {
log.Infof(" Verifying that DGSTimesFailed is %d", times)
err := loopCheck(verifyDGSTimesFailed, times)
if err != nil {
handleError(err)
}
}
func addAutoScalerDetails() {
log.Info(" Adding DGS AutoScaler")
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgscol, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Get(dgsColName, metav1.GetOptions{})
if err != nil {
return err
}
dgscol.Spec.DGSActivePlayersAutoScalerDetails = &dgsv1alpha1.DGSActivePlayersAutoScalerDetails{
MinimumReplicas: 5,
MaximumReplicas: 7,
ScaleInThreshold: 60,
ScaleOutThreshold: 80,
Enabled: true,
CoolDownInMinutes: 5,
MaxPlayersPerServer: 10,
}
_, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Update(dgscol)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGSCol because of %s", retryErr.Error()))
}
}
func resetClusterState(replicas int32, failbehavior dgsv1alpha1.DedicatedGameServerFailBehavior) {
log.Info(" Resetting cluster state")
dgss, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).List(metav1.ListOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot get DGSs"))
}
//delete all of the failed ones
for _, dgs := range dgss.Items {
if dgs.Status.Health == dgsv1alpha1.DGSFailed {
err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Delete(dgs.Name, &metav1.DeleteOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot delete DGS because of %s", err))
}
}
}
log.Infof(" Waiting for %d second(s)", delayInSeconds)
time.Sleep(time.Duration(delayInSeconds) * time.Second)
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgscol, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Get(dgsColName, metav1.GetOptions{})
if err != nil {
return err
}
dgscol.Spec.DGSFailBehavior = failbehavior
dgscol.Spec.Replicas = replicas
dgscol.Status.DGSCollectionHealth = dgsv1alpha1.DGSColCreating
dgscol.Status.DGSTimesFailed = 0
_, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Update(dgscol)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGSCol because of %s", retryErr.Error()))
}
}
func setRandomHealthyDGSToFailed() {
log.Info(" Setting a random healthy DGS from the collection to Failed")
dgss, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).List(metav1.ListOptions{
LabelSelector: shared.LabelDedicatedGameServerCollectionName,
})
if err != nil {
handleError(fmt.Errorf("Cannot get DGSs"))
}
var dgsUpdate *dgsv1alpha1.DedicatedGameServer
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
var dgs *dgsv1alpha1.DedicatedGameServer
for {
dgs = &dgss.Items[getRandomInt(0, len(dgss.Items)-1)]
dgs, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Get(dgs.Name, metav1.GetOptions{})
if err != nil {
return err
}
if dgs.Status.Health == dgsv1alpha1.DGSHealthy && dgs.Status.PodPhase == corev1.PodRunning { // if it's Healthy
dgs.Status.Health = dgsv1alpha1.DGSFailed
break
}
}
dgsUpdate, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Update(dgs)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGS"))
}
if dgsUpdate.Status.Health != dgsv1alpha1.DGSFailed {
handleError(fmt.Errorf("DGS was not updated"))
}
}
func deleteRandomPod() {
log.Info(" Deleting a random pod")
pods, err := client.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot get pods"))
}
err = client.CoreV1().Pods(namespace).Delete(pods.Items[getRandomInt(0, len(pods.Items)-1)].Name, &metav1.DeleteOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot delete pod"))
}
}
func deleteRandomDGS() {
log.Info(" Deleting a random DGS from the collection")
dgss, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).List(metav1.ListOptions{
LabelSelector: shared.LabelDedicatedGameServerCollectionName,
})
if err != nil {
handleError(fmt.Errorf("Cannot get DGSs"))
}
//delete a random element
err = dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Delete(dgss.Items[getRandomInt(0, len(dgss.Items)-1)].Name, &metav1.DeleteOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot delete DGS"))
}
}
func setActivePlayersOfMarkedForDeletionToZero() {
log.Info(" Setting active players of MarkedForDeletion DGS to zero")
dgss, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).List(metav1.ListOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot get DGSs"))
}
for _, dgs := range dgss.Items {
if dgs.Status.Health == dgsv1alpha1.DGSHealthy &&
dgs.Status.MarkedForDeletion &&
dgs.Status.ActivePlayers == 2 {
dgsCopy := dgs.DeepCopy()
dgsCopy.Status.ActivePlayers = 0
var dgsUpdated *dgsv1alpha1.DedicatedGameServer
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgsUpdated, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Update(dgsCopy)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGS %s", dgsCopy.Name))
}
if dgsUpdated.Status.ActivePlayers != 0 {
handleError(fmt.Errorf("DGS %s was not updated", dgsCopy.Name))
}
}
}
}
func setAllActivePlayers(playerscount int) {
log.Infof(" Setting active players to %d for all DGS", playerscount)
dgss, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).List(metav1.ListOptions{})
if err != nil {
handleError(fmt.Errorf("Cannot get DGSs"))
}
for _, dgs := range dgss.Items {
var dgsUpdated *dgsv1alpha1.DedicatedGameServer
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgsCopy, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Get(dgs.Name, metav1.GetOptions{})
if err != nil {
return err
}
dgsCopy.Status.ActivePlayers = playerscount
dgsUpdated, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServers(namespace).Update(dgsCopy)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGS %s because of %s", dgs.Name, err.Error()))
}
if dgsUpdated.Status.ActivePlayers != playerscount {
handleError(fmt.Errorf("DGS %s was not updated with 2 Players, value is %d", dgs.Name, dgsUpdated.Status.ActivePlayers))
}
}
}
func increaseReplicasToTen() {
log.Info(" Increasing replicas of DGSCol to 10")
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgsCol, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Get(dgsColName, metav1.GetOptions{})
if err != nil {
return err
}
dgsCol.Spec.Replicas = 10
_, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Update(dgsCol)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGSCol"))
}
}
func decreaseReplicasToSeven() {
log.Info(" Decreasing replicas of DGSCol to 7")
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dgsCol, err := dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Get(dgsColName, metav1.GetOptions{})
if err != nil {
return err
}
dgsCol.Spec.Replicas = 7
_, err = dgsclient.AzuregamingV1alpha1().DedicatedGameServerCollections(namespace).Update(dgsCol)
return err
})
if retryErr != nil {
handleError(fmt.Errorf("Cannot update DGSCol"))
}
}
func initialValidation() {
count := int32(5)
log.Infof(" Waiting for %d second(s)", delayInSeconds)
time.Sleep(time.Duration(delayInSeconds) * time.Second)
log.Infof(" Verifying that %d pods are Running", count)
err := loopCheck(verifyPods, count)
if err != nil {
log.Error(err)
}
log.Infof(" Verifying that %d DedicatedGameServers are Healthy", count)
err = loopCheck(verifyHealthyDedicatedGameServers, count)
if err != nil {
log.Error(err)
}
log.Infof(" Verifying that DedicatedGameServerCollection with %d replicas is Pod/Running and DGS/Healthy", count)
err = loopCheckDGSCol(verifyDedicatedGameServerCollection, count, dgsv1alpha1.DGSColHealthy, corev1.PodRunning)
if err != nil {
log.Error(err)
}
}