Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 | 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 | |
| 31 | type APIResourceLister interface { |
| 32 | ListAPIResources() []metav1.APIResource |
| 33 | } |
| 34 | |
| 35 | type APIResourceListerFunc func() []metav1.APIResource |
| 36 | |
| 37 | func (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. |
| 43 | type APIVersionHandler struct { |
| 44 | serializer runtime.NegotiatedSerializer |
| 45 | |
| 46 | groupVersion schema.GroupVersion |
| 47 | apiResourceLister APIResourceLister |
| 48 | } |
| 49 | |
| 50 | func 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 | |
| 65 | func (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. |
| 76 | func (s *APIVersionHandler) handle(req *restful.Request, resp *restful.Response) { |
| 77 | s.ServeHTTP(resp.ResponseWriter, req.Request) |
| 78 | } |
| 79 | |
| 80 | func (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 | } |