Skip to content

Commit

Permalink
Adds CounterAction and ListAction the GameServerAllocation Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
igooch committed Sep 29, 2023
1 parent cc40782 commit a035841
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 4 deletions.
122 changes: 118 additions & 4 deletions pkg/allocation/converters/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"agones.dev/agones/pkg/util/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -73,8 +74,16 @@ func ConvertAllocationRequestToGSA(in *pb.AllocationRequest) *allocationv1.GameS
gsa.Spec.Required = *selector
}

if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) && in.Priorities != nil {
gsa.Spec.Priorities = convertAllocationPrioritiesToGSAPriorities(in.GetPriorities())
if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
if in.Priorities != nil {
gsa.Spec.Priorities = convertAllocationPrioritiesToGSAPriorities(in.GetPriorities())
}
if in.Counters != nil {
gsa.Spec.Counters = convertAllocationCountersToGSACounterActions(in.GetCounters())
}
if in.Lists != nil {
gsa.Spec.Lists = convertAllocationListsToGSAListActions(in.GetLists())
}
}

return gsa
Expand Down Expand Up @@ -120,8 +129,16 @@ func ConvertGSAToAllocationRequest(in *allocationv1.GameServerAllocation) *pb.Al
out.MultiClusterSetting.PolicySelector = convertInternalLabelSelectorToLabelSelector(&in.Spec.MultiClusterSetting.PolicySelector)
}

if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) && in.Spec.Priorities != nil {
out.Priorities = convertGSAPrioritiesToAllocationPriorities(in.Spec.Priorities)
if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
if in.Spec.Priorities != nil {
out.Priorities = convertGSAPrioritiesToAllocationPriorities(in.Spec.Priorities)
}
if in.Spec.Counters != nil {
out.Counters = convertGSACounterActionsToAllocationCounters(in.Spec.Counters)
}
if in.Spec.Lists != nil {
out.Lists = convertGSAListActionsToAllocationLists(in.Spec.Lists)
}
}

return out
Expand Down Expand Up @@ -469,3 +486,100 @@ func convertGSAPrioritiesToAllocationPriorities(in []agonesv1.Priority) []*pb.Pr
}
return out
}

// convertAllocationCountersToGSACounterActions converts a map of AllocationRequest_Counters to a
// map of GameServerAllocationSpec CounterActions
func convertAllocationCountersToGSACounterActions(in map[string]*pb.CounterAction) map[string]allocationv1.CounterAction {
out := map[string]allocationv1.CounterAction{}
for k, v := range in {
ca := allocationv1.CounterAction{}

if v.Action != nil {
action := v.Action.GetValue()
ca.Action = &action
}
if v.Amount != nil {
amount := v.Amount.GetValue()
ca.Amount = &amount
}
if v.Capacity != nil {
capacity := v.Capacity.GetValue()
ca.Capacity = &capacity
}

// TODO: Action and Amount must be used together. Do we want to check that both Action & Amount are not nil,
// or both of Action & Amount are nil? Or add an error in CounterActions in pkg/apis/allocation/v1/gameserverallocation.go?
out[k] = ca
}
return out
}

// convertGSACounterActionsToAllocationCounters converts a map of GameServerAllocationSpec CounterActions
// to a map of AllocationRequest_Counters
func convertGSACounterActionsToAllocationCounters(in map[string]allocationv1.CounterAction) map[string]*pb.CounterAction {
out := map[string]*pb.CounterAction{}

for k, v := range in {
ca := pb.CounterAction{}

if v.Action != nil {
ca.Action = wrapperspb.String(*v.Action)
}
if v.Amount != nil {
ca.Amount = wrapperspb.Int64(*v.Amount)
}
if v.Capacity != nil {
ca.Capacity = wrapperspb.Int64(*v.Capacity)
}

out[k] = &ca
}
return out
}

// convertAllocationListsToGSAListActions converts a map of AllocationRequest_Lists to a
// map of GameServerAllocationSpec ListActions
func convertAllocationListsToGSAListActions(in map[string]*pb.ListAction) map[string]allocationv1.ListAction {
out := map[string]allocationv1.ListAction{}

for k, v := range in {
la := allocationv1.ListAction{}

if v.AddValues != nil {
addValues := v.GetAddValues()
copyValues := make([]string, len(addValues))
copy(copyValues, addValues)
la.AddValues = copyValues
}
if v.Capacity != nil {
capacity := v.Capacity.GetValue()
la.Capacity = &capacity
}

out[k] = la
}

return out
}

