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 | "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 | utilnet "k8s.io/apimachinery/pkg/util/net" |
| 28 | "k8s.io/apiserver/pkg/endpoints/handlers/negotiation" |
| 29 | "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" |
| 30 | ) |
| 31 | |
| 32 | // legacyRootAPIHandler creates a webservice serving api group discovery. |
| 33 | type legacyRootAPIHandler struct { |
| 34 | // addresses is used to build cluster IPs for discovery. |
| 35 | addresses Addresses |
| 36 | apiPrefix string |
| 37 | serializer runtime.NegotiatedSerializer |
| 38 | apiVersions []string |
| 39 | } |
| 40 | |
| 41 | func NewLegacyRootAPIHandler(addresses Addresses, serializer runtime.NegotiatedSerializer, apiPrefix string, apiVersions []string) *legacyRootAPIHandler { |
| 42 | // Because in release 1.1, /apis returns response with empty APIVersion, we |
| 43 | // use stripVersionNegotiatedSerializer to keep the response backwards |
| 44 | // compatible. |
| 45 | serializer = stripVersionNegotiatedSerializer{serializer} |
| 46 | |
| 47 | return &legacyRootAPIHandler{ |
| 48 | addresses: addresses, |
| 49 | apiPrefix: apiPrefix, |
| 50 | serializer: serializer, |
| 51 | apiVersions: apiVersions, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // AddApiWebService adds a service to return the supported api versions at the legacy /api. |
| 56 | func (s *legacyRootAPIHandler) WebService() *restful.WebService { |
| 57 | mediaTypes, _ := negotiation.MediaTypesForSerializer(s.serializer) |
| 58 | ws := new(restful.WebService) |
| 59 | ws.Path(s.apiPrefix) |
| 60 | ws.Doc("get available API versions") |
| 61 | ws.Route(ws.GET("/").To(s.handle). |
| 62 | Doc("get available API versions"). |
| 63 | Operation("getAPIVersions"). |
| 64 | Produces(mediaTypes...). |
| 65 | Consumes(mediaTypes...). |
| 66 | Writes(metav1.APIVersions{})) |
| 67 | return ws |
| 68 | } |
| 69 | |
| 70 | func (s *legacyRootAPIHandler) handle(req *restful.Request, resp *restful.Response) { |
| 71 | clientIP := utilnet.GetClientIP(req.Request) |
| 72 | apiVersions := &metav1.APIVersions{ |
| 73 | ServerAddressByClientCIDRs: s.addresses.ServerAddressByClientCIDRs(clientIP), |
| 74 | Versions: s.apiVersions, |
| 75 | } |
| 76 | |
| 77 | responsewriters.WriteObjectNegotiated(s.serializer, schema.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, apiVersions) |
| 78 | } |