blob: 02330e9f3e7c4ea07df7638a60351ce017f0da61 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package discovery
18
19import (
20 "net/http"
21
22 "github.com/emicklei/go-restful"
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.
33type APIGroupHandler struct {
34 serializer runtime.NegotiatedSerializer
35 group metav1.APIGroup
36}
37
38func 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
52func (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.
67func (s *APIGroupHandler) handle(req *restful.Request, resp *restful.Response) {
68 s.ServeHTTP(resp.ResponseWriter, req.Request)
69}
70
71func (s *APIGroupHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
72 responsewriters.WriteObjectNegotiated(s.serializer, schema.GroupVersion{}, w, req, http.StatusOK, &s.group)
73}