// convertGSAListActionsToAllocationLists converts a map of GameServerAllocationSpec ListActions
// to a map of AllocationRequest_Lists
func convertGSAListActionsToAllocationLists(in map[string]allocationv1.ListAction) map[string]*pb.ListAction {
out := map[string]*pb.ListAction{}

for k, v := range in {
la := pb.ListAction{}

if v.AddValues != nil {
copyValues := make([]string, len(v.AddValues))
copy(copyValues, v.AddValues)
la.AddValues = copyValues
}
if v.Capacity != nil {
la.Capacity = wrapperspb.Int64(*v.Capacity)
}

out[k] = &la
}
return out
}
92 changes: 92 additions & 0 deletions pkg/allocation/converters/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
allocated := agonesv1.GameServerStateAllocated
ready := agonesv1.GameServerStateReady
increment := agonesv1.GameServerPriorityIncrement
decrement := agonesv1.GameServerPriorityDecrement
one := int64(1)
ten := int64(10)

tests := []struct {
name string
Expand Down Expand Up @@ -116,6 +121,23 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
Order: pb.Priority_Ascending,
},
},
Counters: map[string]*pb.CounterAction{
"o": {
Action: wrapperspb.String("Increment"),
Amount: wrapperspb.Int64(1),
},
"q": {
Action: wrapperspb.String("Decrement"),
Amount: wrapperspb.Int64(1),
Capacity: wrapperspb.Int64(10),
},
},
Lists: map[string]*pb.ListAction{
"p": {
AddValues: []string{"foo", "bar", "baz"},
Capacity: wrapperspb.Int64(10),
},
},
Scheduling: pb.AllocationRequest_Packed,
Metadata: &pb.MetaPatch{
Labels: map[string]string{
Expand Down Expand Up @@ -181,6 +203,23 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
Order: "Ascending",
},
},
Counters: map[string]allocationv1.CounterAction{
"o": {
Action: &increment,
Amount: &one,
},
"q": {
Action: &decrement,
Amount: &one,
Capacity: &ten,
},
},
Lists: map[string]allocationv1.ListAction{
"p": {
AddValues: []string{"foo", "bar", "baz"},
Capacity: &ten,
},
},
Selectors: []allocationv1.GameServerSelector{
{
LabelSelector: metav1.LabelSelector{
Expand Down Expand Up @@ -534,6 +573,11 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
},
},
},
Lists: map[string]*pb.ListAction{
"d": {
Capacity: wrapperspb.Int64(one),
},
},
Scheduling: pb.AllocationRequest_Distributed,
Metadata: &pb.MetaPatch{},
},
Expand Down Expand Up @@ -568,6 +612,11 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
},
},
},
Lists: map[string]allocationv1.ListAction{
"d": {
Capacity: &one,
},
},
Scheduling: apis.Distributed,
},
},
Expand All @@ -589,6 +638,11 @@ func TestConvertAllocationRequestToGameServerAllocation(t *testing.T) {
}

func TestConvertGSAToAllocationRequest(t *testing.T) {
increment := agonesv1.GameServerPriorityIncrement
decrement := agonesv1.GameServerPriorityDecrement
two := int64(2)
twenty := int64(20)

tests := []struct {
name string
features string
Expand Down Expand Up @@ -669,6 +723,25 @@ func TestConvertGSAToAllocationRequest(t *testing.T) {
Order: "Descending",
},
},
Counters: map[string]allocationv1.CounterAction{
"a": {
Action: &decrement,
Amount: &two,
Capacity: &twenty,
},
"c": {
Action: &increment,
Amount: &two,
},
},
Lists: map[string]allocationv1.ListAction{
"b": {
AddValues: []string{"hello", "world"},
},
"d": {
Capacity: &two,
},
},
Scheduling: apis.Distributed,
},
},
Expand Down Expand Up @@ -729,6 +802,25 @@ func TestConvertGSAToAllocationRequest(t *testing.T) {
Order: pb.Priority_Descending,
},
},
Counters: map[string]*pb.CounterAction{
"a": {
Action: wrapperspb.String(decrement),
Amount: wrapperspb.Int64(two),
Capacity: wrapperspb.Int64(twenty),
},
"c": {
Action: wrapperspb.String(increment),
Amount: wrapperspb.Int64(two),
},
},
Lists: map[string]*pb.ListAction{
"b": {
AddValues: []string{"hello", "world"},
},
"d": {
Capacity: wrapperspb.Int64(two),
},
},
},
},
}
Expand Down

0 comments on commit a035841

Please sign in to comment.