forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
177 lines (153 loc) · 6.43 KB
/
health.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2018 Google Inc. 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 (
"strings"
"agones.dev/agones/pkg/apis/stable"
"agones.dev/agones/pkg/apis/stable/v1alpha1"
"agones.dev/agones/pkg/client/clientset/versioned"
getterv1alpha1 "agones.dev/agones/pkg/client/clientset/versioned/typed/stable/v1alpha1"
"agones.dev/agones/pkg/client/informers/externalversions"
listerv1alpha1 "agones.dev/agones/pkg/client/listers/stable/v1alpha1"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/workerqueue"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
corelisterv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
)
// HealthController watches Pods, and applies
// an Unhealthy state if certain pods crash, or can't be assigned a port, and other
// similar type conditions.
type HealthController struct {
logger *logrus.Entry
podSynced cache.InformerSynced
podLister corelisterv1.PodLister
gameServerGetter getterv1alpha1.GameServersGetter
gameServerLister listerv1alpha1.GameServerLister
workerqueue *workerqueue.WorkerQueue
recorder record.EventRecorder
}
// NewHealthController returns a HealthController
func NewHealthController(kubeClient kubernetes.Interface, agonesClient versioned.Interface, kubeInformerFactory informers.SharedInformerFactory,
agonesInformerFactory externalversions.SharedInformerFactory) *HealthController {
podInformer := kubeInformerFactory.Core().V1().Pods().Informer()
hc := &HealthController{
podSynced: podInformer.HasSynced,
podLister: kubeInformerFactory.Core().V1().Pods().Lister(),
gameServerGetter: agonesClient.StableV1alpha1(),
gameServerLister: agonesInformerFactory.Stable().V1alpha1().GameServers().Lister(),
}
hc.logger = runtime.NewLoggerWithType(hc)
hc.workerqueue = workerqueue.NewWorkerQueue(hc.syncGameServer, hc.logger, stable.GroupName+".HealthController")
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(hc.logger.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
hc.recorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "health-controller"})
podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, newObj interface{}) {
pod := newObj.(*corev1.Pod)
if owner := metav1.GetControllerOf(pod); owner != nil && owner.Kind == "GameServer" {
if v1alpha1.GameServerRolePodSelector.Matches(labels.Set(pod.Labels)) && hc.isUnhealthy(pod) {
key := pod.ObjectMeta.Namespace + "/" + owner.Name
hc.workerqueue.Enqueue(cache.ExplicitKey(key))
}
}
},
})
return hc
}
// isUnhealthy returns if the Pod event is going
// to cause the GameServer to become Unhealthy
func (hc *HealthController) isUnhealthy(pod *corev1.Pod) bool {
return hc.unschedulableWithNoFreePorts(pod) || hc.failedContainer(pod)
}
// unschedulableWithNoFreePorts checks if the reason the Pod couldn't be scheduled
// was because there weren't any free ports in the range specified
func (hc *HealthController) unschedulableWithNoFreePorts(pod *corev1.Pod) bool {
for _, cond := range pod.Status.Conditions {
if cond.Type == corev1.PodScheduled && cond.Reason == corev1.PodReasonUnschedulable {
if strings.Contains(cond.Message, "free ports") {
return true
}
}
}
return false
}
// failedContainer checks each container, and determines if there was a failed
// container
func (hc *HealthController) failedContainer(pod *corev1.Pod) bool {
container := pod.Annotations[v1alpha1.GameServerContainerAnnotation]
for _, cs := range pod.Status.ContainerStatuses {
if cs.Name == container && cs.State.Terminated != nil {
return true
}
}
return false
}
// Run processes the rate limited queue.
// Will block until stop is closed
func (hc *HealthController) Run(stop <-chan struct{}) {
hc.workerqueue.Run(1, stop)
}
// syncGameServer sets the GameSerer to Unhealthy, if its state is Ready
func (hc *HealthController) syncGameServer(key string) error {
hc.logger.WithField("key", key).Info("Synchronising")
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
// don't return an error, as we don't want this retried
runtime.HandleError(hc.logger.WithField("key", key), errors.Wrapf(err, "invalid resource key"))
return nil
}
gs, err := hc.gameServerLister.GameServers(namespace).Get(name)
if err != nil {
if k8serrors.IsNotFound(err) {
hc.logger.WithField("key", key).Info("GameServer is no longer available for syncing")
return nil
}
return errors.Wrapf(err, "error retrieving GameServer %s from namespace %s", name, namespace)
}
var reason string
unhealthy := false
switch gs.Status.State {
case v1alpha1.Starting:
hc.logger.WithField("key", key).Info("GameServer cannot start on this port")
unhealthy = true
reason = "No nodes have free ports for the allocated ports"
case v1alpha1.Ready:
hc.logger.WithField("key", key).Info("GameServer container has terminated")
unhealthy = true
reason = "GameServer container terminated"
}
if unhealthy {
hc.logger.WithField("gs", gs).Infof("Marking GameServer as Unhealthy")
gsCopy := gs.DeepCopy()
gsCopy.Status.State = v1alpha1.Unhealthy
if _, err := hc.gameServerGetter.GameServers(gs.ObjectMeta.Namespace).Update(gsCopy); err != nil {
return errors.Wrapf(err, "error updating GameServer %s to unhealthy", gs.ObjectMeta.Name)
}
hc.recorder.Event(gs, corev1.EventTypeWarning, string(gsCopy.Status.State), reason)
}
return nil
}