Skip to content

Commit 4031cc7

Browse files
tkashembertinatto
authored andcommitted
UPSTREAM: <carry>: send Retry-After when not ready with a caller opt in
UPSTREAM: <carry>: change opt-in due to upstream revert OpenShift-Rebase-Source: cd08005
1 parent 09d1a2c commit 4031cc7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
11031103
if c.ShutdownSendRetryAfter {
11041104
handler = genericfilters.WithRetryAfter(handler, c.lifecycleSignals.NotAcceptingNewRequest.Signaled())
11051105
}
1106+
handler = genericfilters.WithOptInRetryAfter(handler, c.newServerFullyInitializedFunc())
11061107
handler = genericfilters.WithHTTPLogging(handler, c.newIsTerminatingFunc())
11071108
if c.FeatureGate.Enabled(genericfeatures.APIServerTracing) {
11081109
handler = genericapifilters.WithTracing(handler, c.TracerProvider)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 filters
18+
19+
import (
20+
"net/http"
21+
)
22+
23+
func WithOptInRetryAfter(handler http.Handler, initializedFn func() bool) http.Handler {
24+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
25+
var retryAfter bool
26+
if value := req.Header.Get("X-OpenShift-Internal-If-Not-Ready"); value == "reject" {
27+
// the caller opted in for the request to be rejected if the server is not ready
28+
retryAfter = !initializedFn()
29+
}
30+
31+
if !retryAfter {
32+
handler.ServeHTTP(w, req)
33+
return
34+
}
35+
36+
// Return a 429 status asking the client to try again after 5 seconds
37+
w.Header().Set("Retry-After", "5")
38+
http.Error(w, "The apiserver hasn't been fully initialized yet, please try again later.", http.StatusTooManyRequests)
39+
})
40+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ func (c *Config) newIsTerminatingFunc() func() bool {
3737
}
3838
}
3939
}
40+
41+
func (c *Config) newServerFullyInitializedFunc() func() bool {
42+
return func() bool {
43+
select {
44+
case <-c.lifecycleSignals.HasBeenReady.Signaled():
45+
return true
46+
default:
47+
return false
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)