Skip to content

Commit ecfa8cc

Browse files
Merge pull request #2477 from tchap/rollback-httplog
OCPBUGS-43994: UPSTREAM: <carry>: kube-apiserver: wire through isTerminating into handler chain
2 parents 5c9df33 + f6d00c9 commit ecfa8cc

File tree

9 files changed

+16
-53
lines changed

9 files changed

+16
-53
lines changed

cmd/kube-scheduler/app/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func buildHandlerChain(handler http.Handler, authn authenticator.Request, authz
340340
handler = genericapifilters.WithAuthentication(handler, authn, failedHandler, nil, nil)
341341
handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
342342
handler = genericapifilters.WithCacheControl(handler)
343-
handler = genericfilters.WithHTTPLogging(handler, nil)
343+
handler = genericfilters.WithHTTPLogging(handler)
344344
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)
345345

346346
return handler

pkg/kubelet/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ var statusesNoTracePred = httplog.StatusIsNot(
11551155

11561156
// ServeHTTP responds to HTTP requests on the Kubelet.
11571157
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
1158-
handler := httplog.WithLogging(s.restfulCont, statusesNoTracePred, nil)
1158+
handler := httplog.WithLogging(s.restfulCont, statusesNoTracePred)
11591159

11601160
// monitor http requests
11611161
var serverType string

staging/src/k8s.io/apiserver/pkg/server/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
11521152
}
11531153
handler = genericfilters.WithOptInRetryAfter(handler, c.newServerFullyInitializedFunc())
11541154
handler = genericfilters.WithShutdownResponseHeader(handler, c.lifecycleSignals.ShutdownInitiated, c.ShutdownDelayDuration, c.APIServerID)
1155-
handler = genericfilters.WithHTTPLogging(handler, c.newIsTerminatingFunc())
1155+
handler = genericfilters.WithHTTPLogging(handler)
11561156
handler = genericapifilters.WithLatencyTrackers(handler)
11571157
// WithRoutine will execute future handlers in a separate goroutine and serving
11581158
// handler in current goroutine to minimize the stack memory usage. It must be

staging/src/k8s.io/apiserver/pkg/server/filters/timeout_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,6 @@ func TestTimeoutWithLogging(t *testing.T) {
355355
},
356356
),
357357
),
358-
func() bool {
359-
return false
360-
},
361358
),
362359
)
363360
defer ts.Close()

staging/src/k8s.io/apiserver/pkg/server/filters/wrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func WithPanicRecovery(handler http.Handler, resolver request.RequestInfoResolve
5959
}
6060

