From 6262ff2f4a05f0bc8d773e01dc81b0301c0d69d5 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Fri, 26 Feb 2021 08:58:02 +0000 Subject: [PATCH] Update proxy from provider When endpoints are not found, a 503 is returned instead of a 404. Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- go.mod | 2 +- go.sum | 4 ++-- .../openfaas/faas-provider/proxy/proxy.go | 21 ++++++++++++------- vendor/modules.txt | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 22ae0911..8692f262 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/opencontainers/runc v1.0.0-rc9 // indirect github.com/opencontainers/runtime-spec v1.0.2 github.com/openfaas/faas v0.0.0-20201205125747-9bbb25e3c7c4 - github.com/openfaas/faas-provider v0.16.2 + github.com/openfaas/faas-provider v0.17.0 github.com/pkg/errors v0.9.1 github.com/prometheus/procfs v0.2.0 // indirect github.com/sethvargo/go-password v0.1.3 diff --git a/go.sum b/go.sum index 8c1a2752..ed4d3849 100644 --- a/go.sum +++ b/go.sum @@ -181,8 +181,8 @@ github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/ github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs= github.com/openfaas/faas v0.0.0-20201205125747-9bbb25e3c7c4 h1:JJjthDw7WziZQ7sC5C+M2872mIdud5R+s6Cb0cXyPuA= github.com/openfaas/faas v0.0.0-20201205125747-9bbb25e3c7c4/go.mod h1:E0m2rLup0Vvxg53BKxGgaYAGcZa3Xl+vvL7vSi5yQ14= -github.com/openfaas/faas-provider v0.16.2 h1:ChpiZh1RM8zFIzvp31OPlKpTbh5Lcm7f91WCFcpW4gA= -github.com/openfaas/faas-provider v0.16.2/go.mod h1:fq1JL0mX4rNvVVvRLaLRJ3H6o667sHuyP5p/7SZEe98= +github.com/openfaas/faas-provider v0.17.0 h1:4rT8CosKhI5xaAMqbyihEgR6KefO/ViJdF0a8THTgwM= +github.com/openfaas/faas-provider v0.17.0/go.mod h1:fq1JL0mX4rNvVVvRLaLRJ3H6o667sHuyP5p/7SZEe98= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/vendor/github.com/openfaas/faas-provider/proxy/proxy.go b/vendor/github.com/openfaas/faas-provider/proxy/proxy.go index b3fb3083..09412690 100644 --- a/vendor/github.com/openfaas/faas-provider/proxy/proxy.go +++ b/vendor/github.com/openfaas/faas-provider/proxy/proxy.go @@ -34,9 +34,8 @@ import ( ) const ( - watchdogPort = "8080" - defaultContentType = "text/plain" - errMissingFunctionName = "Please provide a valid route /function/function_name." + watchdogPort = "8080" + defaultContentType = "text/plain" ) // BaseURLResolver URL resolver for proxy requests @@ -136,15 +135,15 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient pathVars := mux.Vars(originalReq) functionName := pathVars["name"] if functionName == "" { - httputil.Errorf(w, http.StatusBadRequest, errMissingFunctionName) + httputil.Errorf(w, http.StatusBadRequest, "Provide function name in the request path") return } functionAddr, resolveErr := resolver.Resolve(functionName) if resolveErr != nil { // TODO: Should record the 404/not found error in Prometheus. - log.Printf("resolver error: cannot find %s: %s\n", functionName, resolveErr.Error()) - httputil.Errorf(w, http.StatusNotFound, "Cannot find service: %s.", functionName) + log.Printf("resolver error: no endpoints for %s: %s\n", functionName, resolveErr.Error()) + httputil.Errorf(w, http.StatusServiceUnavailable, "No endpoints available for: %s.", functionName) return } @@ -153,6 +152,7 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient httputil.Errorf(w, http.StatusInternalServerError, "Failed to resolve service: %s.", functionName) return } + if proxyReq.Body != nil { defer proxyReq.Body.Close() } @@ -167,7 +167,10 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient httputil.Errorf(w, http.StatusInternalServerError, "Can't reach service for: %s.", functionName) return } - defer response.Body.Close() + + if response.Body != nil { + defer response.Body.Close() + } log.Printf("%s took %f seconds\n", functionName, seconds.Seconds()) @@ -176,7 +179,9 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient w.Header().Set("Content-Type", getContentType(originalReq.Header, response.Header)) w.WriteHeader(response.StatusCode) - io.Copy(w, response.Body) + if response.Body != nil { + io.Copy(w, response.Body) + } } // buildProxyRequest creates a request object for the proxy request, it will ensure that diff --git a/vendor/modules.txt b/vendor/modules.txt index a169221c..17114721 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -195,7 +195,7 @@ github.com/opencontainers/runtime-spec/specs-go # github.com/openfaas/faas v0.0.0-20201205125747-9bbb25e3c7c4 ## explicit github.com/openfaas/faas/gateway/requests -# github.com/openfaas/faas-provider v0.16.2 +# github.com/openfaas/faas-provider v0.17.0 ## explicit github.com/openfaas/faas-provider github.com/openfaas/faas-provider/auth