| 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 |
"context" |
| 21 |
"net/http" |
| 22 |
) |
| 23 |
|
| 24 |
type muxAndDiscoveryIncompleteKeyType int |
| 25 |
|
| 26 |
const ( |
| 27 |
// muxAndDiscoveryIncompleteKey is a key under which a protection signal for all requests made before the server have installed all known HTTP paths is stored in the request's context |
| 28 |
muxAndDiscoveryIncompleteKey muxAndDiscoveryIncompleteKeyType = iota |
| 29 |
) |
| 30 |
|
| 31 |
// NoMuxAndDiscoveryIncompleteKey checks if the context contains muxAndDiscoveryIncompleteKey. |
| 32 |
// The presence of the key indicates the request has been made when the HTTP paths weren't installed. |
| 33 |
func NoMuxAndDiscoveryIncompleteKey(ctx context.Context) bool { |
| 34 |
muxAndDiscoveryCompleteProtectionKeyValue, _ := ctx.Value(muxAndDiscoveryIncompleteKey).(string) |
| 35 |
return len(muxAndDiscoveryCompleteProtectionKeyValue) == 0 |
| 36 |
} |
| 37 |
|
| 38 |
// WithMuxAndDiscoveryComplete puts the muxAndDiscoveryIncompleteKey in the context if a request has been made before muxAndDiscoveryCompleteSignal has been ready. |
| 39 |
// Putting the key protect us from returning a 404 response instead of a 503. |
| 40 |
// It is especially important for controllers like GC and NS since they act on 404s. |
| 41 |
// |
| 42 |
// The presence of the key is checked in the NotFoundHandler (staging/src/k8s.io/apiserver/pkg/util/notfoundhandler/not_found_handler.go) |
| 43 |
// |
| 44 |
// The primary reason this filter exists is to protect from a potential race between the client's requests reaching the NotFoundHandler and the server becoming ready. |
| 45 |
// Without the protection key a request could still get a 404 response when the registered signals changed their status just slightly before reaching the new handler. |
| 46 |
// In that case, the presence of the key will make the handler return a 503 instead of a 404. |
| 47 |
func WithMuxAndDiscoveryComplete(handler http.Handler, muxAndDiscoveryCompleteSignal <-chan struct{}) http.Handler { |
| 48 |
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 49 |
if muxAndDiscoveryCompleteSignal != nil && !isClosed(muxAndDiscoveryCompleteSignal) { |
| 50 |
req = req.WithContext(context.WithValue(req.Context(), muxAndDiscoveryIncompleteKey, "MuxAndDiscoveryInstallationNotComplete")) |
| 51 |
} |
| 52 |
handler.ServeHTTP(w, req) |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
// isClosed is a convenience function that simply check if the given chan has been closed |
| 57 |
func isClosed(ch <-chan struct{}) bool { |
| 58 |
select { |
| 59 |
case <-ch: |
| 60 |
return true |
| 61 |
default: |
| 62 |
return false |
| 63 |
} |
| 64 |
} |