Skip to content

Commit

Permalink
Merge bfcb5b7 into 80b4ff1
Browse files Browse the repository at this point in the history
  • Loading branch information
maruina committed Dec 21, 2019
2 parents 80b4ff1 + bfcb5b7 commit c20acc6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

## Usage

`go-infrabin` exposes two ports:

* `8888` as a service port
* `8899` as the admin port, for livensss and readiness probes

To override the default values:

* _INFRABIN_MAX_DELAY_ to change the maximum value for the `/delay` endpoint. Default to 120.

## Endpoints
## Service Endpoints

* `GET /`
* _returns_: a JSON response
Expand All @@ -38,7 +43,9 @@ To override the default values:
}
```

* `GET /healthcheck/liveness`
## Admin Endpoints

* `GET /liveness`
* _returns_: a JSON response if healthy or the status code `503` if unhealthy.

```json
Expand Down
47 changes: 36 additions & 11 deletions cmd/go-infrabin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ func RootHandler(w http.ResponseWriter, r *http.Request) {
}
}

// LivenessHandler handles the "/healthcheck/liveness" endpoint
// LivenessHandler handles the "/liveness" endpoint
func LivenessHandler(w http.ResponseWriter, r *http.Request) {
var resp helpers.Response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
resp.ProbeResponse = &helpers.ProbeResponse{
Liveness: "pass",
}

resp.Liveness = "pass"
data := helpers.MarshalResponseToString(resp)
_, err := io.WriteString(w, data)
if err != nil {
Expand All @@ -56,19 +53,26 @@ func LivenessHandler(w http.ResponseWriter, r *http.Request) {
// DelayHandler handles the "/delay" endpoint
func DelayHandler(w http.ResponseWriter, r *http.Request) {
var resp helpers.Response
var val string
var seconds int
vars := mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
val, ok := vars["seconds"]
if !ok {
seconds = 0
}

seconds, err := strconv.Atoi(val)

seconds, err := strconv.Atoi(vars["seconds"])
if err != nil {
log.Printf("cannot convert vars['seconds'] to integer: %v", err)
resp.Error = "cannot convert seconds to integer"
data := helpers.MarshalResponseToString(resp)
w.WriteHeader(http.StatusBadRequest)
_, err = io.WriteString(w, data)
if err != nil {
log.Fatal("error writing to ResponseWriter", err)
}
log.Printf("cannot convert vars['seconds'] to integer: %v", err)
} else {
maxDelay, err := strconv.Atoi(helpers.GetEnv("INFRABIN_MAX_DELAY", "120"))
if err != nil {
Expand All @@ -89,18 +93,39 @@ func DelayHandler(w http.ResponseWriter, r *http.Request) {

func main() {
r := mux.NewRouter()
a := mux.NewRouter()
finish := make(chan bool)

r.HandleFunc("/", RootHandler)
r.HandleFunc("/delay/{seconds}", DelayHandler)
r.HandleFunc("/healthcheck/liveness", LivenessHandler)

srv := &http.Server{
a.HandleFunc("/liveness", LivenessHandler)

serviceSrv := &http.Server{
Handler: r,
Addr: "0.0.0.0:8080",
Addr: "0.0.0.0:8888",
// Good practice: enforce timeouts
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
adminSrv := &http.Server{
Handler: a,
Addr: "0.0.0.0:8899",
// Good practice: enforce timeouts
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}

log.Print("starting go-infrabin")
log.Fatal(srv.ListenAndServe())

go func() {
log.Print("Listening on service port...")
log.Fatal(serviceSrv.ListenAndServe())
}()

go func() {
log.Print("Listening on admin port...")
log.Fatal(adminSrv.ListenAndServe())
}()
<-finish
}
4 changes: 1 addition & 3 deletions cmd/go-infrabin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ func TestLivenessHandler(t *testing.T) {
}

var expected helpers.Response
expected.ProbeResponse = &helpers.ProbeResponse{
Liveness: "pass",
}
expected.Liveness = "pass"
data := helpers.MarshalResponseToString(expected)

if rr.Body.String() != data {
Expand Down
Binary file added go-infrabin
Binary file not shown.
16 changes: 5 additions & 11 deletions internal/helpers/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

// Response creates the go-infrabin main response
type Response struct {
Hostname string `json:"hostname,omitempty"`
KubeResponse *KubeResponse `json:"kubernetes,omitempty"`
ProbeResponse *ProbeResponse `json:"probes,omitempty"`
Delay string `json:"delay,omitempty"`
Error string `json:"error,omitempty"`
Hostname string `json:"hostname,omitempty"`
KubeResponse *KubeResponse `json:"kubernetes,omitempty"`
Liveness string `json:"liveness,omitempty"`
Delay string `json:"delay,omitempty"`
Error string `json:"error,omitempty"`
}

// KubeResponse creates the response if running on Kubernetes
Expand All @@ -22,12 +22,6 @@ type KubeResponse struct {
NodeName string `json:"node_name,omitempty"`
}

// ProbeResponse creates the liveness and reasiness probes response
type ProbeResponse struct {
Liveness string `json:"liveness,omitempty"`
Readiness string `json:"readiness,omitempty"`
}

// MarshalResponseToString marhal a Response struct into a json and return it as string
func MarshalResponseToString(r Response) string {
data, err := json.Marshal(r)
Expand Down

0 comments on commit c20acc6

Please sign in to comment.