Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Added of-watchdog http mode support (for #67): #68

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions handlers/mock_proxy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (mp *MockProxyClient) GetFunctionName(r *http.Request) string {

// CallAndReturnResponse returns a mock response
func (mp *MockProxyClient) CallAndReturnResponse(address string, body []byte, h http.Header) (
[]byte, http.Header, error) {
[]byte, http.Header, int, error) {
args := mp.Called(address, body, h)

return args.Get(0).([]byte), args.Get(1).(http.Header), args.Error(2)
return args.Get(0).([]byte), args.Get(1).(http.Header), args.Get(2).(int), args.Error(3)
}
14 changes: 8 additions & 6 deletions handlers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (p *Proxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
return
}

respBody, respHeaders, err := p.callDownstreamFunction(service, urls, r)
body, headers, status, err := p.callDownstreamFunction(service, urls, r)

if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
Expand All @@ -81,32 +81,34 @@ func (p *Proxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
return
}

setHeaders(respHeaders, rw)
rw.Write(respBody)
setHeaders(headers, rw)
rw.WriteHeader(status)
rw.Write(body)
}

func (p *Proxy) callDownstreamFunction(service string, urls []string, r *http.Request) ([]byte, http.Header, error) {
func (p *Proxy) callDownstreamFunction(service string, urls []string, r *http.Request) ([]byte, http.Header, int, error) {
reqBody, _ := ioutil.ReadAll(r.Body)
reqHeaders := r.Header
defer r.Body.Close()

var respBody []byte
var respHeaders http.Header
var respStatus int
var err error

lb := p.getLoadbalancer(service, urls)
lb.Do(func(endpoint url.URL) error {
// add the querystring from the request
endpoint.RawQuery = r.URL.RawQuery
respBody, respHeaders, err = p.client.CallAndReturnResponse(endpoint.String(), reqBody, reqHeaders)
respBody, respHeaders, respStatus, err = p.client.CallAndReturnResponse(endpoint.String(), reqBody, reqHeaders)
if err != nil {
return err
}

return nil
})

return respBody, respHeaders, err
return respBody, respHeaders, respStatus, err
}

func setHeaders(headers http.Header, rw http.ResponseWriter) {
Expand Down
10 changes: 5 additions & 5 deletions handlers/proxy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// ProxyClient defines the interface for a client which calls faas functions
type ProxyClient interface {
GetFunctionName(*http.Request) string
CallAndReturnResponse(address string, body []byte, headers http.Header) ([]byte, http.Header, error)
CallAndReturnResponse(address string, body []byte, headers http.Header) ([]byte, http.Header, int, error)
}

// HTTPProxyClient allows the calling of functions
Expand Down Expand Up @@ -55,7 +55,7 @@ func (pc *HTTPProxyClient) GetFunctionName(r *http.Request) string {

// CallAndReturnResponse calls the function and returns the response
func (pc *HTTPProxyClient) CallAndReturnResponse(address string, body []byte, headers http.Header) (
[]byte, http.Header, error) {
[]byte, http.Header, int, error) {

defer func(when time.Time) {
seconds := time.Since(when).Seconds()
Expand All @@ -72,20 +72,20 @@ func (pc *HTTPProxyClient) CallAndReturnResponse(address string, body []byte, he
response, err := pc.proxyClient.Do(request)
if err != nil {
log.Println(err.Error())
return nil, nil, err
return nil, nil, response.StatusCode, err
}

respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
pc.logger.Error("Error reading body", "error", err)

return nil, nil, err
return nil, nil, response.StatusCode, err
}
response.Body.Close()

pc.logger.Info("Finished")

return respBody, response.Header, nil
return respBody, response.Header, response.StatusCode, nil
}

func copyHeaders(destination *http.Header, source *http.Header) {
Expand Down
4 changes: 2 additions & 2 deletions handlers/proxy_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestClientReturnsHeadersFromRequest(t *testing.T) {
c, r, s := setupProxyClient(body)
defer s.Close()

_, h, _ := c.CallAndReturnResponse(s.URL, body, r.Header)
_, h, _, _ := c.CallAndReturnResponse(s.URL, body, r.Header)

assert.Equal(t, "somevalue", h.Get("TESTHeader"))
}
Expand All @@ -68,7 +68,7 @@ func TestClientReturnsBodysFromRequest(t *testing.T) {
c, r, s := setupProxyClient(body)
defer s.Close()

b, _, _ := c.CallAndReturnResponse(s.URL, body, r.Header)
b, _, _, _ := c.CallAndReturnResponse(s.URL, body, r.Header)

assert.Equal(t, "my body", string(b))
}
16 changes: 8 additions & 8 deletions handlers/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestProxyHandlerWithFunctionNameCallsResolve(t *testing.T) {
h, rr, r := setupProxy("")
mockProxyClient.On("GetFunctionName", mock.Anything).Return("function")
mockProxyClient.On("CallAndReturnResponse", mock.Anything, mock.Anything, mock.Anything).
Return([]byte{}, http.Header{}, nil)
Return([]byte{}, http.Header{}, http.StatusOK, nil)
mockServiceResolver.On("Resolve", "function").Return([]string{"http://testaddress"})

h(rr, r)
Expand All @@ -68,7 +68,7 @@ func TestProxyHandlerCallsCallAndReturnResponse(t *testing.T) {
h, rr, r := setupProxy("")
mockProxyClient.On("GetFunctionName", mock.Anything).Return("function")
mockProxyClient.On("CallAndReturnResponse", mock.Anything, mock.Anything, mock.Anything).
Return([]byte{}, http.Header{}, nil)
Return([]byte{}, http.Header{}, http.StatusOK, nil)
mockServiceResolver.On("Resolve", "function").Return([]string{"http://testaddress"})

h(rr, r)
Expand All @@ -83,7 +83,7 @@ func TestProxyHandlerReturnsErrorWhenNoEndpoints(t *testing.T) {
h, rr, r := setupProxy("")
mockProxyClient.On("GetFunctionName", mock.Anything).Return("function")
mockProxyClient.On("CallAndReturnResponse", mock.Anything, mock.Anything, mock.Anything).
Return([]byte{}, http.Header{}, nil)
Return([]byte{}, http.Header{}, http.StatusOK, nil)
mockServiceResolver.On("Resolve", "function").Return([]string{})

h(rr, r)
Expand All @@ -99,12 +99,12 @@ func TestProxyHandlerReturnsErrorWhenClientError(t *testing.T) {
h, rr, r := setupProxy("")
mockProxyClient.On("GetFunctionName", mock.Anything).Return("function")
mockProxyClient.On("CallAndReturnResponse", mock.Anything, mock.Anything, mock.Anything).
Return([]byte{}, http.Header{}, fmt.Errorf("Oops, I did it again"))
Return([]byte{}, http.Header{}, http.StatusInternalServerError, fmt.Errorf("Oops, I did it again"))
mockServiceResolver.On("Resolve", "function").Return([]string{"http://testaddress"})

h(rr, r)

assert.Equal(t, http.StatusInternalServerError, rr.Code)
assert.Equal(t, http.StatusInternalServerError, http.StatusInternalServerError, rr.Code)
}

func TestProxyHandlerSetsHeadersOnSuccess(t *testing.T) {
Expand All @@ -114,13 +114,13 @@ func TestProxyHandlerSetsHeadersOnSuccess(t *testing.T) {
h, rr, r := setupProxy("")
mockProxyClient.On("GetFunctionName", mock.Anything).Return("function")
mockProxyClient.On("CallAndReturnResponse", mock.Anything, mock.Anything, mock.Anything).
Return([]byte{}, http.Header{"TestHeader": []string{"Testiculous"}}, nil)
Return([]byte{}, http.Header{"TestHeader": []string{"faas-nomad"}}, http.StatusOK, nil)
mockServiceResolver.On("Resolve", "function").Return([]string{"http://testaddress"})

h(rr, r)

assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "Testiculous", rr.Header().Get("TestHeader"))
assert.Equal(t, "faas-nomad", rr.Header().Get("TestHeader"), http.StatusOK, nil)
}

func TestProxyHandlerSetsBodyOnSuccess(t *testing.T) {
Expand All @@ -130,7 +130,7 @@ func TestProxyHandlerSetsBodyOnSuccess(t *testing.T) {
h, rr, r := setupProxy("")
mockProxyClient.On("GetFunctionName", mock.Anything).Return("function")
mockProxyClient.On("CallAndReturnResponse", mock.Anything, mock.Anything, mock.Anything).
Return([]byte("Something Something"), http.Header{}, nil)
Return([]byte("Something Something"), http.Header{}, http.StatusOK, nil)
mockServiceResolver.On("Resolve", "function").Return([]string{"http://testaddress"})

h(rr, r)
Expand Down
2 changes: 1 addition & 1 deletion nomad_job_files/faas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ EOH
}

config {
image = "openfaas/gateway:0.9.8"
image = "openfaas/gateway:0.9.14"

port_map {
http = 8080
Expand Down
2 changes: 1 addition & 1 deletion provisioning/saltstack/salt/nomad/files/faas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ EOH
}

config {
image = "openfaas/gateway:0.9.8"
image = "openfaas/gateway:0.9.14"

port_map {
http = 8080
Expand Down