Improve error response logging

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
Brad Davidson 2023-05-31 21:01:21 +00:00 committed by Brad Davidson
parent 45d8c1a1a2
commit 5170bc5a04

View File

@ -143,7 +143,7 @@ func cacerts(serverCA string) http.Handler {
var err error var err error
ca, err = os.ReadFile(serverCA) ca, err = os.ReadFile(serverCA)
if err != nil { if err != nil {
sendError(err, resp) sendError(err, resp, req)
return return
} }
} }
@ -218,13 +218,13 @@ func servingKubeletCert(server *config.Control, keyFile string, auth nodePassBoo
nodeName, errCode, err := auth(req) nodeName, errCode, err := auth(req)
if err != nil { if err != nil {
sendError(err, resp, errCode) sendError(err, resp, req, errCode)
return return
} }
caCerts, caKey, key, err := getCACertAndKeys(server.Runtime.ServerCA, server.Runtime.ServerCAKey, server.Runtime.ServingKubeletKey) caCerts, caKey, key, err := getCACertAndKeys(server.Runtime.ServerCA, server.Runtime.ServerCAKey, server.Runtime.ServingKubeletKey)
if err != nil { if err != nil {
sendError(err, resp) sendError(err, resp, req)
return return
} }
@ -234,7 +234,7 @@ func servingKubeletCert(server *config.Control, keyFile string, auth nodePassBoo
for _, v := range strings.Split(nodeIP, ",") { for _, v := range strings.Split(nodeIP, ",") {
ip := net.ParseIP(v) ip := net.ParseIP(v)
if ip == nil { if ip == nil {
sendError(fmt.Errorf("invalid IP address %s", ip), resp) sendError(fmt.Errorf("invalid node IP address %s", ip), resp, req)
return return
} }
ips = append(ips, ip) ips = append(ips, ip)
@ -250,7 +250,7 @@ func servingKubeletCert(server *config.Control, keyFile string, auth nodePassBoo
}, },
}, key, caCerts[0], caKey) }, key, caCerts[0], caKey)
if err != nil { if err != nil {
sendError(err, resp) sendError(err, resp, req)
return return
} }
@ -274,13 +274,13 @@ func clientKubeletCert(server *config.Control, keyFile string, auth nodePassBoot
nodeName, errCode, err := auth(req) nodeName, errCode, err := auth(req)
if err != nil { if err != nil {
sendError(err, resp, errCode) sendError(err, resp, req, errCode)
return return
} }
caCerts, caKey, key, err := getCACertAndKeys(server.Runtime.ClientCA, server.Runtime.ClientCAKey, server.Runtime.ClientKubeletKey) caCerts, caKey, key, err := getCACertAndKeys(server.Runtime.ClientCA, server.Runtime.ClientCAKey, server.Runtime.ClientKubeletKey)
if err != nil { if err != nil {
sendError(err, resp) sendError(err, resp, req)
return return
} }
@ -290,7 +290,7 @@ func clientKubeletCert(server *config.Control, keyFile string, auth nodePassBoot
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}, key, caCerts[0], caKey) }, key, caCerts[0], caKey)
if err != nil { if err != nil {
sendError(err, resp) sendError(err, resp, req)
return return
} }
@ -400,7 +400,7 @@ func serveStatic(urlPrefix, staticDir string) http.Handler {
return http.StripPrefix(urlPrefix, http.FileServer(http.Dir(staticDir))) return http.StripPrefix(urlPrefix, http.FileServer(http.Dir(staticDir)))
} }
func sendError(err error, resp http.ResponseWriter, status ...int) { func sendError(err error, resp http.ResponseWriter, req *http.Request, status ...int) {
var code int var code int
if len(status) == 1 { if len(status) == 1 {
code = status[0] code = status[0]
@ -408,9 +408,11 @@ func sendError(err error, resp http.ResponseWriter, status ...int) {
if code == 0 || code == http.StatusOK { if code == 0 || code == http.StatusOK {
code = http.StatusInternalServerError code = http.StatusInternalServerError
} }
logrus.Error(err) logrus.Errorf("Sending HTTP %d response to %s: %v", code, req.RemoteAddr, err)
resp.WriteHeader(code) responsewriters.ErrorNegotiated(
resp.Write([]byte(err.Error())) apierrors.NewGenericServerResponse(code, req.Method, schema.GroupResource{}, req.URL.Path, err.Error(), 0, true),
scheme.Codecs.WithoutConversion(), schema.GroupVersion{}, resp, req,
)
} }
// nodePassBootstrapper returns a node name, or http error code and error // nodePassBootstrapper returns a node name, or http error code and error