| 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 |
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 |
} |
| 39 |
|
| 40 |
func NewLegacyRootAPIHandler(addresses Addresses, serializer runtime.NegotiatedSerializer, apiPrefix string) *legacyRootAPIHandler { |
| 41 |
// Because in release 1.1, /apis returns response with empty APIVersion, we |
| 42 |
// use stripVersionNegotiatedSerializer to keep the response backwards |
| 43 |
// compatible. |
| 44 |
serializer = stripVersionNegotiatedSerializer{serializer} |
| 45 |
|
| 46 |
return &legacyRootAPIHandler{ |
| 47 |
addresses: addresses, |
| 48 |
apiPrefix: apiPrefix, |
| 49 |
serializer: serializer, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// AddApiWebService adds a service to return the supported api versions at the legacy /api. |
| 54 |
func (s *legacyRootAPIHandler) WebService() *restful.WebService { |
| 55 |
mediaTypes, _ := negotiation.MediaTypesForSerializer(s.serializer) |
| 56 |
ws := new(restful.WebService) |
| 57 |
ws.Path(s.apiPrefix) |
| 58 |
ws.Doc("get available API versions") |
| 59 |
ws.Route(ws.GET("/").To(s.restfulHandle). |
| 60 |
Doc("get available API versions"). |
| 61 |
Operation("getAPIVersions"). |
| 62 |
Produces(mediaTypes...). |
| 63 |
Consumes(mediaTypes...). |
| 64 |
Writes(metav1.APIVersions{})) |
| 65 |
return ws |
| 66 |
} |
| 67 |
|
| 68 |
func (s *legacyRootAPIHandler) restfulHandle(req *restful.Request, resp *restful.Response) { |
| 69 |
s.ServeHTTP(resp.ResponseWriter, req.Request) |
| 70 |
} |
| 71 |
|
| 72 |
func (s *legacyRootAPIHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 73 |
clientIP := utilnet.GetClientIP(req) |
| 74 |
apiVersions := &metav1.APIVersions{ |
| 75 |
ServerAddressByClientCIDRs: s.addresses.ServerAddressByClientCIDRs(clientIP), |
| 76 |
Versions: []string{"v1"}, |
| 77 |
} |
| 78 |
|
| 79 |
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp, req, http.StatusOK, apiVersions, false) |
| 80 |
} |