k8s.io/apiserver@v0.31.1/pkg/endpoints/discovery/aggregated/wrapper.go (about)

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