Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename MetaPatch to Metadata for AllocationRequest #2070

Merged
merged 3 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions pkg/allocation/converters/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ func ConvertAllocationRequestToGSA(in *pb.AllocationRequest) *allocationv1.GameS
}
}

if in.GetMetaPatch() != nil {
// Accept both metadata (preferred) and metapatch until metapatch is fully removed.
metadata := in.GetMetadata()
if metadata == nil {
metadata = in.GetMetaPatch()
}

if metadata != nil {
gsa.Spec.MetaPatch = allocationv1.MetaPatch{
Labels: in.GetMetaPatch().GetLabels(),
Annotations: in.GetMetaPatch().GetAnnotations(),
Labels: metadata.GetLabels(),
Annotations: metadata.GetAnnotations(),
}
}

Expand All @@ -77,6 +83,13 @@ func ConvertGSAToAllocationRequest(in *allocationv1.GameServerAllocation) *pb.Al
Enabled: in.Spec.MultiClusterSetting.Enabled,
},
RequiredGameServerSelector: convertInternalLabelSelectorToLabelSelector(&in.Spec.Required),
Metadata: &pb.MetaPatch{
lambertwang marked this conversation as resolved.
Show resolved Hide resolved
Labels: in.Spec.MetaPatch.Labels,
Annotations: in.Spec.MetaPatch.Annotations,
},
// MetaPatch is deprecated, but we do a double write here to both metapatch and metadata
// to ensure that multi-cluster allocation still works when one cluster has the field
// and another one does not have the field yet.
MetaPatch: &pb.MetaPatch{
Labels: in.Spec.MetaPatch.Labels,
Annotations: in.Spec.MetaPatch.Annotations,
Expand Down
86 changes: 79 additions & 7 deletions pkg/allocation/converters/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
},
},
Scheduling: pb.AllocationRequest_Packed,
Metadata: &pb.MetaPatch{
Labels: map[string]string{
"i": "j",
},
},
MetaPatch: &pb.MetaPatch{
Labels: map[string]string{
"i": "j",
Expand Down Expand Up @@ -117,7 +122,7 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
RequiredGameServerSelector: &pb.LabelSelector{},
PreferredGameServerSelectors: []*pb.LabelSelector{},
Scheduling: pb.AllocationRequest_Distributed,
MetaPatch: &pb.MetaPatch{},
Metadata: &pb.MetaPatch{},
},
want: &allocationv1.GameServerAllocation{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -147,22 +152,87 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
in: nil,
want: nil,
},
{
name: "accepts deprecated metapatch field",
in: &pb.AllocationRequest{
Namespace: "",
MultiClusterSetting: &pb.MultiClusterSetting{},
RequiredGameServerSelector: &pb.LabelSelector{},
PreferredGameServerSelectors: []*pb.LabelSelector{},
Scheduling: pb.AllocationRequest_Distributed,
MetaPatch: &pb.MetaPatch{
Labels: map[string]string{
"a": "b",
},
},
},
want: &allocationv1.GameServerAllocation{
ObjectMeta: metav1.ObjectMeta{
Namespace: "",
},
Spec: allocationv1.GameServerAllocationSpec{
MultiClusterSetting: allocationv1.MultiClusterSetting{
Enabled: false,
},
Scheduling: apis.Distributed,
MetaPatch: allocationv1.MetaPatch{
Labels: map[string]string{
"a": "b",
},
},
},
},
skipConvertFromGSA: true,
},
{
name: "Prefers metadata over metapatch field",
in: &pb.AllocationRequest{
Namespace: "",
MultiClusterSetting: &pb.MultiClusterSetting{},
RequiredGameServerSelector: &pb.LabelSelector{},
PreferredGameServerSelectors: []*pb.LabelSelector{},
Scheduling: pb.AllocationRequest_Distributed,
Metadata: &pb.MetaPatch{
Labels: map[string]string{
"a": "b",
},
},
MetaPatch: &pb.MetaPatch{
Labels: map[string]string{
"c": "d",
},
},
},
want: &allocationv1.GameServerAllocation{
ObjectMeta: metav1.ObjectMeta{
Namespace: "",
},
Spec: allocationv1.GameServerAllocationSpec{
MultiClusterSetting: allocationv1.MultiClusterSetting{
Enabled: false,
},
Scheduling: apis.Distributed,
MetaPatch: allocationv1.MetaPatch{
Labels: map[string]string{
"a": "b",
},
},
},
},
skipConvertFromGSA: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

out := ConvertAllocationRequestToGSA(tc.in)
if !assert.Equal(t, tc.want, out) {
t.Errorf("mismatch with want after conversion: \"%s\"", tc.name)
}
assert.Equal(t, tc.want, out, "mismatch with want after conversion: \"%s\"", tc.name)

if !tc.skipConvertFromGSA {
gsa := ConvertGSAToAllocationRequest(tc.want)
if !assert.Equal(t, tc.in, gsa) {
t.Errorf("mismatch with input after double conversion \"%s\"", tc.name)
}
assert.Equal(t, tc.in, gsa, "mismatch with input after double conversion \"%s\"", tc.name)
}
})
}
Expand Down Expand Up @@ -192,6 +262,7 @@ func TestConvertGSAToAllocationRequestEmpty(t *testing.T) {
MultiClusterSetting: &pb.MultiClusterSetting{},
RequiredGameServerSelector: &pb.LabelSelector{},
Scheduling: pb.AllocationRequest_Distributed,
Metadata: &pb.MetaPatch{},
MetaPatch: &pb.MetaPatch{},
},
}, {
Expand All @@ -204,6 +275,7 @@ func TestConvertGSAToAllocationRequestEmpty(t *testing.T) {
want: &pb.AllocationRequest{
MultiClusterSetting: &pb.MultiClusterSetting{},
RequiredGameServerSelector: &pb.LabelSelector{},
Metadata: &pb.MetaPatch{},
MetaPatch: &pb.MetaPatch{},
},
},
Expand Down
119 changes: 65 additions & 54 deletions pkg/allocation/go/allocation.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pkg/allocation/go/allocation.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@
},
"metaPatch": {
"$ref": "#/definitions/allocationMetaPatch",
"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"
"title": "Deprecated: Please use metadata instead. This field is ignored if the\nmetadata field is set"
},
"metadata": {
"$ref": "#/definitions/allocationMetaPatch",
"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"
}
}
},
Expand Down
Loading