| 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 notfoundhandler |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"net/http" |
| 22 |
|
| 23 |
apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 |
"k8s.io/apimachinery/pkg/runtime" |
| 26 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 27 |
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" |
| 28 |
apirequest "k8s.io/apiserver/pkg/endpoints/request" |
| 29 |
) |
| 30 |
|
| 31 |
// New returns an HTTP handler that is meant to be executed at the end of the delegation chain. |
| 32 |
// It checks if the request have been made before the server has installed all known HTTP paths. |
| 33 |
// In that case it returns a 503 response otherwise it returns a 404. |
| 34 |
// |
| 35 |
// Note that we don't want to add additional checks to the readyz path as it might prevent fixing bricked clusters. |
| 36 |
// This specific handler is meant to "protect" requests that arrive before the paths and handlers are fully initialized. |
| 37 |
func New(serializer runtime.NegotiatedSerializer, isMuxAndDiscoveryCompleteFn func(ctx context.Context) bool) *Handler { |
| 38 |
return &Handler{serializer: serializer, isMuxAndDiscoveryCompleteFn: isMuxAndDiscoveryCompleteFn} |
| 39 |
} |
| 40 |
|
| 41 |
type Handler struct { |
| 42 |
serializer runtime.NegotiatedSerializer |
| 43 |
isMuxAndDiscoveryCompleteFn func(ctx context.Context) bool |
| 44 |
} |
| 45 |
|
| 46 |
func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
| 47 |
if !h.isMuxAndDiscoveryCompleteFn(req.Context()) { |
| 48 |
errMsg := "the request has been made before all known HTTP paths have been installed, please try again" |
| 49 |
err := apierrors.NewServiceUnavailable(errMsg) |
| 50 |
if err.ErrStatus.Details == nil { |
| 51 |
err.ErrStatus.Details = &metav1.StatusDetails{} |
| 52 |
} |
| 53 |
err.ErrStatus.Details.RetryAfterSeconds = int32(5) |
| 54 |
|
| 55 |
gv := schema.GroupVersion{Group: "unknown", Version: "unknown"} |
| 56 |
requestInfo, ok := apirequest.RequestInfoFrom(req.Context()) |
| 57 |
if ok { |
| 58 |
gv.Group = requestInfo.APIGroup |
| 59 |
gv.Version = requestInfo.APIVersion |
| 60 |
} |
| 61 |
responsewriters.ErrorNegotiated(err, h.serializer, gv, rw, req) |
| 62 |
return |
| 63 |
} |
| 64 |
http.NotFound(rw, req) |
| 65 |
} |