Skip to content

Commit 27d1004

Browse files
authored
Rename MetaPatch to Metadata for AllocationRequest (agones-dev#2070)
* Updated tests for allocation changes * Added double write for both metapatch and metadata
1 parent 3b50a84 commit 27d1004

7 files changed

Lines changed: 178 additions & 69 deletions

File tree

‎pkg/allocation/converters/converter.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ func ConvertAllocationRequestToGSA(in *pb.AllocationRequest) *allocationv1.GameS
5050
}
5151
}
5252

53-
if in.GetMetaPatch() != nil {
53+
// Accept both metadata (preferred) and metapatch until metapatch is fully removed.
54+
metadata := in.GetMetadata()
55+
if metadata == nil {
56+
metadata = in.GetMetaPatch()
57+
}
58+
59+
if metadata != nil {
5460
gsa.Spec.MetaPatch = allocationv1.MetaPatch{
55-
Labels: in.GetMetaPatch().GetLabels(),
56-
Annotations: in.GetMetaPatch().GetAnnotations(),
61+
Labels: metadata.GetLabels(),
62+
Annotations: metadata.GetAnnotations(),
5763
}
5864
}
5965

@@ -77,6 +83,13 @@ func ConvertGSAToAllocationRequest(in *allocationv1.GameServerAllocation) *pb.Al
7783
Enabled: in.Spec.MultiClusterSetting.Enabled,
7884
},
7985
RequiredGameServerSelector: convertInternalLabelSelectorToLabelSelector(&in.Spec.Required),
86+
Metadata: &pb.MetaPatch{
87+
Labels: in.Spec.MetaPatch.Labels,
88+
Annotations: in.Spec.MetaPatch.Annotations,
89+
},
90+
// MetaPatch is deprecated, but we do a double write here to both metapatch and metadata
91+
// to ensure that multi-cluster allocation still works when one cluster has the field
92+
// and another one does not have the field yet.
8093
MetaPatch: &pb.MetaPatch{
8194
Labels: in.Spec.MetaPatch.Labels,
8295
Annotations: in.Spec.MetaPatch.Annotations,

‎pkg/allocation/converters/converter_test.go‎

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
6464
},
6565
},
6666
Scheduling: pb.AllocationRequest_Packed,
67+
Metadata: &pb.MetaPatch{
68+
Labels: map[string]string{
69+
"i": "j",
70+
},
71+
},
6772
MetaPatch: &pb.MetaPatch{
6873
Labels: map[string]string{
6974
"i": "j",
@@ -117,7 +122,7 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
117122
RequiredGameServerSelector: &pb.LabelSelector{},
118123
PreferredGameServerSelectors: []*pb.LabelSelector{},
119124
Scheduling: pb.AllocationRequest_Distributed,
120-
MetaPatch: &pb.MetaPatch{},
125+
Metadata: &pb.MetaPatch{},
121126
},
122127
want: &allocationv1.GameServerAllocation{
123128
ObjectMeta: metav1.ObjectMeta{
@@ -147,22 +152,87 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
147152
in: nil,
148153
want: nil,
149154
},
155+
{
156+
name: "accepts deprecated metapatch field",
157+
in: &pb.AllocationRequest{
158+
Namespace: "",
159+
MultiClusterSetting: &pb.MultiClusterSetting{},
160+
RequiredGameServerSelector: &pb.LabelSelector{},
161+
PreferredGameServerSelectors: []*pb.LabelSelector{},
162+
Scheduling: pb.AllocationRequest_Distributed,
163+
MetaPatch: &pb.MetaPatch{
164+
Labels: map[string]string{
165+
"a": "b",
166+
},
167+
},
168+
},
169+
want: &allocationv1.GameServerAllocation{
170+
ObjectMeta: metav1.ObjectMeta{
171+
Namespace: "",
172+
},
173+
Spec: allocationv1.GameServerAllocationSpec{
174+
MultiClusterSetting: allocationv1.MultiClusterSetting{
175+
Enabled: false,
176+
},
177+
Scheduling: apis.Distributed,
178+
MetaPatch: allocationv1.MetaPatch{
179+
Labels: map[string]string{
180+
"a": "b",
181+
},
182+
},
183+
},
184+
},
185+
skipConvertFromGSA: true,
186+
},
187+
{
188+
name: "Prefers metadata over metapatch field",
189+
in: &pb.AllocationRequest{
190+
Namespace: "",
191+
MultiClusterSetting: &pb.MultiClusterSetting{},
192+
RequiredGameServerSelector: &pb.LabelSelector{},
193+
PreferredGameServerSelectors: []*pb.LabelSelector{},
194+
Scheduling: pb.AllocationRequest_Distributed,
195+
Metadata: &pb.MetaPatch{
196+
Labels: map[string]string{
197+
"a": "b",
198+
},
199+
},
200+
MetaPatch: &pb.MetaPatch{
201+
Labels: map[string]string{
202+
"c": "d",
203+
},
204+
},
205+
},
206+
want: &allocationv1.GameServerAllocation{
207+
ObjectMeta: metav1.ObjectMeta{
208+
Namespace: "",
209+
},
210+
Spec: allocationv1.GameServerAllocationSpec{
211+
MultiClusterSetting: allocationv1.MultiClusterSetting{
212+
Enabled: false,
213+
},
214+
Scheduling: apis.Distributed,
215+
MetaPatch: allocationv1.MetaPatch{
216+
Labels: map[string]string{
217+
"a": "b",
218+
},
219+
},
220+
},
221+
},
222+
skipConvertFromGSA: true,
223+
},
150224
}
151225
for _, tc := range tests {
152226
tc := tc
153227
t.Run(tc.name, func(t *testing.T) {
154228
t.Parallel()
155229

156230
out := ConvertAllocationRequestToGSA(tc.in)
157-
if !assert.Equal(t, tc.want, out) {
158-
t.Errorf("mismatch with want after conversion: \"%s\"", tc.name)
159-
}
231+
assert.Equal(t, tc.want, out, "mismatch with want after conversion: \"%s\"", tc.name)
160232

161233
if !tc.skipConvertFromGSA {
162234
gsa := ConvertGSAToAllocationRequest(tc.want)
163-
if !assert.Equal(t, tc.in, gsa) {
164-
t.Errorf("mismatch with input after double conversion \"%s\"", tc.name)
165-
}
235+
assert.Equal(t, tc.in, gsa, "mismatch with input after double conversion \"%s\"", tc.name)
166236
}
167237
})
168238
}
@@ -192,6 +262,7 @@ func TestConvertGSAToAllocationRequestEmpty(t *testing.T) {
192262
MultiClusterSetting: &pb.MultiClusterSetting{},
193263
RequiredGameServerSelector: &pb.LabelSelector{},
194264
Scheduling: pb.AllocationRequest_Distributed,
265+
Metadata: &pb.MetaPatch{},
195266
MetaPatch: &pb.MetaPatch{},
196267
},
197268
}, {
@@ -204,6 +275,7 @@ func TestConvertGSAToAllocationRequestEmpty(t *testing.T) {
204275
want: &pb.AllocationRequest{
205276
MultiClusterSetting: &pb.MultiClusterSetting{},
206277
RequiredGameServerSelector: &pb.LabelSelector{},
278+
Metadata: &pb.MetaPatch{},
207279
MetaPatch: &pb.MetaPatch{},
208280
},
209281
},

‎pkg/allocation/go/allocation.pb.go‎

Lines changed: 65 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/allocation/go/allocation.swagger.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@
9191
},
9292
"metaPatch": {
9393
"$ref": "#/definitions/allocationMetaPatch",
94-
"title": "MetaPatch is optional custom metadata that is added to the game server at\nallocation You can use this to tell the server necessary session data"
94+
"title": "Deprecated: Please use metadata instead. This field is ignored if the\nmetadata field is set"
95+
},
96+
"metadata": {
97+
"$ref": "#/definitions/allocationMetaPatch",
98+
"title": "Metadata is optional custom metadata that is added to the game server at\nallocation. You can use this to tell the server necessary session data"
9599
}
96100
}
97101
},

0 commit comments

Comments
 (0)