/
gameserver_state_cache.go
106 lines (86 loc) · 3.14 KB
/
gameserver_state_cache.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
package gameserversets
import (
"sync"
"agones.dev/agones/pkg/apis/stable/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// gameServerSetCacheEntry manages a list of items created and deleted locally for a single game server set.
type gameServerSetCacheEntry struct {
mu sync.Mutex
pendingCreation map[string]*v1alpha1.GameServer
pendingDeletion map[string]*v1alpha1.GameServer
}
func (e *gameServerSetCacheEntry) created(gs *v1alpha1.GameServer) {
e.mu.Lock()
defer e.mu.Unlock()
if e.pendingCreation == nil {
e.pendingCreation = map[string]*v1alpha1.GameServer{}
}
e.pendingCreation[gs.Name] = gs.DeepCopy()
}
func (e *gameServerSetCacheEntry) deleted(gs *v1alpha1.GameServer) {
e.mu.Lock()
defer e.mu.Unlock()
if e.pendingDeletion == nil {
e.pendingDeletion = map[string]*v1alpha1.GameServer{}
}
// Was pending creation, but deleted already.
if _, ok := e.pendingCreation[gs.Name]; ok {
delete(e.pendingCreation, gs.Name)
}
gsClone := gs.DeepCopy()
t := metav1.Now()
gsClone.ObjectMeta.DeletionTimestamp = &t
e.pendingDeletion[gs.Name] = gsClone
}
// reconcileWithUpdatedServerList returns a list of game servers for a game server set taking into account
// the complete list of game servers passed as parameter and a list of pending creations and deletions.
func (e *gameServerSetCacheEntry) reconcileWithUpdatedServerList(list []*v1alpha1.GameServer) []*v1alpha1.GameServer {
e.mu.Lock()
defer e.mu.Unlock()
var result []*v1alpha1.GameServer
found := map[string]bool{}
for _, gs := range list {
if d := e.pendingDeletion[gs.Name]; d != nil {
if !gs.ObjectMeta.DeletionTimestamp.IsZero() {
// has deletion timestamp - return theirs
result = append(result, d)
delete(e.pendingDeletion, gs.Name)
} else {
result = append(result, d)
}
} else {
// object not deleted locally, trust the list.
result = append(result, gs)
}
if e.pendingCreation[gs.Name] != nil {
// object was pending creation and now showed up in list results, remove local overlay.
delete(e.pendingCreation, gs.Name)
}
found[gs.Name] = true
}
// now delete from 'pendingDeletion' all the items that were not found in the result.
for gsName := range e.pendingDeletion {
if !found[gsName] {
// ("GSSC: %v is now fully deleted", gsName)
delete(e.pendingDeletion, gsName)
}
}
// add all game servers that are pending creation which were not in the list
for _, gs := range e.pendingCreation {
result = append(result, gs)
}
return result
}
// gameServerStateCache manages per-GSS cache of items created and deleted by this controller process
// to compensate for latency due to eventual consistency between client actions and K8s API server.
type gameServerStateCache struct {
cache sync.Map
}
func (c *gameServerStateCache) forGameServerSet(gsSet *v1alpha1.GameServerSet) *gameServerSetCacheEntry {
v, _ := c.cache.LoadOrStore(gsSet.ObjectMeta.Namespace+"/"+gsSet.ObjectMeta.Name, &gameServerSetCacheEntry{})
return v.(*gameServerSetCacheEntry)
}
func (c *gameServerStateCache) deleteGameServerSet(gsSet *v1alpha1.GameServerSet) {
c.cache.Delete(gsSet.ObjectMeta.Namespace + "/" + gsSet.ObjectMeta.Name)
}