| 1 |
/* |
| 2 |
Copyright 2022 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 aggregated |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
|
| 22 |
apidiscoveryv2 "k8s.io/api/apidiscovery/v2" |
| 23 |
apidiscoveryv2beta1 "k8s.io/api/apidiscovery/v2beta1" |
| 24 |
"k8s.io/apimachinery/pkg/runtime/serializer" |
| 25 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 26 |
|
| 27 |
"github.com/emicklei/go-restful/v3" |
| 28 |
"k8s.io/apimachinery/pkg/runtime" |
| 29 |
|
| 30 |
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation" |
| 31 |
) |
| 32 |
|
| 33 |
type WrappedHandler struct { |
| 34 |
s runtime.NegotiatedSerializer |
| 35 |
handler http.Handler |
| 36 |
aggHandler http.Handler |
| 37 |
} |
| 38 |
|
| 39 |
func (wrapped *WrappedHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 40 |
mediaType, _ := negotiation.NegotiateMediaTypeOptions(req.Header.Get("Accept"), wrapped.s.SupportedMediaTypes(), DiscoveryEndpointRestrictions) |
| 41 |
// mediaType.Convert looks at the request accept headers and is used to control whether the discovery document will be aggregated. |
| 42 |
if IsAggregatedDiscoveryGVK(mediaType.Convert) { |
| 43 |
wrapped.aggHandler.ServeHTTP(resp, req) |
| 44 |
return |
| 45 |
} |
| 46 |
wrapped.handler.ServeHTTP(resp, req) |
| 47 |
} |
| 48 |
|
| 49 |
func (wrapped *WrappedHandler) restfulHandle(req *restful.Request, resp *restful.Response) { |
| 50 |
wrapped.ServeHTTP(resp.ResponseWriter, req.Request) |
| 51 |
} |
| 52 |
|
| 53 |
func (wrapped *WrappedHandler) GenerateWebService(prefix string, returnType interface{}) *restful.WebService { |
| 54 |
mediaTypes, _ := negotiation.MediaTypesForSerializer(wrapped.s) |
| 55 |
ws := new(restful.WebService) |
| 56 |
ws.Path(prefix) |
| 57 |
ws.Doc("get available API versions") |
| 58 |
ws.Route(ws.GET("/").To(wrapped.restfulHandle). |
| 59 |
Doc("get available API versions"). |
| 60 |
Operation("getAPIVersions"). |
| 61 |
Produces(mediaTypes...). |
| 62 |
Consumes(mediaTypes...). |
| 63 |
Writes(returnType)) |
| 64 |
return ws |
| 65 |
} |
| 66 |
|
| 67 |
// WrapAggregatedDiscoveryToHandler wraps a handler with an option to |
| 68 |
// emit the aggregated discovery by passing in the aggregated |
| 69 |
// discovery type in content negotiation headers: eg: (Accept: |
| 70 |
// application/json;v=v2;g=apidiscovery.k8s.io;as=APIGroupDiscoveryList) |
| 71 |
func WrapAggregatedDiscoveryToHandler(handler http.Handler, aggHandler http.Handler) *WrappedHandler { |
| 72 |
scheme := runtime.NewScheme() |
| 73 |
utilruntime.Must(apidiscoveryv2.AddToScheme(scheme)) |
| 74 |
utilruntime.Must(apidiscoveryv2beta1.AddToScheme(scheme)) |
| 75 |
codecs := serializer.NewCodecFactory(scheme) |
| 76 |
return &WrappedHandler{codecs, handler, aggHandler} |
| 77 |
} |