Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Adds healthz endpoint
Browse files Browse the repository at this point in the history
1) count errors
2) healthz will response with http 500 error count is not 0
  • Loading branch information
Roman Sokolkov committed Dec 11, 2017
1 parent f576ec4 commit ff1b25a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package main
import (
"flag"
"fmt"
"net/http"
"os"
"runtime"
"sync/atomic"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -59,6 +61,8 @@ var (
source = "https://github.com/giantswarm/calico-node-controller"
)

var errorCount uint64

type controller struct {
kubeClient clientset.Interface
cloud cloudprovider.Interface
Expand Down Expand Up @@ -118,10 +122,39 @@ func main() {
defer close(stop)
go controller.Run(stop)

// Start healthz endpoint.
go func() {
http.HandleFunc("/healthz", healthzHandler)

err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(fmt.Sprintf("failed to setup healthz endpoint: %v", err))
}
}()

// Wait forever.
select {}
}

func runHealthz(stopCh chan struct{}) {

<-stopCh
}

// Increments error count by 1.
func (c *controller) countErr() {
atomic.AddUint64(&errorCount, 1)
}

// Return http 500 if error counter value is not 0.
func healthzHandler(w http.ResponseWriter, r *http.Request) {
if atomic.LoadUint64(&errorCount) != 0 {
w.WriteHeader(http.StatusInternalServerError)
}

w.WriteHeader(http.StatusOK)
}

// Creates new instance of controller.
func newController(
kubeClient clientset.Interface,
Expand Down Expand Up @@ -162,13 +195,15 @@ func (c *controller) MonitorNode() {
instances, ok := c.cloud.Instances()
if !ok {
utilruntime.HandleError(fmt.Errorf("failed to get instances from cloud provider"))
c.countErr()
return
}

// Get nodes known by kubernetes cluster.
nodes, err := c.kubeClient.CoreV1().Nodes().List(metav1.ListOptions{ResourceVersion: "0"})
if err != nil {
c.logger.Log("error", "error monitoring node status", "trace", microerror.Mask(err))
c.countErr()
return
}

Expand Down Expand Up @@ -214,6 +249,7 @@ func (c *controller) MonitorNode() {
"node", node.Name,
"trace", microerror.Mask(err),
)
c.countErr()
continue
}

Expand All @@ -235,6 +271,7 @@ func (c *controller) MonitorNode() {
"node", node.Name,
"trace", microerror.Mask(err),
)
c.countErr()
}
}(node.Name)

Expand Down

0 comments on commit ff1b25a

Please sign in to comment.