-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhealthz_handler.go
More file actions
74 lines (67 loc) · 2.14 KB
/
healthz_handler.go
File metadata and controls
74 lines (67 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package healthsrv
import (
"encoding/json"
"fmt"
"net/http"
"time"
s3 "github.com/aws/aws-sdk-go/service/s3"
"github.com/deis/builder/pkg/gitreceive/log"
"github.com/deis/builder/pkg/sshd"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
)
type healthZRespBucket struct {
Name string `json:"name"`
CreationDate time.Time `json:"creation_date"`
}
func convertBucket(b *s3.Bucket) healthZRespBucket {
return healthZRespBucket{
Name: *b.Name,
CreationDate: *b.CreationDate,
}
}
type healthZResp struct {
Namespaces []string `json:"k8s_namespaces"`
S3Buckets []healthZRespBucket `json:"s3_buckets"`
SSHServerStarted bool `json:"ssh_server_started"`
}
func healthZHandler(nsLister NamespaceLister, bLister BucketLister, serverCircuit *sshd.Circuit) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// there's a race between the boolean eval and the HTTP error returned, but k8s will repeat the health probe request
// and effectively re-evaluate the boolean.
if serverCircuit.State() != sshd.ClosedState {
str := fmt.Sprintf("SSH Server is not yet started")
log.Err(str)
http.Error(w, str, http.StatusAccepted)
return
}
lbOut, err := bLister.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
str := fmt.Sprintf("Error listing buckets (%s)", err)
log.Err(str)
http.Error(w, str, http.StatusInternalServerError)
return
}
var rsp healthZResp
for _, buck := range lbOut.Buckets {
rsp.S3Buckets = append(rsp.S3Buckets, convertBucket(buck))
}
nsList, err := nsLister.List(labels.Everything(), fields.Everything())
if err != nil {
str := fmt.Sprintf("Error listing buckets (%s)", err)
log.Err(str)
http.Error(w, str, http.StatusInternalServerError)
return
}
for _, ns := range nsList.Items {
rsp.Namespaces = append(rsp.Namespaces, ns.Name)
}
rsp.SSHServerStarted = true
if err := json.NewEncoder(w).Encode(rsp); err != nil {
str := fmt.Sprintf("Error encoding JSON (%s)", err)
http.Error(w, str, http.StatusInternalServerError)
return
}
// TODO: check server is running
})
}