Skip to content

Commit cf20a01

Browse files
pooneh-mmarkmandel
authored andcommitted
Changed the LabelSelector reference of gRPC api from apimachinary to self defined proto message (agones-dev#1236)
1 parent 85d836d commit cf20a01

6 files changed

Lines changed: 172 additions & 142 deletions

File tree

‎pkg/allocation/converters/converter.go‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func ConvertAllocationRequestV1Alpha1ToGSAV1(in *pb.AllocationRequest) *allocati
3434
Namespace: in.GetNamespace(),
3535
},
3636
Spec: allocationv1.GameServerAllocationSpec{
37-
Preferred: convertLabelSelectorsFromReference(in.GetPreferredGameServerSelectors()),
37+
Preferred: convertLabelSelectorsToInternalLabelSelectors(in.GetPreferredGameServerSelectors()),
3838
Scheduling: convertAllocationSchedulingV1Alpha1ToSchedulingStrategyV1(in.GetScheduling()),
3939
},
4040
}
@@ -43,8 +43,8 @@ func ConvertAllocationRequestV1Alpha1ToGSAV1(in *pb.AllocationRequest) *allocati
4343
gsa.Spec.MultiClusterSetting = allocationv1.MultiClusterSetting{
4444
Enabled: in.GetMultiClusterSetting().GetEnabled(),
4545
}
46-
if in.GetMultiClusterSetting().GetPolicySelector() != nil {
47-
gsa.Spec.MultiClusterSetting.PolicySelector = *in.GetMultiClusterSetting().GetPolicySelector()
46+
if ls := convertLabelSelectorToInternalLabelSelector(in.GetMultiClusterSetting().GetPolicySelector()); ls != nil {
47+
gsa.Spec.MultiClusterSetting.PolicySelector = *ls
4848
}
4949
}
5050

@@ -55,8 +55,8 @@ func ConvertAllocationRequestV1Alpha1ToGSAV1(in *pb.AllocationRequest) *allocati
5555
}
5656
}
5757

