Skip to content

Commit 96ec946

Browse files
author
Aaron Schlesinger
committed
fix(*): don't report the server up until the SSH server is running
the builder won’t be considered up until the SSH server is actually running
1 parent f3755f6 commit 96ec946

5 files changed

Lines changed: 29 additions & 14 deletions

File tree

boot.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func main() {
4848
pkglog.Err("getting config for %s [%s]", serverConfAppName, err)
4949
os.Exit(1)
5050
}
51+
52+
serverCircuit := sshd.NewCircuit()
53+
5154
s3Client, err := storage.GetClient(cnf.HealthSrvTestStorageRegion)
5255
if err != nil {
5356
pkglog.Err("getting s3 client [%s]", err)
@@ -61,15 +64,15 @@ func main() {
6164
pkglog.Info("starting health check server on port %d", cnf.HealthSrvPort)
6265
healthSrvCh := make(chan error)
6366
go func() {
64-
if err := healthsrv.Start(cnf.HealthSrvPort, kubeClient.Namespaces(), s3Client); err != nil {
67+
if err := healthsrv.Start(cnf.HealthSrvPort, kubeClient.Namespaces(), s3Client, serverCircuit); err != nil {
6568
healthSrvCh <- err
6669
}
6770
}()
6871

6972
pkglog.Info("starting SSH server on %s:%d", cnf.SSHHostIP, cnf.SSHHostPort)
7073
sshCh := make(chan int)
7174
go func() {
72-
sshCh <- pkg.Run(cnf.SSHHostIP, cnf.SSHHostPort, "boot")
75+
sshCh <- pkg.RunBuilder(cnf.SSHHostIP, cnf.SSHHostPort, serverCircuit)
7376
}()
7477

7578
select {

pkg/builder.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ package pkg
55

66
import (
77
"fmt"
8+
"log"
9+
"os"
810

911
"github.com/Masterminds/cookoo"
1012
clog "github.com/Masterminds/cookoo/log"
1113
"github.com/deis/builder/pkg/sshd"
12-
13-
"log"
14-
"os"
1514
)
1615

1716
// Return codes that will be sent to the shell.
@@ -28,7 +27,7 @@ const (
2827
// Git.
2928
//
3029
// Run returns on of the Status* status code constants.
31-
func Run(sshHostIP string, sshHostPort int, cmd string) int {
30+
func RunBuilder(sshHostIP string, sshHostPort int, sshServerCircuit *sshd.Circuit) int {
3231
reg, router, ocxt := cookoo.Cookoo()
3332
log.SetFlags(0) // Time is captured elsewhere.
3433

@@ -59,7 +58,7 @@ func Run(sshHostIP string, sshHostPort int, cmd string) int {
5958
// Start the SSH service.
6059
// TODO: We could refactor Serve to be a command, and then run this as
6160
// a route.
62-
if err := sshd.Serve(reg, router, cxt); err != nil {
61+
if err := sshd.Serve(reg, router, sshServerCircuit, cxt); err != nil {
6362
clog.Errf(cxt, "SSH server failed: %s", err)
6463
return StatusLocalError
6564
}

pkg/healthsrv/healthz_handler.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
s3 "github.com/aws/aws-sdk-go/service/s3"
1010
"github.com/deis/builder/pkg/gitreceive/log"
11+
"github.com/deis/builder/pkg/sshd"
1112
"k8s.io/kubernetes/pkg/fields"
1213
"k8s.io/kubernetes/pkg/labels"
1314
)
@@ -25,12 +26,21 @@ func convertBucket(b *s3.Bucket) healthZRespBucket {
2526
}
2627

2728
type healthZResp struct {
28-
Namespaces []string `json:"k8s_namespaces"`
29-
S3Buckets []healthZRespBucket `json:"s3_buckets"`
29+
Namespaces []string `json:"k8s_namespaces"`
30+
S3Buckets []healthZRespBucket `json:"s3_buckets"`
31+
SSHServerStarted bool `json:"ssh_server_started"`
3032
}
3133

32-
func healthZHandler(nsLister NamespaceLister, bLister BucketLister) http.Handler {
34+
func healthZHandler(nsLister NamespaceLister, bLister BucketLister, serverCircuit *sshd.Circuit) http.Handler {
3335
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36+
// there's a race between the boolean eval and the HTTP error returned, but k8s will repeat the health probe request
37+
// and effectively re-evaluate the boolean.
38+
if serverCircuit.State() != sshd.ClosedState {
39+
str := fmt.Sprintf("SSH Server is not yet started")
40+
log.Err(str)
41+
http.Error(w, str, http.StatusAccepted)
42+
return
43+
}
3444
lbOut, err := bLister.ListBuckets(&s3.ListBucketsInput{})
3545
if err != nil {
3646
str := fmt.Sprintf("Error listing buckets (%s)", err)
@@ -53,7 +63,7 @@ func healthZHandler(nsLister NamespaceLister, bLister BucketLister) http.Handler
5363
for _, ns := range nsList.Items {
5464
rsp.Namespaces = append(rsp.Namespaces, ns.Name)
5565
}
56-
66+
rsp.SSHServerStarted = true
5767
if err := json.NewEncoder(w).Encode(rsp); err != nil {
5868
str := fmt.Sprintf("Error encoding JSON (%s)", err)
5969
http.Error(w, str, http.StatusInternalServerError)

pkg/healthsrv/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package healthsrv
33
import (
44
"fmt"
55
"net/http"
6+
7+
"github.com/deis/builder/pkg/sshd"
68
)
79

810
const (
@@ -11,9 +13,9 @@ const (
1113
)
1214

1315
// Start starts the healthcheck server on $host:$port and blocks. It only returns if the server fails, with the indicative error
14-
func Start(port int, nsLister NamespaceLister, bLister BucketLister) error {
16+
func Start(port int, nsLister NamespaceLister, bLister BucketLister, sshServerCircuit *sshd.Circuit) error {
1517
mux := http.NewServeMux()
16-
mux.Handle("/healthz", healthZHandler(nsLister, bLister))
18+
mux.Handle("/healthz", healthZHandler(nsLister, bLister, sshServerCircuit))
1719

1820
hostStr := fmt.Sprintf(":%d", port)
1921
return http.ListenAndServe(hostStr, mux)

pkg/sshd/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
//
4747
// This puts the following variables into the context:
4848
// - ssh.Closer (chan interface{}): Send a message to this to shutdown the server.
49-
func Serve(reg *cookoo.Registry, router *cookoo.Router, c cookoo.Context) cookoo.Interrupt {
49+
func Serve(reg *cookoo.Registry, router *cookoo.Router, serverCircuit *Circuit, c cookoo.Context) cookoo.Interrupt {
5050
hostkeys := c.Get(HostKeys, []ssh.Signer{}).([]ssh.Signer)
5151
addr := c.Get(Address, "0.0.0.0:2223").(string)
5252
cfg := c.Get(ServerConfig, &ssh.ServerConfig{}).(*ssh.ServerConfig)
@@ -70,6 +70,7 @@ func Serve(reg *cookoo.Registry, router *cookoo.Router, c cookoo.Context) cookoo
7070
c.Put("sshd.Closer", closer)
7171

7272
log.Infof(c, "Listening on %s", addr)
73+
serverCircuit.Close()
7374
srv.listen(listener, cfg, closer)
7475

7576
return nil

0 commit comments

Comments
 (0)