Skip to content

Commit

Permalink
Use github.com/heptio/healthcheck
Browse files Browse the repository at this point in the history
Rather than a handrolled solution, this commit uses heptio's healthcheck
library. Future consumers of the health check handler need only to
register liveness or readiness probes.
  • Loading branch information
enocom committed Feb 20, 2018
1 parent f6ec17a commit cfd7d6c
Show file tree
Hide file tree
Showing 320 changed files with 48,156 additions and 56 deletions.
53 changes: 52 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@

[[constraint]]
name = "google.golang.org/grpc"
version = "1.8.0"
version = "1.8.0"
[[constraint]]
branch = "master"
name = "github.com/heptiolabs/healthcheck"
26 changes: 25 additions & 1 deletion cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package main

import (
"net/http"
"strings"
"time"

Expand All @@ -25,6 +26,8 @@ import (
"agones.dev/agones/pkg/gameservers"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
"github.com/heptiolabs/healthcheck"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -100,19 +103,40 @@ func main() {
logrus.WithError(err).Fatal("Could not create the agones api clientset")
}

health := healthcheck.NewHandler()
agonesInformerFactory := externalversions.NewSharedInformerFactory(agonesClient, 30*time.Second)
kubeInformationFactory := informers.NewSharedInformerFactory(kubeClient, 30*time.Second)
c := gameservers.NewController(minPort, maxPort, sidecarImage, alwaysPullSidecar, kubeClient, kubeInformationFactory, extClient, agonesClient, agonesInformerFactory)
c := gameservers.NewController(health, minPort, maxPort, sidecarImage, alwaysPullSidecar, kubeClient, kubeInformationFactory, extClient, agonesClient, agonesInformerFactory)

stop := signals.NewStopChannel()

kubeInformationFactory.Start(stop)
agonesInformerFactory.Start(stop)

go startHealthCheck(health)

err = c.Run(2, stop)
if err != nil {
logrus.WithError(err).Fatal("Could not run gameserver controller")
}

logrus.Info("Shut down gameserver controller")
}

func startHealthCheck(h healthcheck.Handler) {
logrus.Info("Starting health check...")
srv := &http.Server{
Addr: ":8080",
Handler: h,
}
defer srv.Close() // nolint: errcheck

if err := srv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
logrus.WithError(err).Info("health check: http server closed")
} else {
err := errors.Wrap(err, "Could not listen on :8080")
runtime.HandleError(logrus.WithError(err), err)
}
}
}
34 changes: 4 additions & 30 deletions pkg/gameservers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package gameservers

import (
"fmt"
"net/http"
"time"

"agones.dev/agones/pkg/apis/stable"
Expand All @@ -26,6 +25,7 @@ import (
"agones.dev/agones/pkg/client/informers/externalversions"
listerv1alpha1 "agones.dev/agones/pkg/client/listers/stable/v1alpha1"
"agones.dev/agones/pkg/util/runtime"
"github.com/heptiolabs/healthcheck"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -65,14 +65,15 @@ type Controller struct {
queue workqueue.RateLimitingInterface
portAllocator *PortAllocator
healthController *HealthController
server *http.Server
recorder record.EventRecorder
// this allows for overwriting for testing purposes
syncHandler func(string) error
}

// NewController returns a new gameserver crd controller
func NewController(minPort, maxPort int32,
func NewController(
health healthcheck.Handler,
minPort, maxPort int32,
sidecarImage string,
alwaysPullSidecarImage bool,
kubeClient kubernetes.Interface,
Expand Down Expand Up @@ -131,20 +132,6 @@ func NewController(minPort, maxPort int32,

c.syncHandler = c.syncGameServer

mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("ok"))
if err != nil {
logrus.WithError(err).Error("could not send ok response on healthz")
w.WriteHeader(http.StatusInternalServerError)
}
})

c.server = &http.Server{
Addr: ":8080",
Handler: mux,
}

return c
}

Expand All @@ -153,19 +140,6 @@ func NewController(minPort, maxPort int32,
func (c Controller) Run(threadiness int, stop <-chan struct{}) error {
defer c.queue.ShutDown()

logrus.Info("Starting health check...")
go func() {
if err := c.server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
logrus.WithError(err).Info("health check: http server closed")
} else {
err := errors.Wrap(err, "Could not listen on :8080")
runtime.HandleError(logrus.WithError(err), err)
}
}
}()
defer c.server.Close() // nolint: errcheck

err := c.waitForEstablishedCRD()
if err != nil {
return err
Expand Down
25 changes: 2 additions & 23 deletions pkg/gameservers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package gameservers

import (
"fmt"
"net/http"
"sync"
"testing"
"time"

"agones.dev/agones/pkg/apis/stable"
"agones.dev/agones/pkg/apis/stable/v1alpha1"
"github.com/heptiolabs/healthcheck"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -208,27 +208,6 @@ func TestWatchGameServers(t *testing.T) {
assert.Equal(t, "default/test", <-received)
}

func TestHealthCheck(t *testing.T) {
c, mocks := newFakeController()
mocks.extClient.AddReactor("get", "customresourcedefinitions", func(action k8stesting.Action) (bool, runtime.Object, error) {
return true, newEstablishedCRD(), nil
})

c.syncHandler = func(name string) error {
return nil
}

stop, cancel := startInformers(mocks, c.gameServerSynced)
defer cancel()

go func() {
err := c.Run(1, stop)
assert.Nil(t, err, "Run should not error")
}()

testHTTPHealth(t, "http://localhost:8080/healthz", "ok", http.StatusOK)
}

func TestSyncGameServerDeletionTimestamp(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -778,7 +757,7 @@ func testWithNonZeroDeletionTimestamp(t *testing.T, state v1alpha1.State, f func
// newFakeController returns a controller, backed by the fake Clientset
func newFakeController() (*Controller, mocks) {
m := newMocks()
c := NewController(10, 20, "sidecar:dev", false,
c := NewController(healthcheck.NewHandler(), 10, 20, "sidecar:dev", false,
m.kubeClient, m.kubeInformationFactory, m.extClient, m.agonesClient, m.agonesInformerFactory)
c.recorder = m.fakeRecorder
return c, m
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/beorn7/perks/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/beorn7/perks/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/beorn7/perks/histogram/bench_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cfd7d6c

Please sign in to comment.