Skip to content

Commit 64cde22

Browse files
author
Aaron Schlesinger
committed
fix(pkg/healthz): add readiness & liveness probes
1 parent f669c77 commit 64cde22

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

pkg/healthsrv/healthz_handler.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package healthsrv
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
s3 "github.com/aws/aws-sdk-go/service/s3"
10+
)
11+
12+
type healthZRespBucket struct {
13+
Name string `json:"name"`
14+
CreationDate time.Time `json:"creation_date"`
15+
}
16+
17+
func convertBucket(b *s3.Bucket) healthZRespBucket {
18+
return healthZRespBucket{
19+
Name: *b.Name,
20+
CreationDate: *b.CreationDate,
21+
}
22+
}
23+
24+
type healthZResp struct {
25+
Buckets []healthZRespBucket `json:"buckets"`
26+
}
27+
28+
func healthZHandler(s3Client *s3.S3) http.Handler {
29+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
30+
lbOut, err := s3Client.ListBuckets(s3.ListBucketsInput{})
31+
if err != nil {
32+
str := fmt.Sprintf("Error listing buckets (%s)", err)
33+
log.Printf(str)
34+
http.Error(w, str, http.StatusInternalServerError)
35+
return
36+
}
37+
var rsp healthZResp
38+
for _, buck := range lbOut.Buckets {
39+
rsp.Buckets = append(rsp.Buckets, convertBucket(buck))
40+
}
41+
if err := json.NewEncoder(w).Encode(rsp); err != nil {
42+
str := fmt.Sprintf("Error encoding JSON (%s)", err)
43+
http.Error(w, str, http.StatusInternalServerError)
44+
return
45+
}
46+
// TODO: check k8s API
47+
// TODO: check server is running
48+
})
49+
}

pkg/healthsrv/server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package healthsrv
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
s3 "github.com/aws/aws-sdk-go/service/s3"
8+
)
9+
10+
const (
11+
DefaultHost = "0.0.0.0"
12+
DefaultPort = 8082
13+
)
14+
15+
// Start starts the healthcheck server on $host:$port and blocks. It only returns if the server fails, with the indicative error
16+
func Start(host string, port int, s3Client *s3.S3) error {
17+
mux := http.NewServeMux()
18+
mux.Handle("/healthz", healthZHandler(s3Client))
19+
20+
hostStr := fmt.Sprintf("%s:%d", host, port)
21+
return http.ListenAndServe(hostStr, mux)
22+
}

0 commit comments

Comments
 (0)