58-
if in.GetRequiredGameServerSelector() != nil {
59-
gsa.Spec.Required = *in.RequiredGameServerSelector
58+
if ls := convertLabelSelectorToInternalLabelSelector(in.GetRequiredGameServerSelector()); ls != nil {
59+
gsa.Spec.Required = *ls
6060
}
6161
return gsa
6262
}
@@ -69,20 +69,20 @@ func ConvertGSAV1ToAllocationRequestV1Alpha1(in *allocationv1.GameServerAllocati
6969

7070
out := &pb.AllocationRequest{
7171
Namespace: in.GetNamespace(),
72-
PreferredGameServerSelectors: convertLabelSelectorsToReference(in.Spec.Preferred),
72+
PreferredGameServerSelectors: convertInternalLabelSelectorsToLabelSelectors(in.Spec.Preferred),
7373
Scheduling: convertSchedulingStrategyV1ToAllocationSchedulingV1Alpha1(in.Spec.Scheduling),
7474
MultiClusterSetting: &pb.MultiClusterSetting{
7575
Enabled: in.Spec.MultiClusterSetting.Enabled,
7676
},
77-
RequiredGameServerSelector: &in.Spec.Required,
77+
RequiredGameServerSelector: convertInternalLabelSelectorToLabelSelector(&in.Spec.Required),
7878
MetaPatch: &pb.MetaPatch{
7979
Labels: in.Spec.MetaPatch.Labels,
8080
Annotations: in.Spec.MetaPatch.Annotations,
8181
},
8282
}
8383

8484
if in.Spec.MultiClusterSetting.Enabled {
85-
out.MultiClusterSetting.PolicySelector = &in.Spec.MultiClusterSetting.PolicySelector
85+
out.MultiClusterSetting.PolicySelector = convertInternalLabelSelectorToLabelSelector(&in.Spec.MultiClusterSetting.PolicySelector)
8686
}
8787

8888
return out
@@ -110,21 +110,35 @@ func convertSchedulingStrategyV1ToAllocationSchedulingV1Alpha1(in apis.Schedulin
110110
return pb.AllocationRequest_Packed
111111
}
112112

113-
// convertLabelSelectorsFromReference converts []* to [] for LabelSelector
114-
func convertLabelSelectorsFromReference(in []*metav1.LabelSelector) []metav1.LabelSelector {
115-
var result []metav1.LabelSelector
113+
func convertLabelSelectorToInternalLabelSelector(in *pb.LabelSelector) *metav1.LabelSelector {
114+
if in == nil {
115+
return nil
116+
}
117+
return &metav1.LabelSelector{MatchLabels: in.GetMatchLabels()}
118+
}
119+
120+
func convertInternalLabelSelectorToLabelSelector(in *metav1.LabelSelector) *pb.LabelSelector {
121+
if in == nil {
122+
return nil
123+
}
124+
return &pb.LabelSelector{MatchLabels: in.MatchLabels}
125+
}
126+
127+
func convertInternalLabelSelectorsToLabelSelectors(in []metav1.LabelSelector) []*pb.LabelSelector {
128+
var result []*pb.LabelSelector
116129
for _, l := range in {
117-
result = append(result, *l)
130+
c := convertInternalLabelSelectorToLabelSelector(&l)
131+
result = append(result, c)
118132
}
119133
return result
120134
}
121135

122-
// convertLabelSelectorsToReference converts [] to []* for LabelSelector
123-
func convertLabelSelectorsToReference(in []metav1.LabelSelector) []*metav1.LabelSelector {
124-
var result []*metav1.LabelSelector
136+
func convertLabelSelectorsToInternalLabelSelectors(in []*pb.LabelSelector) []metav1.LabelSelector {
137+
var result []metav1.LabelSelector
125138
for _, l := range in {
126-
l := l
127-
result = append(result, &l)
139+
if c := convertLabelSelectorToInternalLabelSelector(l); c != nil {
140+
result = append(result, *c)
141+
}
128142
}
129143
return result
130144
}

‎pkg/allocation/converters/converter_test.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
3838
Namespace: "ns",
3939
MultiClusterSetting: &pb.MultiClusterSetting{
4040
Enabled: true,
41-
PolicySelector: &metav1.LabelSelector{
41+
PolicySelector: &pb.LabelSelector{
4242
MatchLabels: map[string]string{
4343
"a": "b",
4444
},
4545
},
4646
},
47-
RequiredGameServerSelector: &metav1.LabelSelector{
47+
RequiredGameServerSelector: &pb.LabelSelector{
4848
MatchLabels: map[string]string{
4949
"c": "d",
5050
},
5151
},
52-
PreferredGameServerSelectors: []*metav1.LabelSelector{
52+
PreferredGameServerSelectors: []*pb.LabelSelector{
5353
{
5454
MatchLabels: map[string]string{
5555
"e": "f",
@@ -112,8 +112,8 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
112112
in: &pb.AllocationRequest{
113113
Namespace: "",
114114
MultiClusterSetting: &pb.MultiClusterSetting{},
115-
RequiredGameServerSelector: &metav1.LabelSelector{},
116-
PreferredGameServerSelectors: []*metav1.LabelSelector{},
115+
RequiredGameServerSelector: &pb.LabelSelector{},
116+
PreferredGameServerSelectors: []*pb.LabelSelector{},
117117
Scheduling: pb.AllocationRequest_Distributed,
118118
MetaPatch: &pb.MetaPatch{},
119119
},
@@ -188,7 +188,7 @@ func TestConvertGSAV1ToAllocationRequestV1Alpha1Empty(t *testing.T) {
188188
want: &pb.AllocationRequest{
189189
Namespace: "",
190190
MultiClusterSetting: &pb.MultiClusterSetting{},
191-
RequiredGameServerSelector: &metav1.LabelSelector{},
191+
RequiredGameServerSelector: &pb.LabelSelector{},
192192
Scheduling: pb.AllocationRequest_Distributed,
193193
MetaPatch: &pb.MetaPatch{},
194194
},
@@ -201,7 +201,7 @@ func TestConvertGSAV1ToAllocationRequestV1Alpha1Empty(t *testing.T) {
201201
},
202202
want: &pb.AllocationRequest{
203203
MultiClusterSetting: &pb.MultiClusterSetting{},
204-
RequiredGameServerSelector: &metav1.LabelSelector{},
204+
RequiredGameServerSelector: &pb.LabelSelector{},
205205
MetaPatch: &pb.MetaPatch{},
206206
},
207207
},

0 commit comments

Comments
 (0)