From ad4e127b600546ad93db8a881e5aa19f91463942 Mon Sep 17 00:00:00 2001 From: pooneh-m <46979170+pooneh-m@users.noreply.github.com> Date: Wed, 6 May 2020 16:03:04 -0700 Subject: [PATCH] Change allocator gRPC response state to gRPC error status (#1516) Co-authored-by: Mark Mandel --- cmd/allocator/main.go | 6 +- cmd/allocator/main_test.go | 8 +- pkg/allocation/converters/converter.go | 42 ++--- pkg/allocation/converters/converter_test.go | 57 ++++-- pkg/allocation/go/v1alpha1/allocation.pb.go | 164 +++++++----------- .../go/v1alpha1/allocation.swagger.json | 15 -- pkg/gameserverallocations/allocator.go | 20 ++- pkg/gameserverallocations/controller_test.go | 8 +- proto/allocation/v1alpha1/allocation.proto | 13 -- test/e2e/allocator_test.go | 25 ++- 10 files changed, 158 insertions(+), 200 deletions(-) diff --git a/cmd/allocator/main.go b/cmd/allocator/main.go index ec9c969193..9fc6097a58 100644 --- a/cmd/allocator/main.go +++ b/cmd/allocator/main.go @@ -277,8 +277,8 @@ func (h *serviceHandler) Allocate(ctx context.Context, in *pb.AllocationRequest) logger.Errorf("internal server error - Bad GSA format %v", resultObj) return nil, status.Errorf(codes.Internal, "internal server error- Bad GSA format %v", resultObj) } - response := converters.ConvertGSAV1ToAllocationResponseV1Alpha1(allocatedGsa) - logger.WithField("response", response).Infof("allocation response is being sent") + response, err := converters.ConvertGSAV1ToAllocationResponseV1Alpha1(allocatedGsa) + logger.WithField("response", response).WithError(err).Infof("allocation response is being sent") - return response, nil + return response, err } diff --git a/cmd/allocator/main_test.go b/cmd/allocator/main_test.go index 18528bf1d1..14958771ea 100644 --- a/cmd/allocator/main_test.go +++ b/cmd/allocator/main_test.go @@ -55,10 +55,14 @@ func TestAllocateHandler(t *testing.T) { } response, err := h.Allocate(context.Background(), request) - if !assert.NoError(t, err) { + if !assert.Nil(t, response) { return } - assert.Equal(t, pb.AllocationResponse_Contention, response.State) + st, ok := status.FromError(err) + if !assert.True(t, ok) { + return + } + assert.Equal(t, st.Code(), codes.Aborted) } func TestAllocateHandlerReturnsError(t *testing.T) { diff --git a/pkg/allocation/converters/converter.go b/pkg/allocation/converters/converter.go index d5bfada32c..ae33d66f29 100644 --- a/pkg/allocation/converters/converter.go +++ b/pkg/allocation/converters/converter.go @@ -20,6 +20,8 @@ import ( "agones.dev/agones/pkg/apis" agonesv1 "agones.dev/agones/pkg/apis/agones/v1" allocationv1 "agones.dev/agones/pkg/apis/allocation/v1" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -145,18 +147,21 @@ func convertLabelSelectorsToInternalLabelSelectors(in []*pb.LabelSelector) []met } // ConvertGSAV1ToAllocationResponseV1Alpha1 converts GameServerAllocation V1 (GSA) to AllocationResponse -func ConvertGSAV1ToAllocationResponseV1Alpha1(in *allocationv1.GameServerAllocation) *pb.AllocationResponse { +func ConvertGSAV1ToAllocationResponseV1Alpha1(in *allocationv1.GameServerAllocation) (*pb.AllocationResponse, error) { if in == nil { - return nil + return nil, nil + } + + if err := convertStateV1ToError(in.Status.State); err != nil { + return nil, err } return &pb.AllocationResponse{ - State: convertStateV1ToAllocationStateV1Alpha1(in.Status.State), GameServerName: in.Status.GameServerName, Address: in.Status.Address, NodeName: in.Status.NodeName, Ports: convertAgonesPortsV1ToAllocationPortsV1Alpha1(in.Status.Ports), - } + }, nil } // ConvertAllocationResponseV1Alpha1ToGSAV1 converts AllocationResponse to GameServerAllocation V1 (GSA) @@ -167,15 +172,14 @@ func ConvertAllocationResponseV1Alpha1ToGSAV1(in *pb.AllocationResponse) *alloca out := &allocationv1.GameServerAllocation{ Status: allocationv1.GameServerAllocationStatus{ + State: allocationv1.GameServerAllocationAllocated, GameServerName: in.GameServerName, Address: in.Address, NodeName: in.NodeName, Ports: convertAllocationPortsV1Alpha1ToAgonesPortsV1(in.Ports), }, } - if state, isMatch := convertAllocationStateV1Alpha1ToStateV1(in.State); isMatch { - out.Status.State = state - } + return out } @@ -206,28 +210,14 @@ func convertAllocationPortsV1Alpha1ToAgonesPortsV1(in []*pb.AllocationResponse_G } // convertStateV1ToAllocationStateV1Alpha1 converts GameServerAllocationState V1 (GSA) to AllocationResponse_GameServerAllocationState -func convertStateV1ToAllocationStateV1Alpha1(in allocationv1.GameServerAllocationState) pb.AllocationResponse_GameServerAllocationState { +func convertStateV1ToError(in allocationv1.GameServerAllocationState) error { switch in { case allocationv1.GameServerAllocationAllocated: - return pb.AllocationResponse_Allocated + return nil case allocationv1.GameServerAllocationUnAllocated: - return pb.AllocationResponse_UnAllocated + return status.Error(codes.ResourceExhausted, "there is no available GameServer to allocate") case allocationv1.GameServerAllocationContention: - return pb.AllocationResponse_Contention + return status.Error(codes.Aborted, "too many concurrent requests have overwhelmed the system") } - return pb.AllocationResponse_Unknown -} - -// convertAllocationStateV1Alpha1ToStateV1 converts AllocationResponse_GameServerAllocationState to GameServerAllocationState V1 (GSA) -// if the conversion cannot happen, it returns false. -func convertAllocationStateV1Alpha1ToStateV1(in pb.AllocationResponse_GameServerAllocationState) (allocationv1.GameServerAllocationState, bool) { - switch in { - case pb.AllocationResponse_Allocated: - return allocationv1.GameServerAllocationAllocated, true - case pb.AllocationResponse_UnAllocated: - return allocationv1.GameServerAllocationUnAllocated, true - case pb.AllocationResponse_Contention: - return allocationv1.GameServerAllocationContention, true - } - return allocationv1.GameServerAllocationUnAllocated, false + return status.Error(codes.Unknown, "unknown issue") } diff --git a/pkg/allocation/converters/converter_test.go b/pkg/allocation/converters/converter_test.go index 6c4e59cb65..7eb602cdea 100644 --- a/pkg/allocation/converters/converter_test.go +++ b/pkg/allocation/converters/converter_test.go @@ -22,6 +22,8 @@ import ( agonesv1 "agones.dev/agones/pkg/apis/agones/v1" allocationv1 "agones.dev/agones/pkg/apis/allocation/v1" "github.com/stretchr/testify/assert" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -223,13 +225,14 @@ func TestConvertGSAV1ToAllocationResponseV1Alpha1(t *testing.T) { name string in *allocationv1.GameServerAllocation want *pb.AllocationResponse + wantErrCode codes.Code skipConvertToGSA bool }{ { - name: "status field is set", + name: "status state is set to allocated", in: &allocationv1.GameServerAllocation{ Status: allocationv1.GameServerAllocationStatus{ - State: allocationv1.GameServerAllocationUnAllocated, + State: allocationv1.GameServerAllocationAllocated, GameServerName: "GSN", Ports: []agonesv1.GameServerStatusPort{ { @@ -244,7 +247,6 @@ func TestConvertGSAV1ToAllocationResponseV1Alpha1(t *testing.T) { }, }, want: &pb.AllocationResponse{ - State: pb.AllocationResponse_UnAllocated, GameServerName: "GSN", Address: "address", NodeName: "node-name", @@ -259,15 +261,25 @@ func TestConvertGSAV1ToAllocationResponseV1Alpha1(t *testing.T) { }, }, { - name: "status state is set to allocated", + name: "status field is set to unallocated", in: &allocationv1.GameServerAllocation{ Status: allocationv1.GameServerAllocationStatus{ - State: allocationv1.GameServerAllocationAllocated, + State: allocationv1.GameServerAllocationUnAllocated, + GameServerName: "GSN", + Ports: []agonesv1.GameServerStatusPort{ + { + Port: 123, + }, + { + Name: "port-name", + }, + }, + Address: "address", + NodeName: "node-name", }, }, - want: &pb.AllocationResponse{ - State: pb.AllocationResponse_Allocated, - }, + wantErrCode: codes.ResourceExhausted, + skipConvertToGSA: true, }, { name: "status state is set to contention", @@ -276,9 +288,8 @@ func TestConvertGSAV1ToAllocationResponseV1Alpha1(t *testing.T) { State: allocationv1.GameServerAllocationContention, }, }, - want: &pb.AllocationResponse{ - State: pb.AllocationResponse_Contention, - }, + wantErrCode: codes.Aborted, + skipConvertToGSA: true, }, { name: "Empty fields", @@ -287,14 +298,16 @@ func TestConvertGSAV1ToAllocationResponseV1Alpha1(t *testing.T) { Ports: []agonesv1.GameServerStatusPort{}, }, }, - want: &pb.AllocationResponse{ - State: pb.AllocationResponse_Unknown, - }, + wantErrCode: codes.Unknown, skipConvertToGSA: true, }, { name: "Empty objects", - in: &allocationv1.GameServerAllocation{}, + in: &allocationv1.GameServerAllocation{ + Status: allocationv1.GameServerAllocationStatus{ + State: allocationv1.GameServerAllocationAllocated, + }, + }, want: &pb.AllocationResponse{}, }, { @@ -308,7 +321,14 @@ func TestConvertGSAV1ToAllocationResponseV1Alpha1(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - out := ConvertGSAV1ToAllocationResponseV1Alpha1(tc.in) + out, err := ConvertGSAV1ToAllocationResponseV1Alpha1(tc.in) + if tc.wantErrCode != 0 { + st, ok := status.FromError(err) + if !assert.True(t, ok) { + return + } + assert.Equal(t, tc.wantErrCode, st.Code()) + } if !assert.Equal(t, tc.want, out) { t.Errorf("mismatch with want after conversion: \"%s\"", tc.name) } @@ -332,11 +352,12 @@ func TestConvertAllocationResponseV1Alpha1ToGSAV1(t *testing.T) { { name: "Empty fields", in: &pb.AllocationResponse{ - State: pb.AllocationResponse_Unknown, Ports: []*pb.AllocationResponse_GameServerStatusPort{}, }, want: &allocationv1.GameServerAllocation{ - Status: allocationv1.GameServerAllocationStatus{}, + Status: allocationv1.GameServerAllocationStatus{ + State: allocationv1.GameServerAllocationAllocated, + }, }, }, } diff --git a/pkg/allocation/go/v1alpha1/allocation.pb.go b/pkg/allocation/go/v1alpha1/allocation.pb.go index 117a4e9b52..5261577066 100644 --- a/pkg/allocation/go/v1alpha1/allocation.pb.go +++ b/pkg/allocation/go/v1alpha1/allocation.pb.go @@ -63,40 +63,7 @@ func (x AllocationRequest_SchedulingStrategy) String() string { return proto.EnumName(AllocationRequest_SchedulingStrategy_name, int32(x)) } func (AllocationRequest_SchedulingStrategy) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{0, 0} -} - -// The allocation state -type AllocationResponse_GameServerAllocationState int32 - -const ( - AllocationResponse_Unknown AllocationResponse_GameServerAllocationState = 0 - // Allocated is for successful allocation - AllocationResponse_Allocated AllocationResponse_GameServerAllocationState = 1 - // UnAllocated is for unsuccessful allocation due to lack of gameserver resources - AllocationResponse_UnAllocated AllocationResponse_GameServerAllocationState = 2 - // Contention is for unsuccessful allocation due to contention - AllocationResponse_Contention AllocationResponse_GameServerAllocationState = 3 -) - -var AllocationResponse_GameServerAllocationState_name = map[int32]string{ - 0: "Unknown", - 1: "Allocated", - 2: "UnAllocated", - 3: "Contention", -} -var AllocationResponse_GameServerAllocationState_value = map[string]int32{ - "Unknown": 0, - "Allocated": 1, - "UnAllocated": 2, - "Contention": 3, -} - -func (x AllocationResponse_GameServerAllocationState) String() string { - return proto.EnumName(AllocationResponse_GameServerAllocationState_name, int32(x)) -} -func (AllocationResponse_GameServerAllocationState) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{1, 0} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{0, 0} } type AllocationRequest struct { @@ -123,7 +90,7 @@ func (m *AllocationRequest) Reset() { *m = AllocationRequest{} } func (m *AllocationRequest) String() string { return proto.CompactTextString(m) } func (*AllocationRequest) ProtoMessage() {} func (*AllocationRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{0} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{0} } func (m *AllocationRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AllocationRequest.Unmarshal(m, b) @@ -186,21 +153,20 @@ func (m *AllocationRequest) GetMetaPatch() *MetaPatch { } type AllocationResponse struct { - State AllocationResponse_GameServerAllocationState `protobuf:"varint,1,opt,name=state,proto3,enum=v1alpha1.AllocationResponse_GameServerAllocationState" json:"state,omitempty"` - GameServerName string `protobuf:"bytes,2,opt,name=gameServerName,proto3" json:"gameServerName,omitempty"` - Ports []*AllocationResponse_GameServerStatusPort `protobuf:"bytes,3,rep,name=ports,proto3" json:"ports,omitempty"` - Address string `protobuf:"bytes,4,opt,name=address,proto3" json:"address,omitempty"` - NodeName string `protobuf:"bytes,5,opt,name=nodeName,proto3" json:"nodeName,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + GameServerName string `protobuf:"bytes,2,opt,name=gameServerName,proto3" json:"gameServerName,omitempty"` + Ports []*AllocationResponse_GameServerStatusPort `protobuf:"bytes,3,rep,name=ports,proto3" json:"ports,omitempty"` + Address string `protobuf:"bytes,4,opt,name=address,proto3" json:"address,omitempty"` + NodeName string `protobuf:"bytes,5,opt,name=nodeName,proto3" json:"nodeName,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *AllocationResponse) Reset() { *m = AllocationResponse{} } func (m *AllocationResponse) String() string { return proto.CompactTextString(m) } func (*AllocationResponse) ProtoMessage() {} func (*AllocationResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{1} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{1} } func (m *AllocationResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AllocationResponse.Unmarshal(m, b) @@ -220,13 +186,6 @@ func (m *AllocationResponse) XXX_DiscardUnknown() { var xxx_messageInfo_AllocationResponse proto.InternalMessageInfo -func (m *AllocationResponse) GetState() AllocationResponse_GameServerAllocationState { - if m != nil { - return m.State - } - return AllocationResponse_Unknown -} - func (m *AllocationResponse) GetGameServerName() string { if m != nil { return m.GameServerName @@ -270,7 +229,7 @@ func (m *AllocationResponse_GameServerStatusPort) Reset() { func (m *AllocationResponse_GameServerStatusPort) String() string { return proto.CompactTextString(m) } func (*AllocationResponse_GameServerStatusPort) ProtoMessage() {} func (*AllocationResponse_GameServerStatusPort) Descriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{1, 0} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{1, 0} } func (m *AllocationResponse_GameServerStatusPort) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AllocationResponse_GameServerStatusPort.Unmarshal(m, b) @@ -319,7 +278,7 @@ func (m *MultiClusterSetting) Reset() { *m = MultiClusterSetting{} } func (m *MultiClusterSetting) String() string { return proto.CompactTextString(m) } func (*MultiClusterSetting) ProtoMessage() {} func (*MultiClusterSetting) Descriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{2} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{2} } func (m *MultiClusterSetting) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_MultiClusterSetting.Unmarshal(m, b) @@ -366,7 +325,7 @@ func (m *MetaPatch) Reset() { *m = MetaPatch{} } func (m *MetaPatch) String() string { return proto.CompactTextString(m) } func (*MetaPatch) ProtoMessage() {} func (*MetaPatch) Descriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{3} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{3} } func (m *MetaPatch) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_MetaPatch.Unmarshal(m, b) @@ -413,7 +372,7 @@ func (m *LabelSelector) Reset() { *m = LabelSelector{} } func (m *LabelSelector) String() string { return proto.CompactTextString(m) } func (*LabelSelector) ProtoMessage() {} func (*LabelSelector) Descriptor() ([]byte, []int) { - return fileDescriptor_allocation_0edfd2a4a0f9c2cb, []int{4} + return fileDescriptor_allocation_a8b682e2fff83ddc, []int{4} } func (m *LabelSelector) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_LabelSelector.Unmarshal(m, b) @@ -451,7 +410,6 @@ func init() { proto.RegisterType((*LabelSelector)(nil), "v1alpha1.LabelSelector") proto.RegisterMapType((map[string]string)(nil), "v1alpha1.LabelSelector.MatchLabelsEntry") proto.RegisterEnum("v1alpha1.AllocationRequest_SchedulingStrategy", AllocationRequest_SchedulingStrategy_name, AllocationRequest_SchedulingStrategy_value) - proto.RegisterEnum("v1alpha1.AllocationResponse_GameServerAllocationState", AllocationResponse_GameServerAllocationState_name, AllocationResponse_GameServerAllocationState_value) } // Reference imports to suppress errors if they are not otherwise used. @@ -527,53 +485,49 @@ var _AllocationService_serviceDesc = grpc.ServiceDesc{ } func init() { - proto.RegisterFile("proto/allocation/v1alpha1/allocation.proto", fileDescriptor_allocation_0edfd2a4a0f9c2cb) -} - -var fileDescriptor_allocation_0edfd2a4a0f9c2cb = []byte{ - // 693 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x55, 0x4d, 0x6f, 0xd3, 0x30, - 0x18, 0x5e, 0xda, 0xb5, 0x6b, 0xdf, 0x6a, 0xa5, 0x78, 0x93, 0x08, 0xa5, 0x40, 0x15, 0x10, 0x2a, - 0x3b, 0xb4, 0x6a, 0x91, 0xf8, 0xd8, 0x61, 0x68, 0x1a, 0x30, 0x09, 0x6d, 0x63, 0x4a, 0x35, 0x81, - 0xc4, 0xc9, 0x4d, 0x5e, 0xba, 0x68, 0xa9, 0x9d, 0xc5, 0xce, 0x50, 0xaf, 0x48, 0x88, 0x1f, 0xc0, - 0x91, 0x23, 0x3f, 0x89, 0xbf, 0xc0, 0x99, 0xdf, 0x80, 0xec, 0x34, 0x4d, 0xe8, 0xb2, 0x8a, 0xdd, - 0x6c, 0xbf, 0xcf, 0xf3, 0xf8, 0xfd, 0xb4, 0x61, 0x2b, 0x08, 0xb9, 0xe4, 0x3d, 0xea, 0xfb, 0xdc, - 0xa1, 0xd2, 0xe3, 0xac, 0x77, 0xd1, 0xa7, 0x7e, 0x70, 0x4a, 0xfb, 0x99, 0xb3, 0xae, 0x06, 0x91, - 0x4a, 0x62, 0x6a, 0xb6, 0xc6, 0x9c, 0x8f, 0x7d, 0xec, 0xd1, 0xc0, 0xeb, 0x51, 0xc6, 0xb8, 0xd4, - 0x30, 0x11, 0xe3, 0xac, 0x3f, 0x45, 0xb8, 0xb9, 0x3b, 0x27, 0xdb, 0x78, 0x1e, 0xa1, 0x90, 0xa4, - 0x05, 0x55, 0x46, 0x27, 0x28, 0x02, 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7b, 0x8c, 0x3c, 0x83, 0xb2, 0xaf, 0xe0, 0xc2, 0x34, 0x74, 0x65, - 0xee, 0xe7, 0x34, 0x62, 0x2c, 0x28, 0x5e, 0x33, 0x19, 0x4e, 0xed, 0x19, 0x9c, 0xbc, 0x81, 0x5a, - 0xe6, 0x91, 0x31, 0x0b, 0x9a, 0xfd, 0x30, 0x8f, 0xbd, 0x9b, 0xc2, 0x62, 0x89, 0x2c, 0xb1, 0xf9, - 0x02, 0x6a, 0x19, 0x79, 0xd2, 0x80, 0xe2, 0x19, 0x4e, 0x67, 0x05, 0x51, 0x4b, 0xb2, 0x09, 0xa5, - 0x0b, 0xea, 0x47, 0x49, 0x67, 0xc5, 0x9b, 0xed, 0xc2, 0x73, 0xa3, 0xb9, 0x03, 0x8d, 0x45, 0xed, - 0xeb, 0xf0, 0xad, 0x1f, 0x06, 0xac, 0xff, 0x93, 0x2b, 0xf2, 0x16, 0x6a, 0x13, 0xe5, 0xf3, 0x41, - 0x36, 0x25, 0x9d, 0x2b, 0x32, 0xdb, 0x3d, 0x4c, 0xa1, 0xb3, 0xc0, 0x32, 0x64, 0xe5, 0xdd, 0x22, - 0xe0, 0x3a, 0xde, 0x0d, 0xbe, 0x1a, 0xd9, 0x07, 0x5b, 0xb5, 0x9e, 0xe7, 0x20, 0x09, 0xa0, 0x92, - 0xb4, 0x13, 0xb9, 0xb3, 0xe4, 0x0d, 0x6a, 0xb6, 0x96, 0x8d, 0x98, 0xf5, 0xf8, 0xcb, 0xaf, 0xdf, - 0xdf, 0x0b, 0x0f, 0xac, 0x7b, 0xe9, 0xcf, 0xa2, 0x26, 0x56, 0xe8, 0x06, 0x4f, 0xff, 0x98, 0x6d, - 0x63, 0x6b, 0x54, 0xd6, 0xff, 0xc7, 0x93, 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x95, 0xc0, 0x03, - 0xf4, 0x95, 0x06, 0x00, 0x00, + proto.RegisterFile("proto/allocation/v1alpha1/allocation.proto", fileDescriptor_allocation_a8b682e2fff83ddc) +} + +var fileDescriptor_allocation_a8b682e2fff83ddc = []byte{ + // 631 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0xcd, 0x6e, 0xd3, 0x40, + 0x10, 0xc6, 0x49, 0x13, 0x92, 0x89, 0x28, 0x61, 0x5b, 0x09, 0xcb, 0x04, 0x88, 0x0c, 0x42, 0xa1, + 0x07, 0x47, 0x29, 0x07, 0xa0, 0x87, 0xa2, 0x8a, 0x9f, 0x4a, 0xa8, 0x2d, 0x95, 0x73, 0xe0, 0xc0, + 0x69, 0x63, 0x0f, 0xa9, 0xd5, 0x8d, 0xd7, 0xdd, 0x5d, 0x57, 0xca, 0x15, 0x09, 0xf1, 0x00, 0x1c, + 0x79, 0x2c, 0x5e, 0x81, 0x33, 0x2f, 0xc0, 0x05, 0xed, 0x26, 0x8e, 0x4d, 0x6a, 0x2c, 0xf5, 0xb6, + 0xbb, 0xf3, 0x7d, 0xdf, 0xce, 0x7c, 0x33, 0xbb, 0xb0, 0x93, 0x08, 0xae, 0xf8, 0x90, 0x32, 0xc6, + 0x03, 0xaa, 0x22, 0x1e, 0x0f, 0x2f, 0x47, 0x94, 0x25, 0x67, 0x74, 0x54, 0x38, 0xf3, 0x0c, 0x88, + 0xb4, 0xb2, 0x90, 0xd3, 0x9b, 0x72, 0x3e, 0x65, 0x38, 0xa4, 0x49, 0x34, 0xa4, 0x71, 0xcc, 0x95, + 0x81, 0xc9, 0x05, 0xce, 0xfd, 0x5d, 0x87, 0x3b, 0x07, 0x2b, 0xb2, 0x8f, 0x17, 0x29, 0x4a, 0x45, + 0x7a, 0xd0, 0x8e, 0xe9, 0x0c, 0x65, 0x42, 0x03, 0xb4, 0xad, 0xbe, 0x35, 0x68, 0xfb, 0xf9, 0x01, + 0xf9, 0x00, 0x5b, 0xb3, 0x94, 0xa9, 0xe8, 0x35, 0x4b, 0xa5, 0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0xb8, 0xf5, 0x8f, 0x57, 0xe4, 0x3d, 0x74, 0x66, 0x3a, + 0xe7, 0xa3, 0xa2, 0x25, 0x83, 0xff, 0x38, 0xeb, 0x1d, 0xe7, 0xd0, 0x65, 0x61, 0x05, 0xb2, 0xce, + 0x6e, 0x1d, 0x70, 0x9d, 0xec, 0x76, 0xbf, 0x5a, 0xc5, 0x8f, 0x50, 0x0f, 0x58, 0x14, 0x20, 0x49, + 0xa0, 0xb5, 0x3c, 0x44, 0x72, 0xaf, 0xe2, 0x6d, 0x3b, 0xbd, 0xaa, 0x87, 0xe0, 0x3e, 0xfd, 0xf2, + 0xf3, 0xd7, 0xf7, 0xda, 0x23, 0xf7, 0x41, 0xfe, 0x63, 0xeb, 0x77, 0x25, 0xcd, 0x18, 0xe7, 0x7f, + 0xf7, 0x9e, 0xb5, 0x33, 0x69, 0x9a, 0x7f, 0xf9, 0xd9, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x19, + 0x62, 0xb9, 0x64, 0xed, 0x05, 0x00, 0x00, } diff --git a/pkg/allocation/go/v1alpha1/allocation.swagger.json b/pkg/allocation/go/v1alpha1/allocation.swagger.json index e72aefbbef..1c1b585eb3 100644 --- a/pkg/allocation/go/v1alpha1/allocation.swagger.json +++ b/pkg/allocation/go/v1alpha1/allocation.swagger.json @@ -50,18 +50,6 @@ ], "default": "Packed" }, - "AllocationResponseGameServerAllocationState": { - "type": "string", - "enum": [ - "Unknown", - "Allocated", - "UnAllocated", - "Contention" - ], - "default": "Unknown", - "description": "- Allocated: Allocated is for successful allocation\n - UnAllocated: UnAllocated is for unsuccessful allocation due to lack of gameserver resources\n - Contention: Contention is for unsuccessful allocation due to contention", - "title": "The allocation state" - }, "AllocationResponseGameServerStatusPort": { "type": "object", "properties": { @@ -110,9 +98,6 @@ "v1alpha1AllocationResponse": { "type": "object", "properties": { - "state": { - "$ref": "#/definitions/AllocationResponseGameServerAllocationState" - }, "gameServerName": { "type": "string" }, diff --git a/pkg/gameserverallocations/allocator.go b/pkg/gameserverallocations/allocator.go index a44fe77ed0..0ed59b5655 100644 --- a/pkg/gameserverallocations/allocator.go +++ b/pkg/gameserverallocations/allocator.go @@ -38,7 +38,9 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" "google.golang.org/grpc" + "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" + "google.golang.org/grpc/status" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -181,7 +183,7 @@ func (c *Allocator) Sync(stop <-chan struct{}) error { func (c *Allocator) Allocate(gsa *allocationv1.GameServerAllocation, stop <-chan struct{}) (k8sruntime.Object, error) { // server side validation if causes, ok := gsa.Validate(); !ok { - status := &metav1.Status{ + s := &metav1.Status{ Status: metav1.StatusFailure, Message: fmt.Sprintf("GameServerAllocation is invalid: Invalid value: %#v", gsa), Reason: metav1.StatusReasonInvalid, @@ -194,14 +196,14 @@ func (c *Allocator) Allocate(gsa *allocationv1.GameServerAllocation, stop <-chan } var gvks []schema.GroupVersionKind - gvks, _, err := apiserver.Scheme.ObjectKinds(status) + gvks, _, err := apiserver.Scheme.ObjectKinds(s) if err != nil { return nil, errors.Wrap(err, "could not find objectkinds for status") } c.loggerForGameServerAllocation(gsa).Debug("GameServerAllocation is invalid") - status.TypeMeta = metav1.TypeMeta{Kind: gvks[0].Kind, APIVersion: gvks[0].Version} - return status, nil + s.TypeMeta = metav1.TypeMeta{Kind: gvks[0].Kind, APIVersion: gvks[0].Version} + return s, nil } // If multi-cluster setting is enabled, allocate base on the multicluster allocation policy. @@ -321,7 +323,6 @@ func (c *Allocator) applyMultiClusterAllocation(gsa *allocationv1.GameServerAllo func (c *Allocator) allocateFromRemoteCluster(gsa *allocationv1.GameServerAllocation, connectionInfo *multiclusterv1.ClusterConnectionInfo, namespace string) (*allocationv1.GameServerAllocation, error) { var allocationResponse *pb.AllocationResponse - // TODO: handle converting error to apiserver error // TODO: cache the client dialOpts, err := c.createRemoteClusterDialOption(namespace, connectionInfo.SecretName) if err != nil { @@ -357,6 +358,7 @@ func (c *Allocator) allocateFromRemoteCluster(gsa *allocationv1.GameServerAlloca } return nil }) + return converters.ConvertAllocationResponseV1Alpha1ToGSAV1(allocationResponse), err } @@ -545,6 +547,14 @@ func Retry(backoff wait.Backoff, fn func() error) error { var lastConflictErr error err := wait.ExponentialBackoff(backoff, func() (bool, error) { err := fn() + + st, ok := status.FromError(err) + if ok { + if st.Code() == codes.ResourceExhausted { + return true, err + } + } + switch { case err == nil: return true, nil diff --git a/pkg/gameserverallocations/controller_test.go b/pkg/gameserverallocations/controller_test.go index ebff3e4952..ea38cba00d 100644 --- a/pkg/gameserverallocations/controller_test.go +++ b/pkg/gameserverallocations/controller_test.go @@ -38,6 +38,8 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8sruntime "k8s.io/apimachinery/pkg/runtime" @@ -968,7 +970,6 @@ func TestMultiClusterAllocationFromRemote(t *testing.T) { assert.Equal(t, endpoint+":443", e) serverResponse := pb.AllocationResponse{ GameServerName: expectedGSName, - State: pb.AllocationResponse_Allocated, } return &serverResponse, nil } @@ -979,7 +980,7 @@ func TestMultiClusterAllocationFromRemote(t *testing.T) { } }) - t.Run("Remote server returns unallocated and then error", func(t *testing.T) { + t.Run("Remote server returns conflict and then random error", func(t *testing.T) { c, m := newFakeController() fleetName := addReactorForGameServer(&m) @@ -992,7 +993,7 @@ func TestMultiClusterAllocationFromRemote(t *testing.T) { if count == 0 { serverResponse := pb.AllocationResponse{} count++ - return &serverResponse, nil + return &serverResponse, status.Error(codes.Aborted, "conflict") } retry++ @@ -1090,7 +1091,6 @@ func TestMultiClusterAllocationFromRemote(t *testing.T) { assert.Equal(t, healthyEndpoint, endpoint) serverResponse := pb.AllocationResponse{ GameServerName: expectedGSName, - State: pb.AllocationResponse_Allocated, } return &serverResponse, nil } diff --git a/proto/allocation/v1alpha1/allocation.proto b/proto/allocation/v1alpha1/allocation.proto index 28bb674b27..7fe7783fba 100644 --- a/proto/allocation/v1alpha1/allocation.proto +++ b/proto/allocation/v1alpha1/allocation.proto @@ -54,19 +54,6 @@ message AllocationRequest { } message AllocationResponse { - GameServerAllocationState state = 1; - - // The allocation state - enum GameServerAllocationState { - Unknown = 0; - // Allocated is for successful allocation - Allocated = 1; - // UnAllocated is for unsuccessful allocation due to lack of gameserver resources - UnAllocated = 2; - // Contention is for unsuccessful allocation due to contention - Contention = 3; - } - string gameServerName = 2; repeated GameServerStatusPort ports = 3; string address = 4; diff --git a/test/e2e/allocator_test.go b/test/e2e/allocator_test.go index c6b53c781c..61b1d246fd 100644 --- a/test/e2e/allocator_test.go +++ b/test/e2e/allocator_test.go @@ -101,13 +101,11 @@ func TestAllocator(t *testing.T) { logrus.WithError(err).Info("failing Allocate request") return false, nil } - assert.Equal(t, pb.AllocationResponse_Allocated, response.State) + validateAllocatorResponse(t, response) return true, nil }) - if !assert.NoError(t, err) { - assert.FailNow(t, "Http test failed") - } + assert.NoError(t, err) } // Tests multi-cluster allocation by reusing the same cluster but across namespace. @@ -172,7 +170,7 @@ func TestAllocatorCrossNamespace(t *testing.T) { conn, err := grpc.Dial(requestURL, dialOpts) if err != nil { - logrus.WithError(err).Info("failing http request") + logrus.WithError(err).Info("failing grpc.Dial") return false, nil } defer conn.Close() // nolint: errcheck @@ -183,13 +181,11 @@ func TestAllocatorCrossNamespace(t *testing.T) { logrus.WithError(err).Info("failing Allocate request") return false, nil } - assert.Equal(t, pb.AllocationResponse_Allocated, response.State) + validateAllocatorResponse(t, response) return true, nil }) - if !assert.NoError(t, err) { - assert.FailNow(t, "Http test failed") - } + assert.NoError(t, err) } func createAllocationPolicy(t *testing.T, p *multiclusterv1.GameServerAllocationPolicy) { @@ -393,3 +389,14 @@ func generateTLSCertPair(t *testing.T, host string) ([]byte, []byte) { return pemPubBytes, pemPrivBytes } + +func validateAllocatorResponse(t *testing.T, resp *pb.AllocationResponse) { + t.Helper() + if !assert.NotNil(t, resp) { + return + } + assert.Greater(t, len(resp.Ports), 0) + assert.NotEmpty(t, resp.GameServerName) + assert.NotEmpty(t, resp.Address) + assert.NotEmpty(t, resp.NodeName) +}