Skip to content

Commit f461e2f

Browse files
committed
ENH: Checks initial reload in HEALTHCHECK
1 parent 38a3bf9 commit f461e2f

3 files changed

Lines changed: 60 additions & 19 deletions

File tree

scripts/check.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ if [[ "$HEALTHCHECK" == "true" ]]; then
1515
fi
1616

1717
if [[ "$LISTENER_ADDRESS" != "" ]]; then
18-
wget -qO- "http://${LISTENER_ADDRESS}:8080/v1/docker-flow-swarm-listener/ping"
18+
wget -qO- "http://localhost:8080/v1/docker-flow-proxy/successfulinitreload"
1919

2020
if [[ $? -ne 0 ]]; then
21-
echo "ERROR: Unable to ping ${LISTENER_ADDRESS}"
21+
echo "ERROR: Initial reload was not successful"
2222
exit 1
2323
fi
2424
fi

server.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package main
22

33
import (
4-
"./actions"
5-
"./metrics"
6-
"./proxy"
7-
"./server"
84
"fmt"
9-
"github.com/gorilla/mux"
10-
"github.com/prometheus/client_golang/prometheus"
115
"net/http"
126
"os"
137
"strconv"
148
"strings"
159
"time"
10+
11+
"./actions"
12+
"./metrics"
13+
"./proxy"
14+
"./server"
15+
"github.com/gorilla/mux"
16+
"github.com/prometheus/client_golang/prometheus"
1617
)
1718

1819
// Server defines interface used for creating DFP Web server
@@ -22,10 +23,11 @@ type Server interface {
2223
}
2324

2425
type serve struct {
25-
IP string `short:"i" long:"ip" default:"0.0.0.0" env:"IP" description:"IP the server listens to."`
26-
ListenerAddress string `short:"l" long:"listener-address" env:"LISTENER_ADDRESS" description:"The address of the Docker Flow: Swarm Listener. The address matches the name of the Swarm service (e.g. swarm-listener)"`
27-
Port string `short:"p" long:"port" default:"8080" env:"PORT" description:"Port the server listens to."`
28-
ServiceName string `short:"n" long:"service-name" default:"proxy" env:"SERVICE_NAME" description:"The name of the proxy service. It is used only when running in 'swarm' mode and must match the '--name' parameter used to launch the service."`
26+
IP string `short:"i" long:"ip" default:"0.0.0.0" env:"IP" description:"IP the server listens to."`
27+
ListenerAddress string `short:"l" long:"listener-address" env:"LISTENER_ADDRESS" description:"The address of the Docker Flow: Swarm Listener. The address matches the name of the Swarm service (e.g. swarm-listener)"`
28+
Port string `short:"p" long:"port" default:"8080" env:"PORT" description:"Port the server listens to."`
29+
ServiceName string `short:"n" long:"service-name" default:"proxy" env:"SERVICE_NAME" description:"The name of the proxy service. It is used only when running in 'swarm' mode and must match the '--name' parameter used to launch the service."`
30+
SuccessfulInitReload bool
2931
// TODO: Remove
3032
actions.BaseReconfigure
3133
}
@@ -68,6 +70,7 @@ func (m *serve) Execute(args []string) error {
6870
r.HandleFunc("/v1/docker-flow-proxy/reconfigure", server2.ReconfigureHandler)
6971
r.HandleFunc("/v1/docker-flow-proxy/reload", server2.ReloadHandler)
7072
r.HandleFunc("/v1/docker-flow-proxy/remove", server2.RemoveHandler)
73+
r.HandleFunc("/v1/docker-flow-proxy/successfulinitreload", m.SuccessfulInitReloadHandler)
7174
r.HandleFunc("/v1/test", server2.Test1Handler)
7275
r.HandleFunc("/v2/test", server2.Test2Handler)
7376
return httpListenAndServe(address, r)
@@ -91,8 +94,11 @@ func (m *serve) reconfigure(server server.Server) error {
9194
err.Error(),
9295
interval/time.Second,
9396
)
94-
} else if !repeatReload {
95-
break
97+
} else {
98+
m.SuccessfulInitReload = true
99+
if !repeatReload {
100+
break
101+
}
96102
}
97103
}
98104

@@ -109,6 +115,16 @@ func (m *serve) reconfigure(server server.Server) error {
109115
return nil
110116
}
111117

118+
// SuccessfulInitReloadHandler responses with StatusOK when is SuccessfulInitReload otherwise
119+
// it response with StatusInternalServerError
120+
func (m *serve) SuccessfulInitReloadHandler(w http.ResponseWriter, req *http.Request) {
121+
if !m.SuccessfulInitReload {
122+
w.WriteHeader(http.StatusInternalServerError)
123+
return
124+
}
125+
w.WriteHeader(http.StatusOK)
126+
}
127+
112128
// TODO: Move to server package
113129
func (m *serve) certPutHandler(w http.ResponseWriter, req *http.Request) {
114130
cert.Put(w, req)

server_test.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
package main
44

55
import (
6-
"./actions"
7-
"./proxy"
8-
"./server"
96
"fmt"
10-
"github.com/stretchr/testify/mock"
11-
"github.com/stretchr/testify/suite"
127
"net/http"
138
"net/http/httptest"
149
"os"
1510
"strings"
1611
"testing"
1712
"time"
13+
14+
"./actions"
15+
"./proxy"
16+
"./server"
17+
"github.com/stretchr/testify/mock"
18+
"github.com/stretchr/testify/suite"
1819
)
1920

2021
type ServerTestSuite struct {
@@ -256,6 +257,30 @@ func (s *ServerTestSuite) Test_Execute_RepeatsContactingSwarmListenerAddress() {
256257
s.Equal(fmt.Sprintf("http://%s:8080-2", expectedListenerAddress), actualListenerAddress2)
257258
}
258259

260+
// SuccessfulInitReloadHandler
261+
262+
func (s *ServerTestSuite) Test_SuccessfulInitReloadHandler_ReturnsStatus200() {
263+
req, _ := http.NewRequest("GET", "/v1/docker-flow-proxy/successfulinitreload", nil)
264+
265+
srv := serve{}
266+
srv.SuccessfulInitReload = true
267+
srv.SuccessfulInitReloadHandler(s.ResponseWriter, req)
268+
269+
s.ResponseWriter.AssertCalled(s.T(), "WriteHeader", http.StatusOK)
270+
271+
}
272+
273+
func (s *ServerTestSuite) Test_SuccessfulInitReloadHandler_ReturnsStatusInternalServerError() {
274+
275+
req, _ := http.NewRequest("GET", "/v1/docker-flow-proxy/successfulinitreload", nil)
276+
277+
srv := serve{}
278+
srv.SuccessfulInitReload = false
279+
srv.SuccessfulInitReloadHandler(s.ResponseWriter, req)
280+
281+
s.ResponseWriter.AssertCalled(s.T(), "WriteHeader", http.StatusInternalServerError)
282+
}
283+
259284
// CertPutHandler
260285

261286
func (s *ServerTestSuite) Test_CertPutHandler_InvokesCertPut_WhenUrlIsCert() {

0 commit comments

Comments
 (0)