-
Notifications
You must be signed in to change notification settings - Fork 813
/
gameservers.go
104 lines (87 loc) · 3.74 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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 network address that the given Pod is run on.
// This will default to the ExternalDNS, but if the ExternalDNS is
// not set, it will fall back to the ExternalIP then InternalDNS then InternalIP,
// If externalDNS is false, skip ExternalDNS and InternalDNS.
// 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.NodeExternalDNS {
return a.Address, nil
}
}
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 DNS/IP, so fall back to the private DNS/IP
for _, a := range node.Status.Addresses {
if a.Type == corev1.NodeInternalDNS {
return a.Address, nil
}
}
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, syncPodPortsToGameServer func(*agonesv1.GameServer, *corev1.Pod) error) (*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
if err := syncPodPortsToGameServer(gs, pod); err != nil {
return gs, errors.Wrapf(err, "cloud product error syncing ports on GameServer %s", gs.ObjectMeta.Name)
}
// 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
}