-
Notifications
You must be signed in to change notification settings - Fork 820
/
gameservers.go
85 lines (73 loc) · 3.16 KB
/
gameservers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gameservers
import (
"net"
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
// isGameServerPod returns if this Pod is a Pod that comes from a GameServer
func isGameServerPod(pod *corev1.Pod) bool {
if agonesv1.GameServerRolePodSelector.Matches(labels.Set(pod.ObjectMeta.Labels)) {
owner := metav1.GetControllerOf(pod)
return owner != nil && owner.Kind == "GameServer"
}
return false
}
// address returns the IP that the given Pod is run on.
// This will default to the ExternalIP, but if the ExternalIP is
// not set, it will fall back to the InternalIP,
// since we can have clusters that are private, and/or tools like minikube
// that only report an InternalIP.
func address(node *corev1.Node) (string, error) {
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeExternalIP && net.ParseIP(a.Address) != nil {
return a.Address, nil
}
}
// There might not be a public IP, so fall back to the private IP
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeInternalIP && net.ParseIP(a.Address) != nil {
return a.Address, nil
}
}
return "", errors.Errorf("Could not find an address for Node: %s", node.ObjectMeta.Name)
}
// applyGameServerAddressAndPort gathers the address and port details from the node and pod
// and applies them to the GameServer that is passed in, and returns it.
func applyGameServerAddressAndPort(gs *agonesv1.GameServer, node *corev1.Node, pod *corev1.Pod) (*agonesv1.GameServer, error) {
addr, err := address(node)
if err != nil {
return gs, errors.Wrapf(err, "error getting external address for GameServer %s", gs.ObjectMeta.Name)
}
gs.Status.Address = addr
gs.Status.NodeName = pod.Spec.NodeName
// HostPort is always going to be populated, even when dynamic
// This will be a double up of information, but it will be easier to read
gs.Status.Ports = make([]agonesv1.GameServerStatusPort, len(gs.Spec.Ports))
for i, p := range gs.Spec.Ports {
gs.Status.Ports[i] = p.Status()
}
return gs, nil
}
// isBeforePodCreated checks to see if the GameServer is in a state in which the pod could not have been
// created yet. This includes "Starting" in which a pod MAY exist, but may not yet be available, depending on when the
// informer cache updates
func isBeforePodCreated(gs *agonesv1.GameServer) bool {
state := gs.Status.State
return state == agonesv1.GameServerStatePortAllocation || state == agonesv1.GameServerStateCreating || state == agonesv1.GameServerStateStarting
}