blob: aadfc7a5b49546a63f250de1a81dd072e19d4dac [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 restful "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
31type APIResourceLister interface {
32 ListAPIResources() []metav1.APIResource
33}
34
35type APIResourceListerFunc func() []metav1.APIResource
36
37func (f APIResourceListerFunc) ListAPIResources() []metav1.APIResource {
38 return f()
39}
40
41// APIVersionHandler creates a webservice serving the supported resources for the version
42// E.g., such a web service will be registered at /apis/extensions/v1beta1.
43type APIVersionHandler struct {
44 serializer runtime.NegotiatedSerializer
45
46 groupVersion schema.GroupVersion
47 apiResourceLister APIResourceLister
48}
49
50func NewAPIVersionHandler(serializer runtime.NegotiatedSerializer, groupVersion schema.GroupVersion, apiResourceLister APIResourceLister) *APIVersionHandler {
51 if keepUnversioned(groupVersion.Group) {
52 // Because in release 1.1, /apis/extensions returns response with empty
53 // APIVersion, we use stripVersionNegotiatedSerializer to keep the
54 // response backwards compatible.
55 serializer = stripVersionNegotiatedSerializer{serializer}
56 }
57
58 return &APIVersionHandler{
59 serializer: serializer,
60 groupVersion: groupVersion,
61 apiResourceLister: apiResourceLister,
62 }
63}
64
65func (s *APIVersionHandler) AddToWebService(ws *restful.WebService) {
66 mediaTypes, _ := negotiation.MediaTypesForSerializer(s.serializer)
67 ws.Route(ws.GET("/").To(s.handle).
68 Doc("get available resources").
69 Operation("getAPIResources").
70 Produces(mediaTypes...).
71 Consumes(mediaTypes...).
72 Writes(metav1.APIResourceList{}))
73}
74
75// handle returns a handler which will return the api.VersionAndVersion of the group.
76func (s *APIVersionHandler) handle(req *restful.Request, resp *restful.Response) {
77 s.ServeHTTP(resp.ResponseWriter, req.Request)
78}
79
80func (s *APIVersionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
81 responsewriters.WriteObjectNegotiated(s.serializer, schema.GroupVersion{}, w, req, http.StatusOK,
82 &metav1.APIResourceList{GroupVersion: s.groupVersion.String(), APIResources: s.apiResourceLister.ListAPIResources()})
83}