Return ServiceUnavailable error consistently from proxy

This commit is contained in:
Jordan Liggitt 2018-01-23 10:45:29 -05:00
parent d512e87bb8
commit 91ba8c37d0
No known key found for this signature in database
GPG Key ID: 39928704103C7229
2 changed files with 35 additions and 6 deletions

View File

@ -120,7 +120,8 @@ func (r *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
location.Scheme = "https"
rloc, err := r.serviceResolver.ResolveEndpoint(handlingInfo.serviceNamespace, handlingInfo.serviceName)
if err != nil {
http.Error(w, fmt.Sprintf("missing route (%s)", err.Error()), http.StatusInternalServerError)
glog.Errorf("error resolving %s/%s: %v", handlingInfo.serviceNamespace, handlingInfo.serviceName, err)
http.Error(w, "service unavailable", http.StatusServiceUnavailable)
return
}
location.Host = rloc.Host

View File

@ -18,6 +18,7 @@ package apiserver
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -84,13 +85,11 @@ func (*fakeRequestContextMapper) Update(req *http.Request, context genericapireq
type mockedRouter struct {
destinationHost string
err error
}
func (r *mockedRouter) ResolveEndpoint(namespace, name string) (*url.URL, error) {
return &url.URL{
Scheme: "https",
Host: r.destinationHost,
}, nil
return &url.URL{Scheme: "https", Host: r.destinationHost}, r.err
}
func TestProxyHandler(t *testing.T) {
@ -109,6 +108,8 @@ func TestProxyHandler(t *testing.T) {
path string
apiService *apiregistration.APIService
serviceResolver ServiceResolver
expectedStatusCode int
expectedBody string
expectedCalled bool
@ -220,6 +221,29 @@ func TestProxyHandler(t *testing.T) {
},
expectedStatusCode: http.StatusServiceUnavailable,
},
"service unresolveable": {
user: &user.DefaultInfo{
Name: "username",
Groups: []string{"one", "two"},
},
path: "/request/path",
serviceResolver: &mockedRouter{err: fmt.Errorf("unresolveable")},
apiService: &apiregistration.APIService{
ObjectMeta: metav1.ObjectMeta{Name: "v1.foo"},
Spec: apiregistration.APIServiceSpec{
Service: &apiregistration.ServiceReference{Name: "bad-service", Namespace: "test-ns"},
Group: "foo",
Version: "v1",
CABundle: testCACrt,
},
Status: apiregistration.APIServiceStatus{
Conditions: []apiregistration.APIServiceCondition{
{Type: apiregistration.Available, Status: apiregistration.ConditionTrue},
},
},
},
expectedStatusCode: http.StatusServiceUnavailable,
},
"fail on bad serving cert": {
user: &user.DefaultInfo{
Name: "username",
@ -247,9 +271,13 @@ func TestProxyHandler(t *testing.T) {
target.Reset()
func() {
serviceResolver := tc.serviceResolver
if serviceResolver == nil {
serviceResolver = &mockedRouter{destinationHost: targetServer.Listener.Addr().String()}
}
handler := &proxyHandler{
localDelegate: http.NewServeMux(),
serviceResolver: &mockedRouter{destinationHost: targetServer.Listener.Addr().String()},
serviceResolver: serviceResolver,
proxyTransport: &http.Transport{},
}
handler.contextMapper = &fakeRequestContextMapper{user: tc.user}