Skip to content

Commit

Permalink
Merge pull request #28 from jelmersnoeck/add-healthz-check
Browse files Browse the repository at this point in the history
Add health endpoint.
  • Loading branch information
jelmersnoeck authored Sep 13, 2018
2 parents f4ed62b + e8fa9cb commit 10ec760
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/kube/with-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ spec:
imagePullPolicy: IfNotPresent
args:
- operator
livenessProbe:
httpGet:
path: /_healthz
port: 9090
scheme: HTTP
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 50
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
httpGet:
path: /_healthz
port: 9090
scheme: HTTP
failureThreshold: 3
initialDelaySeconds: 5
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 1
resources:
requests:
cpu: 1m
Expand Down
25 changes: 25 additions & 0 deletions internal/httpsvc/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package httpsvc

import (
"fmt"
"net/http"
)

// Health represents a Health server which can be used to expose a health check.
type Health struct {
Server
}

// Start starts the Health Server.
func (s *Health) Start(stopCh <-chan struct{}) error {
registerHealthCheck(&s.ServeMux)

return s.Server.Start(stopCh)
}

func registerHealthCheck(mux *http.ServeMux) {
mux.HandleFunc("/_healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
}
38 changes: 38 additions & 0 deletions internal/httpsvc/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package httpsvc

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

// Server is a wrapper around a http.ServeMux which sets up necessary endpoints
// to run our operator.
type Server struct {
http.ServeMux

Addr string
Port int
}

// Start starts the HTTP Server.
func (s *Server) Start(stopCh <-chan struct{}) error {
srv := http.Server{
Addr: fmt.Sprintf("%s:%d", s.Addr, s.Port),
Handler: &s.ServeMux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

go func() {
<-stopCh

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
srv.Shutdown(ctx)
}()

return srv.ListenAndServe()
}
17 changes: 17 additions & 0 deletions internal/ingressmonitor/cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"time"

"github.com/jelmersnoeck/ingress-monitor/internal/httpsvc"
"github.com/jelmersnoeck/ingress-monitor/internal/ingressmonitor"
"github.com/jelmersnoeck/ingress-monitor/internal/provider"
"github.com/jelmersnoeck/ingress-monitor/internal/provider/logger"
Expand All @@ -22,6 +23,9 @@ var operatorFlags struct {
MasterURL string
KubeConfig string
ResyncPeriod string

HTTPAddr string
HTTPPort int
}

// operatorCmd represents the operator command
Expand Down Expand Up @@ -54,10 +58,20 @@ func runOperator(cmd *cobra.Command, args []string) {
log.Fatalf("Error building IngressMonitor clientset: %s", err)
}

// register the available providers
fact := provider.NewFactory(kubeClient)
statuscake.Register(fact)
logger.Register(fact)

// register the health server
healthSrv := &httpsvc.Health{
Server: httpsvc.Server{
Addr: operatorFlags.HTTPAddr,
Port: operatorFlags.HTTPPort,
},
}
go healthSrv.Start(stopCh)

op, err := ingressmonitor.NewOperator(
kubeClient, imClient, operatorFlags.Namespace,
resync, fact,
Expand All @@ -78,4 +92,7 @@ func init() {
operatorCmd.PersistentFlags().StringVar(&operatorFlags.MasterURL, "master-url", "", "The URL of the master API.")
operatorCmd.PersistentFlags().StringVar(&operatorFlags.KubeConfig, "kubeconfig", "", "Kubeconfig which should be used to talk to the API.")
operatorCmd.PersistentFlags().StringVar(&operatorFlags.ResyncPeriod, "resync-period", "30s", "Resyncing period to ensure all monitors are up to date.")

operatorCmd.PersistentFlags().StringVar(&operatorFlags.HTTPAddr, "http-addr", "0.0.0.0", "address the health server will bind to")
operatorCmd.PersistentFlags().IntVar(&operatorFlags.HTTPPort, "http-port", 9090, "port on which the health server is available")
}

0 comments on commit 10ec760

Please sign in to comment.