agones.dev/agones@v1.54.0/pkg/sdkserver/sdk.go (about) 1 // Copyright 2018 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 sdkserver 16 17 import ( 18 agonesv1 "agones.dev/agones/pkg/apis/agones/v1" 19 "agones.dev/agones/pkg/sdk" 20 "agones.dev/agones/pkg/util/runtime" 21 "google.golang.org/protobuf/proto" 22 ) 23 24 const ( 25 // metadataPrefix prefix for labels and annotations 26 metadataPrefix = "agones.dev/sdk-" 27 ) 28 29 // convert converts a K8s GameServer object, into a gRPC SDK GameServer object 30 func convert(gs *agonesv1.GameServer) *sdk.GameServer { 31 meta := gs.ObjectMeta 32 status := gs.Status 33 health := gs.Spec.Health 34 result := &sdk.GameServer{ 35 ObjectMeta: &sdk.GameServer_ObjectMeta{ 36 Name: meta.Name, 37 Namespace: meta.Namespace, 38 Uid: string(meta.UID), 39 ResourceVersion: meta.ResourceVersion, 40 Generation: meta.Generation, 41 CreationTimestamp: meta.CreationTimestamp.Unix(), 42 Annotations: meta.Annotations, 43 Labels: meta.Labels, 44 }, 45 Spec: &sdk.GameServer_Spec{ 46 Health: &sdk.GameServer_Spec_Health{ 47 Disabled: health.Disabled, 48 PeriodSeconds: health.PeriodSeconds, 49 FailureThreshold: health.FailureThreshold, 50 InitialDelaySeconds: health.InitialDelaySeconds, 51 }, 52 }, 53 Status: &sdk.GameServer_Status{ 54 State: string(status.State), 55 Address: status.Address, 56 }, 57 } 58 if meta.DeletionTimestamp != nil { 59 result.ObjectMeta.DeletionTimestamp = meta.DeletionTimestamp.Unix() 60 } 61 62 // look around and add all the addresses 63 for _, a := range status.Addresses { 64 result.Status.Addresses = append(result.Status.Addresses, &sdk.GameServer_Status_Address{ 65 Type: string(a.Type), 66 Address: a.Address, 67 }) 68 } 69 // loop around and add all the ports 70 for _, p := range status.Ports { 71 grpcPort := &sdk.GameServer_Status_Port{ 72 Name: p.Name, 73 Port: p.Port, 74 } 75 result.Status.Ports = append(result.Status.Ports, grpcPort) 76 } 77 78 if runtime.FeatureEnabled(runtime.FeaturePlayerTracking) { 79 if gs.Status.Players != nil { 80 result.Status.Players = &sdk.GameServer_Status_PlayerStatus{ 81 Count: gs.Status.Players.Count, 82 Capacity: gs.Status.Players.Capacity, 83 Ids: gs.Status.Players.IDs, 84 } 85 } 86 } 87 88 if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) { 89 if gs.Status.Counters != nil { 90 counters := make(map[string]*sdk.GameServer_Status_CounterStatus, len(gs.Status.Counters)) 91 for key, counter := range gs.Status.Counters { 92 counters[key] = &sdk.GameServer_Status_CounterStatus{Count: *proto.Int64(counter.Count), Capacity: *proto.Int64(counter.Capacity)} 93 } 94 result.Status.Counters = counters 95 } 96 97 if gs.Status.Lists != nil { 98 lists := make(map[string]*sdk.GameServer_Status_ListStatus, len(gs.Status.Lists)) 99 for key, list := range gs.Status.Lists { 100 lists[key] = &sdk.GameServer_Status_ListStatus{Capacity: *proto.Int64(list.Capacity), Values: list.DeepCopy().Values} 101 } 102 result.Status.Lists = lists 103 } 104 } 105 106 return result 107 }