6161
// WithHTTPLogging enables logging of incoming requests.
62-
func WithHTTPLogging(handler http.Handler, isTerminating func() bool) http.Handler {
63-
return httplog.WithLogging(handler, httplog.DefaultStacktracePred, isTerminating)
62+
func WithHTTPLogging(handler http.Handler) http.Handler {
63+
return httplog.WithLogging(handler, httplog.DefaultStacktracePred)
6464
}
6565

6666
func withPanicRecovery(handler http.Handler, crashHandler func(http.ResponseWriter, *http.Request, interface{})) http.Handler {

staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ type respLogger struct {
6767
addedInfo strings.Builder
6868
addedKeyValuePairs []interface{}
6969
startTime time.Time
70-
isTerminating bool
7170

7271
captureErrorOutput bool
7372

@@ -101,13 +100,13 @@ func DefaultStacktracePred(status int) bool {
101100
const withLoggingLevel = 3
102101

103102
// WithLogging wraps the handler with logging.
104-
func WithLogging(handler http.Handler, pred StacktracePred, isTerminatingFn func() bool) http.Handler {
103+
func WithLogging(handler http.Handler, pred StacktracePred) http.Handler {
105104
return withLogging(handler, pred, func() bool {
106105
return klog.V(withLoggingLevel).Enabled()
107-
}, isTerminatingFn)
106+
})
108107
}
109108

110-
func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogRequest ShouldLogRequestPred, isTerminatingFn func() bool) http.Handler {
109+
func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogRequest ShouldLogRequestPred) http.Handler {
111110
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
112111
if !shouldLogRequest() {
113112
handler.ServeHTTP(w, req)
@@ -118,16 +117,14 @@ func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogR
118117
if old := respLoggerFromRequest(req); old != nil {
119118
panic("multiple WithLogging calls!")
120119
}
120+
121121
startTime := time.Now()
122122
if receivedTimestamp, ok := request.ReceivedTimestampFrom(ctx); ok {
123123
startTime = receivedTimestamp
124124
}
125125

126-
isTerminating := false
127-
if isTerminatingFn != nil {
128-
isTerminating = isTerminatingFn()
129-
}
130-
rl := newLoggedWithStartTime(req, w, startTime).StacktraceWhen(stackTracePred).IsTerminating(isTerminating)
126+
rl := newLoggedWithStartTime(req, w, startTime)
127+
rl.StacktraceWhen(stackTracePred)
131128
req = req.WithContext(context.WithValue(ctx, respLoggerContextKey, rl))
132129

133130
var logFunc func()
@@ -138,9 +135,6 @@ func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogR
138135
}
139136
}()
140137

141-
if klog.V(3).Enabled() || (rl.isTerminating && klog.V(1).Enabled()) {
142-
defer rl.Log()
143-
}
144138
w = responsewriter.WrapForHTTP1Or2(rl)
145139
handler.ServeHTTP(w, req)
146140

@@ -211,12 +205,6 @@ func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
211205
return rl
212206
}
213207

214-
// IsTerminating informs the logger that the server is terminating.
215-
func (rl *respLogger) IsTerminating(is bool) *respLogger {
216-
rl.isTerminating = is
217-
return rl
218-
}
219-
220208
// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
221209
// for any status *not* in the given list.
222210
func StatusIsNot(statuses ...int) StacktracePred {

staging/src/k8s.io/apiserver/pkg/server/httplog/httplog_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestWithLogging(t *testing.T) {
6767
shouldLogRequest := func() bool { return true }
6868
var handler http.Handler
6969
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
70-
handler = withLogging(withLogging(handler, DefaultStacktracePred, shouldLogRequest, nil), DefaultStacktracePred, shouldLogRequest, nil)
70+
handler = withLogging(withLogging(handler, DefaultStacktracePred, shouldLogRequest), DefaultStacktracePred, shouldLogRequest)
7171

7272
func() {
7373
defer func() {
@@ -111,7 +111,7 @@ func TestLogOf(t *testing.T) {
111111
t.Errorf("Expected %v, got %v", test.want, got)
112112
}
113113
})
114-
handler = withLogging(handler, DefaultStacktracePred, func() bool { return test.shouldLogRequest }, nil)
114+
handler = withLogging(handler, DefaultStacktracePred, func() bool { return test.shouldLogRequest })
115115
w := httptest.NewRecorder()
116116
handler.ServeHTTP(w, req)
117117
})
@@ -135,7 +135,7 @@ func TestUnlogged(t *testing.T) {
135135
}
136136
})
137137
if makeLogger {
138-
handler = WithLogging(handler, DefaultStacktracePred, nil)
138+
handler = WithLogging(handler, DefaultStacktracePred)
139139
}
140140

141141
handler.ServeHTTP(origWriter, req)
@@ -216,7 +216,7 @@ func TestRespLoggerWithDecoratedResponseWriter(t *testing.T) {
216216
}
217217
})
218218

219-
handler = withLogging(handler, DefaultStacktracePred, func() bool { return true }, nil)
219+
handler = withLogging(handler, DefaultStacktracePred, func() bool { return true })
220220
handler.ServeHTTP(test.r(), req)
221221
})
222222
}

staging/src/k8s.io/apiserver/pkg/server/patch_config.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,6 @@ limitations under the License.
1616

1717
package server
1818

19-
// newIsTerminatingFunc returns a 'func() bool' that relies on the
20-
// 'ShutdownInitiated' life cycle signal of answer if the apiserver
21-
// has started the termination process.
22-
func (c *Config) newIsTerminatingFunc() func() bool {
23-
var shutdownCh <-chan struct{}
24-
// TODO: a properly initialized Config object should always have lifecycleSignals
25-
// initialized, but some config unit tests leave lifecycleSignals as nil.
26-
// Fix the unit tests upstream and then we can remove this check.
27-
if c.lifecycleSignals.ShutdownInitiated != nil {
28-
shutdownCh = c.lifecycleSignals.ShutdownInitiated.Signaled()
29-
}
30-
31-
return func() bool {
32-
select {
33-
case <-shutdownCh:
34-
return true
35-
default:
36-
return false
37-
}
38-
}
39-
}
40-
4119
func (c *Config) newServerFullyInitializedFunc() func() bool {
4220
return func() bool {
4321
select {

staging/src/k8s.io/controller-manager/app/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func BuildHandlerChain(apiHandler http.Handler, authorizationInfo *apiserver.Aut
4848
}
4949
handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
5050
handler = genericapifilters.WithCacheControl(handler)
51-
handler = genericfilters.WithHTTPLogging(handler, nil)
51+
handler = genericfilters.WithHTTPLogging(handler)
5252
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)
5353

5454
return handler

0 commit comments

Comments
 (0)