|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making TKEStack |
| 3 | + * available. |
| 4 | + * |
| 5 | + * Copyright (C) 2012-2021 Tencent. All Rights Reserved. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 8 | + * this file except in compliance with the License. You may obtain a copy of the |
| 9 | + * License at |
| 10 | + * |
| 11 | + * https://opensource.org/licenses/Apache-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OF ANY KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package rest |
| 20 | + |
| 21 | +import ( |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/emicklei/go-restful" |
| 25 | + "k8s.io/client-go/rest" |
| 26 | + platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion" |
| 27 | + "tkestack.io/tke/pkg/platform/proxy" |
| 28 | +) |
| 29 | + |
| 30 | +type VersionProxyHandler struct { |
| 31 | + config *rest.Config |
| 32 | +} |
| 33 | + |
| 34 | +func NewVersionProxyHandler(config *rest.Config) *VersionProxyHandler { |
| 35 | + return &VersionProxyHandler{ |
| 36 | + config: config, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Install install /version route |
| 41 | +// Please confirm /version is not in pkg/apiserver/filter/authentication.go defaultIgnoreAuthPathPrefixes |
| 42 | +func (s *VersionProxyHandler) Install(c *restful.Container) { |
| 43 | + versionWS := new(restful.WebService) |
| 44 | + versionWS.Path("/version") |
| 45 | + versionWS.Doc("git code version from which this is built") |
| 46 | + versionWS.Route( |
| 47 | + versionWS.GET("/").To(s.handle). |
| 48 | + Doc("get the code version"). |
| 49 | + Operation("getVersion"). |
| 50 | + Produces(restful.MIME_JSON). |
| 51 | + Consumes(restful.MIME_JSON)) |
| 52 | + |
| 53 | + c.Add(versionWS) |
| 54 | +} |
| 55 | + |
| 56 | +// /version route process |
| 57 | +func (s *VersionProxyHandler) handle(req *restful.Request, resp *restful.Response) { |
| 58 | + platformClient := platforminternalclient.NewForConfigOrDie(s.config) |
| 59 | + client, err := proxy.ClientSet(req.Request.Context(), platformClient) |
| 60 | + if err != nil { |
| 61 | + resp.WriteHeaderAndEntity(http.StatusBadRequest, err.Error()) |
| 62 | + return |
| 63 | + } |
| 64 | + version, err := client.Discovery().ServerVersion() |
| 65 | + |
| 66 | + if err != nil { |
| 67 | + resp.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error()) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + resp.WriteHeaderAndEntity(http.StatusOK, version) |
| 72 | +} |
0 commit comments