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

     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 discovery
    18  
    19  import (
    20  	"net/http"
    21  
    22  	"github.com/emicklei/go-restful/v3"
    23  
    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/negotiation"
    28  	"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
    29  )
    30  
    31  // APIGroupHandler creates a webservice serving the supported versions, preferred version, and name
    32  // of a group. E.g., such a web service will be registered at /apis/extensions.
    33  type APIGroupHandler struct {
    34  	serializer runtime.NegotiatedSerializer
    35  	group      metav1.APIGroup
    36  }
    37  
    38  func NewAPIGroupHandler(serializer runtime.NegotiatedSerializer, group metav1.APIGroup) *APIGroupHandler {
    39  	if keepUnversioned(group.Name) {
    40  		// Because in release 1.1, /apis/extensions returns response with empty
    41  		// APIVersion, we use stripVersionNegotiatedSerializer to keep the
    42  		// response backwards compatible.
    43  		serializer = stripVersionNegotiatedSerializer{serializer}
    44  	}
    45  
    46  	return &APIGroupHandler{
    47  		serializer: serializer,
    48  		group:      group,
    49  	}
    50  }
    51  
    52  func (s *APIGroupHandler) WebService() *restful.WebService {
    53  	mediaTypes, _ := negotiation.MediaTypesForSerializer(s.serializer)
    54  	ws := new(restful.WebService)
    55  	ws.Path(APIGroupPrefix + "/" + s.group.Name)
    56  	ws.Doc("get information of a group")
    57  	ws.Route(ws.GET("/").To(s.handle).
    58  		Doc("get information of a group").
    59  		Operation("getAPIGroup").
    60  		Produces(mediaTypes...).
    61  		Consumes(mediaTypes...).
    62  		Writes(metav1.APIGroup{}))
    63  	return ws
    64  }
    65  
    66  // handle returns a handler which will return the api.GroupAndVersion of the group.
    67  func (s *APIGroupHandler) handle(req *restful.Request, resp *restful.Response) {
    68  	s.ServeHTTP(resp.ResponseWriter, req.Request)
    69  }
    70  
    71  func (s *APIGroupHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    72  	responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, &s.group, false)
    73  }