Skip to content

Commit 950bf87

Browse files
Victor ProdanVictor Prodan
authored andcommitted
Metadata propagation from fleet allocation to game server
In the fleet allocation configuration you can add optional custom metadata that will be added to the game server in the moment of allocation
1 parent d231543 commit 950bf87

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

examples/fleetallocation.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ metadata:
2828
spec:
2929
# The name of the fleet to allocate from. Must be an existing Fleet in the same namespace
3030
# as this FleetAllocation
31-
fleetName: fleet-example
31+
fleetName: fleet-example
32+
# Custom metadata that is added to game server status in the moment of allocation
33+
# You can use this to tell the server necessary session data
34+
metadata:
35+
labels:
36+
mode: deathmatch
37+
annotations:
38+
map: garden22

pkg/apis/stable/v1alpha1/fleetallocation.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ type FleetAllocationList struct {
4242
// FleetAllocationSpec is the spec for a Fleet
4343
// Allocation
4444
type FleetAllocationSpec struct {
45-
FleetName string `json:"fleetName"`
45+
FleetName string `json:"fleetName"`
46+
MetaPatch FleetAllocationMeta `json:"metadata,omitempty"`
47+
}
48+
49+
// FleetAllocationMeta is the metadata used to patch the GameServer metadata on allocation
50+
type FleetAllocationMeta struct {
51+
Labels map[string]string `json:"labels,omitempty"`
52+
Annotations map[string]string `json:"annotations,omitempty"`
4653
}
4754

4855
// FleetAllocationStatus will contain the

pkg/fleetallocation/controller.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (c *Controller) creationMutationHandler(review admv1beta1.AdmissionReview)
134134
return review, errors.Wrapf(err, "error retrieving fleet %s", fa.Name)
135135
}
136136

137-
gs, err := c.allocate(fleet)
137+
gs, err := c.allocate(fleet, &fa.Spec.MetaPatch)
138138
if err != nil {
139139
review.Response.Allowed = false
140140
review.Response.Result = &metav1.Status{
@@ -250,7 +250,7 @@ func (c *Controller) mutationValidationHandler(review admv1beta1.AdmissionReview
250250
}
251251

252252
// allocate allocated a GameServer from a given Fleet
253-
func (c *Controller) allocate(f *stablev1alpha1.Fleet) (*stablev1alpha1.GameServer, error) {
253+
func (c *Controller) allocate(f *stablev1alpha1.Fleet, fam *stablev1alpha1.FleetAllocationMeta) (*stablev1alpha1.GameServer, error) {
254254
var allocation *stablev1alpha1.GameServer
255255
// can only allocate one at a time, as we don't want two separate processes
256256
// trying to allocate the same GameServer to different clients
@@ -280,6 +280,10 @@ func (c *Controller) allocate(f *stablev1alpha1.Fleet) (*stablev1alpha1.GameServ
280280
gsCopy := allocation.DeepCopy()
281281
gsCopy.Status.State = stablev1alpha1.Allocated
282282

283+
if fam != nil {
284+
c.patchMetadata(gsCopy, fam)
285+
}
286+
283287
gs, err := c.gameServerGetter.GameServers(f.ObjectMeta.Namespace).Update(gsCopy)
284288
if err != nil {
285289
return gs, errors.Wrapf(err, "error updating GameServer %s", gsCopy.ObjectMeta.Name)
@@ -288,3 +292,25 @@ func (c *Controller) allocate(f *stablev1alpha1.Fleet) (*stablev1alpha1.GameServ
288292

289293
return gs, nil
290294
}
295+
296+
// patch the labels and annotations of an allocated GameServer with metadata from a FleetAllocation
297+
func (c *Controller) patchMetadata(gs *stablev1alpha1.GameServer, fam *stablev1alpha1.FleetAllocationMeta) {
298+
// patch ObjectMeta labels
299+
if fam.Labels != nil {
300+
if gs.ObjectMeta.Labels == nil {
301+
gs.ObjectMeta.Labels = make(map[string]string, len(fam.Labels))
302+
}
303+
for key, value := range fam.Labels {
304+
gs.ObjectMeta.Labels[key] = value
305+
}
306+
}
307+
// apply annotations patch
308+
if fam.Annotations != nil {
309+
if gs.ObjectMeta.Annotations == nil {
310+
gs.ObjectMeta.Annotations = make(map[string]string, len(fam.Annotations))
311+
}
312+
for key, value := range fam.Annotations {
313+
gs.ObjectMeta.Annotations[key] = value
314+
}
315+
}
316+
}

pkg/fleetallocation/controller_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ func TestControllerAllocate(t *testing.T) {
144144
f, gsSet, gsList := defaultFixtures(4)
145145
c, m := newFakeController()
146146
n := metav1.Now()
147+
l := map[string]string{"mode": "deathmatch"}
148+
a := map[string]string{"map": "searide"}
149+
fam := &v1alpha1.FleetAllocationMeta{Labels: l, Annotations: a}
150+
147151
gsList[3].ObjectMeta.DeletionTimestamp = &n
148152

149153
m.AgonesClient.AddReactor("list", "fleets", func(action k8stesting.Action) (bool, runtime.Object, error) {
@@ -172,25 +176,35 @@ func TestControllerAllocate(t *testing.T) {
172176
_, cancel := agtesting.StartInformers(m)
173177
defer cancel()
174178

175-
gs, err := c.allocate(f)
179+
gs, err := c.allocate(f, fam)
176180
assert.Nil(t, err)
177181
assert.Equal(t, v1alpha1.Allocated, gs.Status.State)
178182
assert.True(t, updated)
183+
for key, value := range fam.Labels {
184+
v, ok := gs.ObjectMeta.Labels[key]
185+
assert.True(t, ok)
186+
assert.Equal(t, v, value)
187+
}
188+
for key, value := range fam.Annotations {
189+
v, ok := gs.ObjectMeta.Annotations[key]
190+
assert.True(t, ok)
191+
assert.Equal(t, v, value)
192+
}
179193

180194
updated = false
181-
gs, err = c.allocate(f)
195+
gs, err = c.allocate(f, nil)
182196
assert.Nil(t, err)
183197
assert.Equal(t, v1alpha1.Allocated, gs.Status.State)
184198
assert.True(t, updated)
185199

186200
updated = false
187-
gs, err = c.allocate(f)
201+
gs, err = c.allocate(f, nil)
188202
assert.Nil(t, err)
189203
assert.Equal(t, v1alpha1.Allocated, gs.Status.State)
190204
assert.True(t, updated)
191205

192206
updated = false
193-
_, err = c.allocate(f)
207+
_, err = c.allocate(f, nil)
194208
assert.NotNil(t, err)
195209
assert.Equal(t, ErrNoGameServerReady, err)
196210
assert.False(t, updated)
@@ -230,7 +244,7 @@ func TestControllerAllocateMutex(t *testing.T) {
230244
allocate := func() {
231245
defer wg.Done()
232246
for i := 1; i <= 10; i++ {
233-
_, err := c.allocate(f)
247+
_, err := c.allocate(f, nil)
234248
assert.Nil(t, err)
235249
}
236250
}

0 commit comments

Comments
 (0)