agones.dev/agones@v1.54.0/pkg/allocation/converters/converter.go (about)

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package converters includes API conversions between GameServerAllocation API and the Allocation proto APIs.
    16  package converters
    17  
    18  import (
    19  	"google.golang.org/grpc/codes"
    20  	"google.golang.org/grpc/status"
    21  	"google.golang.org/protobuf/types/known/wrapperspb"
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	pb "agones.dev/agones/pkg/allocation/go"
    26  	"agones.dev/agones/pkg/apis"
    27  	agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
    28  	allocationv1 "agones.dev/agones/pkg/apis/allocation/v1"
    29  	"agones.dev/agones/pkg/util/runtime"
    30  )
    31  
    32  // ConvertAllocationRequestToGSA converts AllocationRequest to GameServerAllocation V1 (GSA)
    33  func ConvertAllocationRequestToGSA(in *pb.AllocationRequest) *allocationv1.GameServerAllocation {
    34  	if in == nil {
    35  		return nil
    36  	}
    37  
    38  	gsa := &allocationv1.GameServerAllocation{
    39  		ObjectMeta: metav1.ObjectMeta{
    40  			Namespace: in.GetNamespace(),
    41  		},
    42  		Spec: allocationv1.GameServerAllocationSpec{
    43  			// nolint:staticcheck
    44  			Preferred:  convertGameServerSelectorsToInternalGameServerSelectors(in.GetPreferredGameServerSelectors()),
    45  			Selectors:  convertGameServerSelectorsToInternalGameServerSelectors(in.GetGameServerSelectors()),
    46  			Scheduling: convertAllocationSchedulingToGSASchedulingStrategy(in.GetScheduling()),
    47  		},
    48  	}
    49  
    50  	if in.GetMultiClusterSetting() != nil {
    51  		gsa.Spec.MultiClusterSetting = allocationv1.MultiClusterSetting{
    52  			Enabled: in.GetMultiClusterSetting().GetEnabled(),
    53  		}
    54  		if ls := convertLabelSelectorToInternalLabelSelector(in.GetMultiClusterSetting().GetPolicySelector()); ls != nil {
    55  			gsa.Spec.MultiClusterSetting.PolicySelector = *ls
    56  		}
    57  	}
    58  
    59  	// Accept both metadata (preferred) and metapatch until metapatch is fully removed.
    60  	metadata := in.GetMetadata()
    61  	if metadata == nil {
    62  		metadata = in.GetMetaPatch()
    63  	}
    64  
    65  	if metadata != nil {
    66  		gsa.Spec.MetaPatch = allocationv1.MetaPatch{
    67  			Labels:      metadata.GetLabels(),
    68  			Annotations: metadata.GetAnnotations(),
    69  		}
    70  	}
    71  
    72  	// nolint:staticcheck
    73  	if selector := convertGameServerSelectorToInternalGameServerSelector(in.GetRequiredGameServerSelector()); selector != nil {
    74  		// nolint:staticcheck
    75  		gsa.Spec.Required = *selector
    76  	}
    77  
    78  	if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
    79  		if in.Priorities != nil {
    80  			gsa.Spec.Priorities = convertAllocationPrioritiesToGSAPriorities(in.GetPriorities())
    81  		}
    82  		if in.Counters != nil {
    83  			gsa.Spec.Counters = convertAllocationCountersToGSACounterActions(in.GetCounters())
    84  		}
    85  		if in.Lists != nil {
    86  			gsa.Spec.Lists = convertAllocationListsToGSAListActions(in.GetLists())
    87  		}
    88  	}
    89  
    90  	return gsa
    91  }
    92  
    93  // ConvertGSAToAllocationRequest converts AllocationRequest to GameServerAllocation V1 (GSA)
    94  func ConvertGSAToAllocationRequest(in *allocationv1.GameServerAllocation) *pb.AllocationRequest {
    95  	if in == nil {
    96  		return nil
    97  	}
    98  
    99  	out := &pb.AllocationRequest{
   100  		Namespace:           in.GetNamespace(),
   101  		Scheduling:          convertGSASchedulingStrategyToAllocationScheduling(in.Spec.Scheduling),
   102  		GameServerSelectors: convertInternalLabelSelectorsToLabelSelectors(in.Spec.Selectors),
   103  		MultiClusterSetting: &pb.MultiClusterSetting{
   104  			Enabled: in.Spec.MultiClusterSetting.Enabled,
   105  		},
   106  		Metadata: &pb.MetaPatch{
   107  			Labels:      in.Spec.MetaPatch.Labels,
   108  			Annotations: in.Spec.MetaPatch.Annotations,
   109  		},
   110  		// MetaPatch is deprecated, but we do a double write here to both metapatch and metadata
   111  		// to ensure that multi-cluster allocation still works when one cluster has the field
   112  		// and another one does not have the field yet.
   113  		MetaPatch: &pb.MetaPatch{
   114  			Labels:      in.Spec.MetaPatch.Labels,
   115  			Annotations: in.Spec.MetaPatch.Annotations,
   116  		},
   117  	}
   118  
   119  	l := len(out.GameServerSelectors)
   120  	if l > 0 {
   121  		// nolint:staticcheck
   122  		// Sets all but the last GameServerSelector as PreferredGameServerSelectors
   123  		out.PreferredGameServerSelectors = out.GameServerSelectors[:l-1]
   124  		// nolint:staticcheck
   125  		// Sets the last GameServerSelector as RequiredGameServerSelector
   126  		out.RequiredGameServerSelector = out.GameServerSelectors[l-1]
   127  	}
   128  
   129  	if in.Spec.MultiClusterSetting.Enabled {
   130  		out.MultiClusterSetting.PolicySelector = convertInternalLabelSelectorToLabelSelector(&in.Spec.MultiClusterSetting.PolicySelector)
   131  	}
   132  
   133  	if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
   134  		if in.Spec.Priorities != nil {
   135  			out.Priorities = convertGSAPrioritiesToAllocationPriorities(in.Spec.Priorities)
   136  		}
   137  		if in.Spec.Counters != nil {
   138  			out.Counters = convertGSACounterActionsToAllocationCounters(in.Spec.Counters)
   139  		}
   140  		if in.Spec.Lists != nil {
   141  			out.Lists = convertGSAListActionsToAllocationLists(in.Spec.Lists)
   142  		}
   143  	}
   144  
   145  	return out
   146  }
   147  
   148  // convertAllocationSchedulingToGSASchedulingStrategy converts AllocationRequest_SchedulingStrategy to apis.SchedulingStrategy
   149  func convertAllocationSchedulingToGSASchedulingStrategy(in pb.AllocationRequest_SchedulingStrategy) apis.SchedulingStrategy {
   150  	switch in {
   151  	case pb.AllocationRequest_Packed:
   152  		return apis.Packed
   153  	case pb.AllocationRequest_Distributed:
   154  		return apis.Distributed
   155  	}
   156  	return apis.Packed
   157  }
   158  
   159  // convertGSASchedulingStrategyToAllocationScheduling converts  apis.SchedulingStrategy to pb.AllocationRequest_SchedulingStrategy
   160  func convertGSASchedulingStrategyToAllocationScheduling(in apis.SchedulingStrategy) pb.AllocationRequest_SchedulingStrategy {
   161  	switch in {
   162  	case apis.Packed:
   163  		return pb.AllocationRequest_Packed
   164  	case apis.Distributed:
   165  		return pb.AllocationRequest_Distributed
   166  	}
   167  	return pb.AllocationRequest_Packed
   168  }
   169  
   170  func convertLabelSelectorToInternalLabelSelector(in *pb.LabelSelector) *metav1.LabelSelector {
   171  	if in == nil {
   172  		return nil
   173  	}
   174  	return &metav1.LabelSelector{MatchLabels: in.GetMatchLabels()}
   175  }
   176  
   177  func convertGameServerSelectorToInternalGameServerSelector(in *pb.GameServerSelector) *allocationv1.GameServerSelector {
   178  	if in == nil {
   179  		return nil
   180  	}
   181  	result := &allocationv1.GameServerSelector{
   182  		LabelSelector: metav1.LabelSelector{MatchLabels: in.GetMatchLabels()},
   183  	}
   184  
   185  	switch in.GameServerState {
   186  	case pb.GameServerSelector_ALLOCATED:
   187  		allocated := agonesv1.GameServerStateAllocated
   188  		result.GameServerState = &allocated
   189  	case pb.GameServerSelector_READY:
   190  		ready := agonesv1.GameServerStateReady
   191  		result.GameServerState = &ready
   192  	}
   193  
   194  	if runtime.FeatureEnabled(runtime.FeaturePlayerAllocationFilter) && in.Players != nil {
   195  		result.Players = &allocationv1.PlayerSelector{
   196  			MinAvailable: int64(in.Players.MinAvailable),
   197  			MaxAvailable: int64(in.Players.MaxAvailable),
   198  		}
   199  	}
   200  
   201  	if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
   202  		if in.Counters != nil {
   203  			result.Counters = map[string]allocationv1.CounterSelector{}
   204  			for k, v := range in.GetCounters() {
   205  				result.Counters[k] = allocationv1.CounterSelector{
   206  					MinCount:     v.MinCount,
   207  					MaxCount:     v.MaxCount,
   208  					MinAvailable: v.MinAvailable,
   209  					MaxAvailable: v.MaxAvailable,
   210  				}
   211  			}
   212  		}
   213  		if in.Lists != nil {
   214  			result.Lists = map[string]allocationv1.ListSelector{}
   215  			for k, v := range in.GetLists() {
   216  				result.Lists[k] = allocationv1.ListSelector{
   217  					ContainsValue: v.ContainsValue,
   218  					MinAvailable:  v.MinAvailable,
   219  					MaxAvailable:  v.MaxAvailable,
   220  				}
   221  			}
   222  		}
   223  	}
   224  
   225  	return result
   226  }
   227  
   228  func convertInternalGameServerSelectorToGameServer(in *allocationv1.GameServerSelector) *pb.GameServerSelector {
   229  	if in == nil {
   230  		return nil
   231  	}
   232  	result := &pb.GameServerSelector{
   233  		MatchLabels: in.MatchLabels,
   234  	}
   235  
   236  	if in.GameServerState != nil {
   237  		switch *in.GameServerState {
   238  		case agonesv1.GameServerStateReady:
   239  			result.GameServerState = pb.GameServerSelector_READY
   240  		case agonesv1.GameServerStateAllocated:
   241  			result.GameServerState = pb.GameServerSelector_ALLOCATED
   242  		}
   243  	}
   244  
   245  	if runtime.FeatureEnabled(runtime.FeaturePlayerAllocationFilter) && in.Players != nil {
   246  		result.Players = &pb.PlayerSelector{
   247  			MinAvailable: uint64(in.Players.MinAvailable),
   248  			MaxAvailable: uint64(in.Players.MaxAvailable),
   249  		}
   250  	}
   251  
   252  	if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
   253  		if in.Counters != nil {
   254  			result.Counters = map[string]*pb.CounterSelector{}
   255  			for k, v := range in.Counters {
   256  				result.Counters[k] = &pb.CounterSelector{
   257  					MinCount:     v.MinCount,
   258  					MaxCount:     v.MaxCount,
   259  					MinAvailable: v.MinAvailable,
   260  					MaxAvailable: v.MaxAvailable,
   261  				}
   262  			}
   263  		}
   264  		if in.Lists != nil {
   265  			result.Lists = map[string]*pb.ListSelector{}
   266  			for k, v := range in.Lists {
   267  				result.Lists[k] = &pb.ListSelector{
   268  					ContainsValue: v.ContainsValue,
   269  					MinAvailable:  v.MinAvailable,
   270  					MaxAvailable:  v.MaxAvailable,
   271  				}
   272  			}
   273  		}
   274  	}
   275  
   276  	return result
   277  }
   278  
   279  func convertInternalLabelSelectorToLabelSelector(in *metav1.LabelSelector) *pb.LabelSelector {
   280  	if in == nil {
   281  		return nil
   282  	}
   283  	return &pb.LabelSelector{MatchLabels: in.MatchLabels}
   284  }
   285  
   286  func convertInternalLabelSelectorsToLabelSelectors(in []allocationv1.GameServerSelector) []*pb.GameServerSelector {
   287  	var result []*pb.GameServerSelector
   288  	for _, l := range in {
   289  		c := convertInternalGameServerSelectorToGameServer(&l)
   290  		result = append(result, c)
   291  	}
   292  	return result
   293  }
   294  
   295  func convertGameServerSelectorsToInternalGameServerSelectors(in []*pb.GameServerSelector) []allocationv1.GameServerSelector {
   296  	var result []allocationv1.GameServerSelector
   297  	for _, l := range in {
   298  		if selector := convertGameServerSelectorToInternalGameServerSelector(l); selector != nil {
   299  			result = append(result, *selector)
   300  		}
   301  	}
   302  	return result
   303  }
   304  
   305  // ConvertGSAToAllocationResponse converts GameServerAllocation V1 (GSA) to AllocationResponse
   306  func ConvertGSAToAllocationResponse(in *allocationv1.GameServerAllocation, grpcUnallocatedStatusCode codes.Code) (*pb.AllocationResponse, error) {
   307  	if in == nil {
   308  		return nil, nil
   309  	}
   310  
   311  	if err := convertStateV1ToError(in.Status.State, grpcUnallocatedStatusCode); err != nil {
   312  		return nil, err
   313  	}
   314  
   315  	res := &pb.AllocationResponse{
   316  		GameServerName: in.Status.GameServerName,
   317  		Address:        in.Status.Address,
   318  		Addresses:      convertGSAAddressesToAllocationAddresses(in.Status.Addresses),
   319  		NodeName:       in.Status.NodeName,
   320  		Ports:          convertGSAAgonesPortsToAllocationPorts(in.Status.Ports),
   321  		Source:         in.Status.Source,
   322  		Metadata:       convertGSAMetadataToAllocationMetadata(in.Status.Metadata),
   323  	}
   324  	if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
   325  		if in.Status.Counters != nil {
   326  			res.Counters = convertGSACountersToAllocationCounters(in.Status.Counters)
   327  		}
   328  		if in.Status.Lists != nil {
   329  			res.Lists = convertGSAListsToAllocationLists(in.Status.Lists)
   330  		}
   331  	}
   332  
   333  	return res, nil
   334  }
   335  
   336  // convertGSACountersToAllocationCounters converts a map of GameServerStatusCounter to AllocationResponse_CounterStatus
   337  func convertGSACountersToAllocationCounters(in map[string]agonesv1.CounterStatus) map[string]*pb.AllocationResponse_CounterStatus {
   338  	out := map[string]*pb.AllocationResponse_CounterStatus{}
   339  	for k, v := range in {
   340  		out[k] = &pb.AllocationResponse_CounterStatus{
   341  			Count:    wrapperspb.Int64(v.Count),
   342  			Capacity: wrapperspb.Int64(v.Capacity),
   343  		}
   344  	}
   345  	return out
   346  }
   347  
   348  // convertGSAListsToAllocationLists converts a map of GameServerStatusList to AllocationResponse_ListStatus
   349  func convertGSAListsToAllocationLists(in map[string]agonesv1.ListStatus) map[string]*pb.AllocationResponse_ListStatus {
   350  	out := map[string]*pb.AllocationResponse_ListStatus{}
   351  	for k, v := range in {
   352  		out[k] = &pb.AllocationResponse_ListStatus{
   353  			Values:   v.Values,
   354  			Capacity: wrapperspb.Int64(v.Capacity),
   355  		}
   356  	}
   357  	return out
   358  }
   359  
   360  // ConvertAllocationResponseToGSA converts AllocationResponse to GameServerAllocation V1 (GSA)
   361  func ConvertAllocationResponseToGSA(in *pb.AllocationResponse, rs string) *allocationv1.GameServerAllocation {
   362  	if in == nil {
   363  		return nil
   364  	}
   365  
   366  	out := &allocationv1.GameServerAllocation{
   367  		Status: allocationv1.GameServerAllocationStatus{
   368  			State:          allocationv1.GameServerAllocationAllocated,
   369  			GameServerName: in.GameServerName,
   370  			Address:        in.Address,
   371  			Addresses:      convertAllocationAddressesToGSAAddresses(in.Addresses),
   372  			NodeName:       in.NodeName,
   373  			Ports:          convertAllocationPortsToGSAAgonesPorts(in.Ports),
   374  			Source:         rs,
   375  			Metadata:       convertAllocationMetadataToGSAMetadata(in.Metadata),
   376  		},
   377  	}
   378  	if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
   379  		if in.Counters != nil {
   380  			out.Status.Counters = convertAllocationCountersToGSACounters(in.Counters)
   381  		}
   382  		if in.Lists != nil {
   383  			out.Status.Lists = convertAllocationListsToGSALists(in.Lists)
   384  		}
   385  	}
   386  	out.SetGroupVersionKind(allocationv1.SchemeGroupVersion.WithKind("GameServerAllocation"))
   387  
   388  	return out
   389  }
   390  
   391  // convertGSAAddressesToAllocationAddresses converts corev1.NodeAddress to AllocationResponse_GameServerStatusAddress
   392  func convertGSAAddressesToAllocationAddresses(in []corev1.NodeAddress) []*pb.AllocationResponse_GameServerStatusAddress {
   393  	var addresses []*pb.AllocationResponse_GameServerStatusAddress
   394  	for _, addr := range in {
   395  		addresses = append(addresses, &pb.AllocationResponse_GameServerStatusAddress{
   396  			Type:    string(addr.Type),
   397  			Address: addr.Address,
   398  		})
   399  	}
   400  	return addresses
   401  }
   402  
   403  // convertAllocationAddressesToGSAAddresses converts AllocationResponse_GameServerStatusAddress to corev1.NodeAddress
   404  func convertAllocationAddressesToGSAAddresses(in []*pb.AllocationResponse_GameServerStatusAddress) []corev1.NodeAddress {
   405  	var addresses []corev1.NodeAddress
   406  	for _, addr := range in {
   407  		addresses = append(addresses, corev1.NodeAddress{
   408  			Type:    corev1.NodeAddressType(addr.Type),
   409  			Address: addr.Address,
   410  		})
   411  	}
   412  	return addresses
   413  }
   414  
   415  // convertGSAAgonesPortsToAllocationPorts converts GameServerStatusPort V1 (GSA) to AllocationResponse_GameServerStatusPort
   416  func convertGSAAgonesPortsToAllocationPorts(in []agonesv1.GameServerStatusPort) []*pb.AllocationResponse_GameServerStatusPort {
   417  	var pbPorts []*pb.AllocationResponse_GameServerStatusPort
   418  	for _, port := range in {
   419  		pbPort := &pb.AllocationResponse_GameServerStatusPort{
   420  			Name: port.Name,
   421  			Port: port.Port,
   422  		}
   423  		pbPorts = append(pbPorts, pbPort)
   424  	}
   425  	return pbPorts
   426  }
   427  
   428  // convertAllocationPortsToGSAAgonesPorts converts AllocationResponse_GameServerStatusPort to GameServerStatusPort V1 (GSA)
   429  func convertAllocationPortsToGSAAgonesPorts(in []*pb.AllocationResponse_GameServerStatusPort) []agonesv1.GameServerStatusPort {
   430  	var out []agonesv1.GameServerStatusPort
   431  	for _, port := range in {
   432  		p := &agonesv1.GameServerStatusPort{
   433  			Name: port.Name,
   434  			Port: port.Port,
   435  		}
   436  		out = append(out, *p)
   437  	}
   438  	return out
   439  }
   440  
   441  func convertGSAMetadataToAllocationMetadata(in *allocationv1.GameServerMetadata) *pb.AllocationResponse_GameServerMetadata {
   442  	if in == nil {
   443  		return nil
   444  	}
   445  	metadata := &pb.AllocationResponse_GameServerMetadata{}
   446  	metadata.Labels = in.Labels
   447  	metadata.Annotations = in.Annotations
   448  	return metadata
   449  }
   450  
   451  func convertAllocationMetadataToGSAMetadata(in *pb.AllocationResponse_GameServerMetadata) *allocationv1.GameServerMetadata {
   452  	if in == nil {
   453  		return nil
   454  	}
   455  	metadata := &allocationv1.GameServerMetadata{}
   456  	metadata.Labels = in.Labels
   457  	metadata.Annotations = in.Annotations
   458  	return metadata
   459  }
   460  
   461  func convertAllocationCountersToGSACounters(in map[string]*pb.AllocationResponse_CounterStatus) map[string]agonesv1.CounterStatus {
   462  	out := map[string]agonesv1.CounterStatus{}
   463  	for k, v := range in {
   464  		out[k] = agonesv1.CounterStatus{
   465  			Count:    v.Count.GetValue(),
   466  			Capacity: v.Capacity.GetValue(),
   467  		}
   468  	}
   469  	return out
   470  }
   471  
   472  func convertAllocationListsToGSALists(in map[string]*pb.AllocationResponse_ListStatus) map[string]agonesv1.ListStatus {
   473  	out := map[string]agonesv1.ListStatus{}
   474  	for k, v := range in {
   475  		out[k] = agonesv1.ListStatus{
   476  			Values:   v.Values,
   477  			Capacity: v.Capacity.GetValue(),
   478  		}
   479  	}
   480  	return out
   481  }
   482  
   483  // convertStateV1ToError converts GameServerAllocationState V1 (GSA) to AllocationResponse_GameServerAllocationState
   484  func convertStateV1ToError(in allocationv1.GameServerAllocationState, grpcUnallocatedStatusCode codes.Code) error {
   485  
   486  	switch in {
   487  	case allocationv1.GameServerAllocationAllocated:
   488  		return nil
   489  	case allocationv1.GameServerAllocationUnAllocated:
   490  		return status.Error(grpcUnallocatedStatusCode, "there is no available GameServer to allocate")
   491  	case allocationv1.GameServerAllocationContention:
   492  		return status.Error(codes.Aborted, "too many concurrent requests have overwhelmed the system")
   493  	}
   494  	return status.Error(codes.Unknown, "unknown issue")
   495  }
   496  
   497  // convertAllocationPrioritiesToGSAPriorities converts a list of AllocationRequest_Priorities to a
   498  // list of GameServerAllocationSpec (GSA.Spec) Priorities
   499  func convertAllocationPrioritiesToGSAPriorities(in []*pb.Priority) []agonesv1.Priority {
   500  	var out []agonesv1.Priority
   501  	for _, p := range in {
   502  		var t string
   503  		var o string
   504  		switch p.Type {
   505  		case pb.Priority_List:
   506  			t = agonesv1.GameServerPriorityList
   507  		default: // case pb.Priority_Counter and case nil
   508  			t = agonesv1.GameServerPriorityCounter
   509  		}
   510  		switch p.Order {
   511  		case pb.Priority_Descending:
   512  			o = agonesv1.GameServerPriorityDescending
   513  		default: // case pb.Priority_Ascending and case nil
   514  			o = agonesv1.GameServerPriorityAscending
   515  		}
   516  		priority := agonesv1.Priority{
   517  			Type:  t,
   518  			Key:   p.Key,
   519  			Order: o,
   520  		}
   521  		out = append(out, priority)
   522  	}
   523  	return out
   524  }
   525  
   526  // convertAllocationPrioritiesToGSAPriorities converts a list of GameServerAllocationSpec (GSA.Spec)
   527  // Priorities to a list of AllocationRequest_Priorities
   528  func convertGSAPrioritiesToAllocationPriorities(in []agonesv1.Priority) []*pb.Priority {
   529  	var out []*pb.Priority
   530  	for _, p := range in {
   531  		var pt pb.Priority_Type
   532  		var po pb.Priority_Order
   533  		switch p.Type {
   534  		case agonesv1.GameServerPriorityList:
   535  			pt = pb.Priority_List
   536  		default: // case agonesv1.GameServerPriorityCounter and case nil
   537  			pt = pb.Priority_Counter
   538  		}
   539  		switch p.Order {
   540  		case agonesv1.GameServerPriorityDescending:
   541  			po = pb.Priority_Descending
   542  		default: // case agonesv1.GameServerPriorityAscending and case nil
   543  			po = pb.Priority_Ascending
   544  		}
   545  		priority := pb.Priority{
   546  			Type:  pt,
   547  			Key:   p.Key,
   548  			Order: po,
   549  		}
   550  		out = append(out, &priority)
   551  	}
   552  	return out
   553  }
   554  
   555  // convertAllocationCountersToGSACounterActions converts a map of AllocationRequest_Counters to a
   556  // map of GameServerAllocationSpec CounterActions
   557  func convertAllocationCountersToGSACounterActions(in map[string]*pb.CounterAction) map[string]allocationv1.CounterAction {
   558  	out := map[string]allocationv1.CounterAction{}
   559  	for k, v := range in {
   560  		ca := allocationv1.CounterAction{}
   561  
   562  		if v.Action != nil {
   563  			action := v.Action.GetValue()
   564  			ca.Action = &action
   565  		}
   566  		if v.Amount != nil {
   567  			amount := v.Amount.GetValue()
   568  			ca.Amount = &amount
   569  		}
   570  		if v.Capacity != nil {
   571  			capacity := v.Capacity.GetValue()
   572  			ca.Capacity = &capacity
   573  		}
   574  
   575  		out[k] = ca
   576  	}
   577  	return out
   578  }
   579  
   580  // convertGSACounterActionsToAllocationCounters converts a map of GameServerAllocationSpec CounterActions
   581  // to a map of AllocationRequest_Counters
   582  func convertGSACounterActionsToAllocationCounters(in map[string]allocationv1.CounterAction) map[string]*pb.CounterAction {
   583  	out := map[string]*pb.CounterAction{}
   584  
   585  	for k, v := range in {
   586  		ca := pb.CounterAction{}
   587  
   588  		if v.Action != nil {
   589  			ca.Action = wrapperspb.String(*v.Action)
   590  		}
   591  		if v.Amount != nil {
   592  			ca.Amount = wrapperspb.Int64(*v.Amount)
   593  		}
   594  		if v.Capacity != nil {
   595  			ca.Capacity = wrapperspb.Int64(*v.Capacity)
   596  		}
   597  
   598  		out[k] = &ca
   599  	}
   600  	return out
   601  }
   602  
   603  // convertAllocationListsToGSAListActions converts a map of AllocationRequest_Lists to a
   604  // map of GameServerAllocationSpec ListActions
   605  func convertAllocationListsToGSAListActions(in map[string]*pb.ListAction) map[string]allocationv1.ListAction {
   606  	out := map[string]allocationv1.ListAction{}
   607  
   608  	for k, v := range in {
   609  		la := allocationv1.ListAction{}
   610  
   611  		if v.AddValues != nil {
   612  			addValues := v.GetAddValues()
   613  			copyValues := make([]string, len(addValues))
   614  			copy(copyValues, addValues)
   615  			la.AddValues = copyValues
   616  		}
   617  		if v.DeleteValues != nil {
   618  			deleteValues := v.GetDeleteValues()
   619  			copyValues := make([]string, len(deleteValues))
   620  			copy(copyValues, deleteValues)
   621  			la.DeleteValues = copyValues
   622  		}
   623  		if v.Capacity != nil {
   624  			capacity := v.Capacity.GetValue()
   625  			la.Capacity = &capacity
   626  		}
   627  
   628  		out[k] = la
   629  	}
   630  
   631  	return out
   632  }
   633  
   634  // convertGSAListActionsToAllocationLists converts a map of GameServerAllocationSpec ListActions
   635  // to a map of AllocationRequest_Lists
   636  func convertGSAListActionsToAllocationLists(in map[string]allocationv1.ListAction) map[string]*pb.ListAction {
   637  	out := map[string]*pb.ListAction{}
   638  
   639  	for k, v := range in {
   640  		la := pb.ListAction{}
   641  
   642  		if v.AddValues != nil {
   643  			copyValues := make([]string, len(v.AddValues))
   644  			copy(copyValues, v.AddValues)
   645  			la.AddValues = copyValues
   646  		}
   647  		if v.DeleteValues != nil {
   648  			copyValues := make([]string, len(v.DeleteValues))
   649  			copy(copyValues, v.DeleteValues)
   650  			la.DeleteValues = copyValues
   651  		}
   652  		if v.Capacity != nil {
   653  			la.Capacity = wrapperspb.Int64(*v.Capacity)
   654  		}
   655  
   656  		out[k] = &la
   657  	}
   658  	return out
   659  }