Debian Codegraph @

pkg/server/handler.go

raw
1 /*
2 Copyright 2017 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 import (
20 "bytes"
21 "fmt"
22 "net/http"
23 rt "runtime"
24 "sort"
25 "strings"
26
27 "github.com/emicklei/go-restful/v3"
28 "k8s.io/klog/v2"
29
30 apierrors "k8s.io/apimachinery/pkg/api/errors"
31 "k8s.io/apimachinery/pkg/runtime"
32 "k8s.io/apimachinery/pkg/runtime/schema"
33 "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
34 "k8s.io/apiserver/pkg/server/mux"
35 )
36
37 // APIServerHandlers holds the different http.Handlers used by the API server.
38 // This includes the full handler chain, the director (which chooses between gorestful and nonGoRestful,
39 // the gorestful handler (used for the API) which falls through to the nonGoRestful handler on unregistered paths,
40 // and the nonGoRestful handler (which can contain a fallthrough of its own)
41 // FullHandlerChain -> Director -> {GoRestfulContainer,NonGoRestfulMux} based on inspection of registered web services
42 type APIServerHandler struct {
43 // FullHandlerChain is the one that is eventually served with. It should include the full filter
44 // chain and then call the Director.
45 FullHandlerChain http.Handler
46 // The registered APIs. InstallAPIs uses this. Other servers probably shouldn't access this directly.
47 GoRestfulContainer *restful.Container
48 // NonGoRestfulMux is the final HTTP handler in the chain.
49 // It comes after all filters and the API handling
50 // This is where other servers can attach handler to various parts of the chain.
51 NonGoRestfulMux *mux.PathRecorderMux
52
53 // Director is here so that we can properly handle fall through and proxy cases.
54 // This looks a bit bonkers, but here's what's happening. We need to have /apis handling registered in gorestful in order to have
55 // swagger generated for compatibility. Doing that with `/apis` as a webservice, means that it forcibly 404s (no defaulting allowed)
56 // all requests which are not /apis or /apis/. We need those calls to fall through behind gorestful for proper delegation. Trying to
57 // register for a pattern which includes everything behind it doesn't work because gorestful negotiates for verbs and content encoding
58 // and all those things go crazy when gorestful really just needs to pass through. In addition, openapi enforces unique verb constraints
59 // which we don't fit into and it still muddies up swagger. Trying to switch the webservices into a route doesn't work because the
60 // containing webservice faces all the same problems listed above.
61 // This leads to the crazy thing done here. Our mux does what we need, so we'll place it in front of gorestful. It will introspect to
62 // decide if the route is likely to be handled by gorestful and route there if needed. Otherwise, it goes to NonGoRestfulMux mux in
63 // order to handle "normal" paths and delegation. Hopefully no API consumers will ever have to deal with this level of detail. I think
64 // we should consider completely removing gorestful.
65 // Other servers should only use this opaquely to delegate to an API server.
66 Director http.Handler
67 }
68
69 // HandlerChainBuilderFn is used to wrap the GoRestfulContainer handler using the provided handler chain.
70 // It is normally used to apply filtering like authentication and authorization
71 type HandlerChainBuilderFn func(apiHandler http.Handler) http.Handler
72
73 func NewAPIServerHandler(name string, s runtime.NegotiatedSerializer, handlerChainBuilder HandlerChainBuilderFn, notFoundHandler http.Handler) *APIServerHandler {
74 nonGoRestfulMux := mux.NewPathRecorderMux(name)
75 if notFoundHandler != nil {
76 nonGoRestfulMux.NotFoundHandler(notFoundHandler)
77 }
78
79 gorestfulContainer := restful.NewContainer()
80 gorestfulContainer.Router(restful.CurlyRouter{}) // e.g. for proxy/{kind}/{name}/{*}
81 gorestfulContainer.RecoverHandler(func(panicReason interface{}, httpWriter http.ResponseWriter) {
82 logStackOnRecover(s, panicReason, httpWriter)
83 })
84 gorestfulContainer.ServiceErrorHandler(func(serviceErr restful.ServiceError, request *restful.Request, response *restful.Response) {
85 serviceErrorHandler(s, serviceErr, request, response)
86 })
87
88 director := director{
89 name: name,
90 goRestfulContainer: gorestfulContainer,
91 nonGoRestfulMux: nonGoRestfulMux,
92 }
93
94 return &APIServerHandler{
95 FullHandlerChain: handlerChainBuilder(director),
96 GoRestfulContainer: gorestfulContainer,
97 NonGoRestfulMux: nonGoRestfulMux,
98 Director: director,
99 }
100 }
101
102 // ListedPaths returns the paths that should be shown under /
103 func (a *APIServerHandler) ListedPaths() []string {
104 var handledPaths []string
105 // Extract the paths handled using restful.WebService
106 for _, ws := range a.GoRestfulContainer.RegisteredWebServices() {
107 handledPaths = append(handledPaths, ws.RootPath())
108 }
109 handledPaths = append(handledPaths, a.NonGoRestfulMux.ListedPaths()...)
110 sort.Strings(handledPaths)
111
112 return handledPaths
113 }
114
115 type director struct {
116 name string
117 goRestfulContainer *restful.Container
118 nonGoRestfulMux *mux.PathRecorderMux
119 }
120
121 func (d director) ServeHTTP(w http.ResponseWriter, req *http.Request) {
122 path := req.URL.Path
123
124 // check to see if our webservices want to claim this path
125 for _, ws := range d.goRestfulContainer.RegisteredWebServices() {
126 switch {
127 case ws.RootPath() == "/apis":
128 // if we are exactly /apis or /apis/, then we need special handling in loop.
129 // normally these are passed to the nonGoRestfulMux, but if discovery is enabled, it will go directly.
130 // We can't rely on a prefix match since /apis matches everything (see the big comment on Director above)
131 if path == "/apis" || path == "/apis/" {
132 klog.V(5).Infof("%v: %v %q satisfied by gorestful with webservice %v", d.name, req.Method, path, ws.RootPath())
133 // don't use servemux here because gorestful servemuxes get messed up when removing webservices
134 // TODO fix gorestful, remove TPRs, or stop using gorestful
135 d.goRestfulContainer.Dispatch(w, req)
136 return
137 }
138
139 case strings.HasPrefix(path, ws.RootPath()):
140 // ensure an exact match or a path boundary match
141 if len(path) == len(ws.RootPath()) || path[len(ws.RootPath())] == '/' {
142 klog.V(5).Infof("%v: %v %q satisfied by gorestful with webservice %v", d.name, req.Method, path, ws.RootPath())
143 // don't use servemux here because gorestful servemuxes get messed up when removing webservices
144 // TODO fix gorestful, remove TPRs, or stop using gorestful
145 d.goRestfulContainer.Dispatch(w, req)
146 return
147 }
148 }
149 }
150
151 // if we didn't find a match, then we just skip gorestful altogether
152 klog.V(5).Infof("%v: %v %q satisfied by nonGoRestful", d.name, req.Method, path)
153 d.nonGoRestfulMux.ServeHTTP(w, req)
154 }
155
156 // TODO: Unify with RecoverPanics?
157 func logStackOnRecover(s runtime.NegotiatedSerializer, panicReason interface{}, w http.ResponseWriter) {
158 var buffer bytes.Buffer
159 buffer.WriteString(fmt.Sprintf("recover from panic situation: - %v\r\n", panicReason))
160 for i := 2; ; i++ {
161 _, file, line, ok := rt.Caller(i)
162 if !ok {
163 break
164 }
165 buffer.WriteString(fmt.Sprintf(" %s:%d\r\n", file, line))
166 }
167 klog.Errorln(buffer.String())
168
169 headers := http.Header{}
170 if ct := w.Header().Get("Content-Type"); len(ct) > 0 {
171 headers.Set("Accept", ct)
172 }
173 responsewriters.ErrorNegotiated(apierrors.NewGenericServerResponse(http.StatusInternalServerError, "", schema.GroupResource{}, "", "", 0, false), s, schema.GroupVersion{}, w, &http.Request{Header: headers})
174 }
175
176 func serviceErrorHandler(s runtime.NegotiatedSerializer, serviceErr restful.ServiceError, request *restful.Request, resp *restful.Response) {
177 responsewriters.ErrorNegotiated(
178 apierrors.NewGenericServerResponse(serviceErr.Code, "", schema.GroupResource{}, "", serviceErr.Message, 0, false),
179 s,
180 schema.GroupVersion{},
181 resp,
182 request.Request,
183 )
184 }
185
186 // ServeHTTP makes it an http.Handler
187 func (a *APIServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
188 a.FullHandlerChain.ServeHTTP(w, r)
189 }