Skip to content

Commit dd27d28

Browse files
authored
Merge pull request agones-dev#34 from googleprivate/health
Add health check.
2 parents d37832e + a3749a5 commit dd27d28

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

build/install.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ spec:
5151
value: "true"
5252
- name: SIDECAR # overwrite the GameServer sidecar image that is used
5353
value: ${REGISTRY}/gameservers-sidecar:${VERSION}
54+
livenessProbe:
55+
httpGet:
56+
path: /healthz
57+
port: 8080
58+
initialDelaySeconds: 3
59+
periodSeconds: 3

gameservers/controller/controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package main
1717
import (
1818
"time"
1919

20+
"net/http"
21+
2022
"github.com/agonio/agon/pkg/apis/stable"
2123
stablev1alpha1 "github.com/agonio/agon/pkg/apis/stable/v1alpha1"
2224
"github.com/agonio/agon/pkg/client/clientset/versioned"
@@ -58,6 +60,7 @@ type Controller struct {
5860
gameServerSynced cache.InformerSynced
5961
nodeLister corelisterv1.NodeLister
6062
queue workqueue.RateLimitingInterface
63+
server *http.Server
6164

6265
// this allows for overwriting for testing purposes
6366
syncHandler func(string) error
@@ -102,6 +105,16 @@ func NewController(sidecarImage string,
102105

103106
c.syncHandler = c.syncGameServer
104107

108+
mux := http.NewServeMux()
109+
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
110+
w.Write([]byte("ok"))
111+
})
112+
113+
c.server = &http.Server{
114+
Addr: ":8080",
115+
Handler: mux,
116+
}
117+
105118
return c
106119
}
107120

@@ -110,6 +123,14 @@ func NewController(sidecarImage string,
110123
func (c Controller) Run(threadiness int, stop <-chan struct{}) error {
111124
defer c.queue.ShutDown()
112125

126+
logrus.Info("Starting health check...")
127+
go func() {
128+
if err := c.server.ListenAndServe(); err != nil {
129+
logrus.WithError(err).Error("Could not listen on :8080")
130+
}
131+
}()
132+
defer c.server.Close()
133+
113134
err := c.waitForEstablishedCRD()
114135
if err != nil {
115136
return err

gameservers/controller/controller_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121

2222
"fmt"
2323

24+
"io/ioutil"
25+
"net/http"
26+
2427
"github.com/agonio/agon/pkg/apis/stable"
2528
"github.com/agonio/agon/pkg/apis/stable/v1alpha1"
2629
agonfake "github.com/agonio/agon/pkg/client/clientset/versioned/fake"
@@ -158,8 +161,6 @@ func TestSyncGameServer(t *testing.T) {
158161
}
159162

160163
func TestWatchGameServers(t *testing.T) {
161-
t.Parallel()
162-
163164
c, mocks := newFakeController()
164165
fixture := v1alpha1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}}
165166
fakeWatch := watch.NewFake()
@@ -198,6 +199,32 @@ func TestWatchGameServers(t *testing.T) {
198199
assert.Equal(t, "default/test", <-received)
199200
}
200201

202+
func TestHealthCheck(t *testing.T) {
203+
c, mocks := newFakeController()
204+
mocks.extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
205+
return true, newEstablishedCRD(), nil
206+
})
207+
208+
c.syncHandler = func(name string) error {
209+
return nil
210+
}
211+
212+
stop := startInformers(c, mocks)
213+
defer close(stop)
214+
215+
go func() {
216+
err := c.Run(1, stop)
217+
assert.Nil(t, err, "Run should not error")
218+
}()
219+
220+
resp, err := http.Get("http://localhost:8080/healthz")
221+
assert.Nil(t, err, "health check error should be nil")
222+
defer resp.Body.Close()
223+
body, err := ioutil.ReadAll(resp.Body)
224+
assert.Nil(t, err, "read response error should be nil")
225+
assert.Equal(t, []byte("ok"), body, "response body should be 'ok'")
226+
}
227+
201228
func TestSyncGameServerBlankState(t *testing.T) {
202229

203230
t.Run("GameServer with a blank initial state", func(t *testing.T) {

install.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ spec:
4141
stable.agon.io/role: controller
4242
spec:
4343
containers:
44-
- nameW: gameservers-controller
44+
- name: gameservers-controller
4545
image: gcr.io/agon-images/gameservers-controller:0.1
4646
env:
4747
# - name: SIDECAR # overwrite the GameServer sidecar image that is used
4848
# value: gcr.io/agon-images/gameservers-sidecar:0.1
49+
livenessProbe:
50+
httpGet:
51+
path: /healthz
52+
port: 8080
53+
initialDelaySeconds: 3
54+
periodSeconds: 3

0 commit comments

Comments
 (0)