| 1 |
/* |
| 2 |
Copyright 2014 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 routes |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
|
| 22 |
"github.com/emicklei/go-restful/v3" |
| 23 |
|
| 24 |
"k8s.io/apimachinery/pkg/version" |
| 25 |
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" |
| 26 |
) |
| 27 |
|
| 28 |
// Version provides a webservice with version information. |
| 29 |
type Version struct { |
| 30 |
Version *version.Info |
| 31 |
} |
| 32 |
|
| 33 |
// Install registers the APIServer's `/version` handler. |
| 34 |
func (v Version) Install(c *restful.Container) { |
| 35 |
if v.Version == nil { |
| 36 |
return |
| 37 |
} |
| 38 |
|
| 39 |
// Set up a service to return the git code version. |
| 40 |
versionWS := new(restful.WebService) |
| 41 |
versionWS.Path("/version") |
| 42 |
versionWS.Doc("get the version information for this server.") |
| 43 |
versionWS.Route( |
| 44 |
versionWS.GET("/").To(v.handleVersion). |
| 45 |
Doc("get the version information for this server"). |
| 46 |
Operation("getCodeVersion"). |
| 47 |
Produces(restful.MIME_JSON). |
| 48 |
Consumes(restful.MIME_JSON). |
| 49 |
Writes(version.Info{})) |
| 50 |
|
| 51 |
c.Add(versionWS) |
| 52 |
} |
| 53 |
|
| 54 |
// handleVersion writes the server's version information. |
| 55 |
func (v Version) handleVersion(req *restful.Request, resp *restful.Response) { |
| 56 |
responsewriters.WriteRawJSON(http.StatusOK, *v.Version, resp.ResponseWriter) |
| 57 |
} |