Skip to content

Commit a736659

Browse files
stttssoltysh
authored andcommitted
UPSTREAM: <carry>: kube-apiserver: wire through isTerminating into handler chain
Origin-commit: 5772e7285acbe901762d8cd8cb1fc33d8b459d04 openshift-rebase(v1.24):source=c7c48fdacb0 openshift-rebase(v1.24):source=c7c48fdacb0 openshift-rebase(v1.24):source=c7c48fdacb0 UPSTREAM: <carry>: use lifeCycleSignals for isTerminating openshift-rebase(v1.24):source=d3045350e43
1 parent 932411e commit a736659

File tree

10 files changed

+70
-18
lines changed

10 files changed

+70
-18
lines changed

cmd/kube-apiserver/app/server.go

-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"k8s.io/kubernetes/openshift-kube-apiserver/enablement"
3434
"k8s.io/kubernetes/openshift-kube-apiserver/openshiftkubeapiserver"
3535

36-
"github.com/go-openapi/spec"
3736
"github.com/spf13/cobra"
3837

3938
oteltrace "go.opentelemetry.io/otel/trace"
@@ -389,7 +388,6 @@ func CreateKubeAPIServerConfig(s completedServerRunOptions) (
389388
func buildGenericConfig(
390389
s *options.ServerRunOptions,
391390
proxyTransport *http.Transport,
392-
393391
) (
394392
genericConfig *genericapiserver.Config,
395393
versionedInformers clientgoinformers.SharedInformerFactory,

cmd/kube-scheduler/app/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func buildHandlerChain(handler http.Handler, authn authenticator.Request, authz
247247
handler = genericapifilters.WithAuthentication(handler, authn, failedHandler, nil)
248248
handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
249249
handler = genericapifilters.WithCacheControl(handler)
250-
handler = genericfilters.WithHTTPLogging(handler)
250+
handler = genericfilters.WithHTTPLogging(handler, nil)
251251
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)
252252

253253
return handler

pkg/kubelet/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ var statusesNoTracePred = httplog.StatusIsNot(
10241024

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

10291029
// monitor http requests
10301030
var serverType string

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
856856
if c.ShutdownSendRetryAfter {
857857
handler = genericfilters.WithRetryAfter(handler, c.lifecycleSignals.NotAcceptingNewRequest.Signaled())
858858
}
859-
handler = genericfilters.WithHTTPLogging(handler)
859+
handler = genericfilters.WithHTTPLogging(handler, c.newIsTerminatingFunc())
860860
if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.APIServerTracing) {
861861
handler = genericapifilters.WithTracing(handler, c.TracerProvider)
862862
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ func TestTimeoutWithLogging(t *testing.T) {
341341
},
342342
),
343343
),
344+
func() bool {
345+
return false
346+
},
344347
),
345348
)
346349
defer ts.Close()

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

+2-2
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) http.Handler {
63-
return httplog.WithLogging(handler, httplog.DefaultStacktracePred)
62+
func WithHTTPLogging(handler http.Handler, isTerminating func() bool) http.Handler {
63+
return httplog.WithLogging(handler, httplog.DefaultStacktracePred, isTerminating)
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

+18-6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type respLogger struct {
6565
addedInfo strings.Builder
6666
addedKeyValuePairs []interface{}
6767
startTime time.Time
68+
isTerminating bool
6869

6970
captureErrorOutput bool
7071

@@ -98,13 +99,13 @@ func DefaultStacktracePred(status int) bool {
9899
const withLoggingLevel = 3
99100

100101
// WithLogging wraps the handler with logging.
101-
func WithLogging(handler http.Handler, pred StacktracePred) http.Handler {
102+
func WithLogging(handler http.Handler, pred StacktracePred, isTerminatingFn func() bool) http.Handler {
102103
return withLogging(handler, pred, func() bool {
103104
return klog.V(withLoggingLevel).Enabled()
104-
})
105+
}, isTerminatingFn)
105106
}
106107

107-
func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogRequest ShouldLogRequestPred) http.Handler {
108+
func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogRequest ShouldLogRequestPred, isTerminatingFn func() bool) http.Handler {
108109
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
109110
if !shouldLogRequest() {
110111
handler.ServeHTTP(w, req)
@@ -115,17 +116,22 @@ func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogR
115116
if old := respLoggerFromRequest(req); old != nil {
116117
panic("multiple WithLogging calls!")
117118
}
118-
119119
startTime := time.Now()
120120
if receivedTimestamp, ok := request.ReceivedTimestampFrom(ctx); ok {
121121
startTime = receivedTimestamp
122122
}
123123

124-
rl := newLoggedWithStartTime(req, w, startTime)
125-
rl.StacktraceWhen(stackTracePred)
124+
isTerminating := false
125+
if isTerminatingFn != nil {
126+
isTerminating = isTerminatingFn()
127+
}
128+
rl := newLoggedWithStartTime(req, w, startTime).StacktraceWhen(stackTracePred).IsTerminating(isTerminating)
126129
req = req.WithContext(context.WithValue(ctx, respLoggerContextKey, rl))
127130
defer rl.Log()
128131

132+
if klog.V(3).Enabled() || (rl.isTerminating && klog.V(1).Enabled()) {
133+
defer rl.Log()
134+
}
129135
w = responsewriter.WrapForHTTP1Or2(rl)
130136
handler.ServeHTTP(w, req)
131137
})
@@ -185,6 +191,12 @@ func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
185191
return rl
186192
}
187193

194+
// IsTerminating informs the logger that the server is terminating.
195+
func (rl *respLogger) IsTerminating(is bool) *respLogger {
196+
rl.isTerminating = is
197+
return rl
198+
}
199+
188200
// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
189201
// for any status *not* in the given list.
190202
func StatusIsNot(statuses ...int) StacktracePred {

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

+4-4
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), DefaultStacktracePred, shouldLogRequest)
70+
handler = withLogging(withLogging(handler, DefaultStacktracePred, shouldLogRequest, nil), DefaultStacktracePred, shouldLogRequest, nil)
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 })
114+
handler = withLogging(handler, DefaultStacktracePred, func() bool { return test.shouldLogRequest }, nil)
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)
138+
handler = WithLogging(handler, DefaultStacktracePred, nil)
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 })
219+
handler = withLogging(handler, DefaultStacktracePred, func() bool { return true }, nil)
220220
handler.ServeHTTP(test.r(), req)
221221
})
222222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2021 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 server
18+
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+
}

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

+1-1
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)
51+
handler = genericfilters.WithHTTPLogging(handler, nil)
5252
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)
5353

5454
return handler

0 commit comments

Comments
 (0)