| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package filters |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
|
| 22 |
"k8s.io/apimachinery/pkg/util/runtime" |
| 23 |
"k8s.io/apiserver/pkg/audit" |
| 24 |
"k8s.io/apiserver/pkg/endpoints/metrics" |
| 25 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 26 |
"k8s.io/apiserver/pkg/server/httplog" |
| 27 |
"k8s.io/klog/v2" |
| 28 |
) |
| 29 |
|
| 30 |
// WithPanicRecovery wraps an http Handler to recover and log panics (except in the special case of http.ErrAbortHandler panics, which suppress logging). |
| 31 |
func WithPanicRecovery(handler http.Handler, resolver request.RequestInfoResolver) http.Handler { |
| 32 |
return withPanicRecovery(handler, func(w http.ResponseWriter, req *http.Request, err interface{}) { |
| 33 |
if err == http.ErrAbortHandler { |
| 34 |
// Honor the http.ErrAbortHandler sentinel panic value |
| 35 |
// |
| 36 |
// If ServeHTTP panics, the server (the caller of ServeHTTP) assumes |
| 37 |
// that the effect of the panic was isolated to the active request. |
| 38 |
// It recovers the panic, logs a stack trace to the server error log, |
| 39 |
// and either closes the network connection or sends an HTTP/2 |
| 40 |
// RST_STREAM, depending on the HTTP protocol. To abort a handler so |
| 41 |
// the client sees an interrupted response but the server doesn't log |
| 42 |
// an error, panic with the value ErrAbortHandler. |
| 43 |
// |
| 44 |
// Note that HandleCrash function is actually crashing, after calling the handlers |
| 45 |
if info, err := resolver.NewRequestInfo(req); err != nil { |
| 46 |
metrics.RecordRequestAbort(req, nil) |
| 47 |
} else { |
| 48 |
metrics.RecordRequestAbort(req, info) |
| 49 |
} |
| 50 |
// This call can have different handlers, but the default chain rate limits. Call it after the metrics are updated |
| 51 |
// in case the rate limit delays it. If you outrun the rate for this one timed out requests, something has gone |
| 52 |
// seriously wrong with your server, but generally having a logging signal for timeouts is useful. |
| 53 |
runtime.HandleErrorWithContext(req.Context(), nil, "Timeout or abort while handling", "method", req.Method, "URI", req.RequestURI, "auditID", audit.GetAuditIDTruncated(req.Context())) |
| 54 |
return |
| 55 |
} |
| 56 |
http.Error(w, "This request caused apiserver to panic. Look in the logs for details.", http.StatusInternalServerError) |
| 57 |
klog.ErrorS(nil, "apiserver panic'd", "method", req.Method, "URI", req.RequestURI, "auditID", audit.GetAuditIDTruncated(req.Context())) |
| 58 |
}) |
| 59 |
} |
| 60 |
|
| 61 |
// WithHTTPLogging enables logging of incoming requests. |
| 62 |
func WithHTTPLogging(handler http.Handler) http.Handler { |
| 63 |
return httplog.WithLogging(handler, httplog.DefaultStacktracePred) |
| 64 |
} |
| 65 |
|
| 66 |
func withPanicRecovery(handler http.Handler, crashHandler func(http.ResponseWriter, *http.Request, interface{})) http.Handler { |
| 67 |
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 68 |
defer runtime.HandleCrash(func(err interface{}) { |
| 69 |
crashHandler(w, req, err) |
| 70 |
}) |
| 71 |
|
| 72 |
// Dispatch to the internal handler |
| 73 |
handler.ServeHTTP(w, req) |
| 74 |
}) |
| 75 |
} |