Skip to content

Commit

Permalink
UPSTREAM: <carry>: change opt-in due to upstream revert
Browse files Browse the repository at this point in the history
openshift-rebase(v1.24):source=4954e48523c
  • Loading branch information
tkashem authored and soltysh committed Aug 22, 2022
1 parent 2737706 commit 24a7dab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiserver/pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
if c.CompressionDisabledFunc != nil {
handler = genericapifilters.WithCompressionDisabled(handler, c.CompressionDisabledFunc)
}
handler = genericfilters.WithOptInRetryAfter(handler, c.newNotReadyRetryAfterFunc())
handler = genericfilters.WithOptInRetryAfter(handler, c.newServerFullyInitializedFunc())
handler = genericfilters.WithHTTPLogging(handler, c.newIsTerminatingFunc())
if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.APIServerTracing) {
handler = genericapifilters.WithTracing(handler, c.TracerProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,21 @@ import (
"net/http"
)

func WithOptInRetryAfter(handler http.Handler, notReadyRetryAfterFn ShouldRespondWithRetryAfterFunc) http.Handler {
func WithOptInRetryAfter(handler http.Handler, initializedFn func() bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var (
params *RetryAfterParams
sendRetryAfter bool
)
var retryAfter bool
if value := req.Header.Get("X-OpenShift-Internal-If-Not-Ready"); value == "reject" {
// the caller opted in for the request to be rejected if the server is not ready
params, sendRetryAfter = notReadyRetryAfterFn()
retryAfter = !initializedFn()
}

if !sendRetryAfter {
if !retryAfter {
handler.ServeHTTP(w, req)
return
}

// If we are here this means it's time to send Retry-After response
//
// Copied from net/http2 library
// "Connection" headers aren't allowed in HTTP/2 (RFC 7540, 8.1.2.2),
// but respect "Connection" == "close" to mean sending a GOAWAY and tearing
// down the TCP connection notReadyRetryAfterFn idle, like we do for HTTP/1.
if params.TearDownConnection {
w.Header().Set("Connection", "close")
}

// Return a 429 status asking the client to try again after 5 seconds
w.Header().Set("Retry-After", "5")
http.Error(w, params.Message, http.StatusTooManyRequests)
http.Error(w, "The apiserver hasn't been fully initialized yet, please try again later.", http.StatusTooManyRequests)
})
}
16 changes: 4 additions & 12 deletions staging/src/k8s.io/apiserver/pkg/server/patch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ limitations under the License.

package server

import (
genericfilters "k8s.io/apiserver/pkg/server/filters"
)

// newIsTerminatingFunc returns a 'func() bool' that relies on the
// 'ShutdownInitiated' life cycle signal of answer if the apiserver
// has started the termination process.
Expand All @@ -42,17 +38,13 @@ func (c *Config) newIsTerminatingFunc() func() bool {
}
}

func (c *Config) newNotReadyRetryAfterFunc() genericfilters.ShouldRespondWithRetryAfterFunc {
params := &genericfilters.RetryAfterParams{
Message: "The apiserver hasn't been fully initialized yet, please try again later.",
}

return func() (*genericfilters.RetryAfterParams, bool) {
func (c *Config) newServerFullyInitializedFunc() func() bool {
return func() bool {
select {
case <-c.lifecycleSignals.HasBeenReady.Signaled():
return nil, false
return true
default:
return params, true
return false
}
}
}

0 comments on commit 24a7dab

Please sign in to comment.