mirror of https://github.com/k3s-io/k3s
Merge pull request #29981 from ping035627/ping035627-patch-0803
Automatic merge from submit-queue Implement restful for InstallLogsSupport The PR implement restful for InstallLogsSupport in apiserver.go.pull/6/head
commit
c24c1eedf2
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"swaggerVersion": "1.2",
|
||||
"apiVersion": "",
|
||||
"basePath": "https://10.10.10.10:6443",
|
||||
"resourcePath": "/logs",
|
||||
"apis": [
|
||||
{
|
||||
"path": "/logs/{logpath}",
|
||||
"description": "get log files",
|
||||
"operations": [
|
||||
{
|
||||
"type": "void",
|
||||
"method": "GET",
|
||||
"nickname": "logFileHandler",
|
||||
"parameters": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": "/logs",
|
||||
"description": "get log files",
|
||||
"operations": [
|
||||
{
|
||||
"type": "void",
|
||||
"method": "GET",
|
||||
"nickname": "logFileListHandler",
|
||||
"parameters": []
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"models": {}
|
||||
}
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"swaggerVersion": "1.2",
|
||||
"apis": [
|
||||
{
|
||||
"path": "/logs",
|
||||
"description": "get log files"
|
||||
},
|
||||
{
|
||||
"path": "/version",
|
||||
"description": "git code version from which this is built"
|
||||
|
|
|
@ -104,6 +104,7 @@ curl -w "\n" -fs "${SWAGGER_API_PATH}" > "${SWAGGER_ROOT_DIR}/resourceListing.js
|
|||
curl -w "\n" -fs "${SWAGGER_API_PATH}version" > "${SWAGGER_ROOT_DIR}/version.json"
|
||||
curl -w "\n" -fs "${SWAGGER_API_PATH}api" > "${SWAGGER_ROOT_DIR}/api.json"
|
||||
curl -w "\n" -fs "${SWAGGER_API_PATH}apis" > "${SWAGGER_ROOT_DIR}/apis.json"
|
||||
curl -w "\n" -fs "${SWAGGER_API_PATH}logs" > "${SWAGGER_ROOT_DIR}/logs.json"
|
||||
kube::log::status "SUCCESS"
|
||||
|
||||
# ex: ts=2 sw=2 et filetype=sh
|
||||
|
|
|
@ -208,11 +208,28 @@ func InstallVersionHandler(mux Mux, container *restful.Container) {
|
|||
container.Add(versionWS)
|
||||
}
|
||||
|
||||
// InstallLogsSupport registers the APIServer log support function into a mux.
|
||||
func InstallLogsSupport(mux Mux) {
|
||||
// TODO: use restful: ws.Route(ws.GET("/logs/{logpath:*}").To(fileHandler))
|
||||
// InstallLogsSupport registers the APIServer's `/logs` into a mux.
|
||||
func InstallLogsSupport(mux Mux, container *restful.Container) {
|
||||
// use restful: ws.Route(ws.GET("/logs/{logpath:*}").To(fileHandler))
|
||||
// See github.com/emicklei/go-restful/blob/master/examples/restful-serve-static.go
|
||||
mux.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log/"))))
|
||||
ws := new(restful.WebService)
|
||||
ws.Path("/logs")
|
||||
ws.Doc("get log files")
|
||||
ws.Route(ws.GET("/{logpath:*}").To(logFileHandler))
|
||||
ws.Route(ws.GET("/").To(logFileListHandler))
|
||||
|
||||
container.Add(ws)
|
||||
}
|
||||
|
||||
func logFileHandler(req *restful.Request, resp *restful.Response) {
|
||||
logdir := "/var/log"
|
||||
actual := path.Join(logdir, req.PathParameter("logpath"))
|
||||
http.ServeFile(resp.ResponseWriter, req.Request, actual)
|
||||
}
|
||||
|
||||
func logFileListHandler(req *restful.Request, resp *restful.Response) {
|
||||
logdir := "/var/log"
|
||||
http.ServeFile(resp.ResponseWriter, req.Request, logdir)
|
||||
}
|
||||
|
||||
// TODO: needs to perform response type negotiation, this is probably the wrong way to recover panics
|
||||
|
|
|
@ -1100,6 +1100,29 @@ func TestList(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestLogs(t *testing.T) {
|
||||
handler := handle(map[string]rest.Storage{})
|
||||
server := httptest.NewServer(handler)
|
||||
defer server.Close()
|
||||
client := http.Client{}
|
||||
|
||||
request, err := http.NewRequest("GET", server.URL+"/logs", nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("Data: %s", string(body))
|
||||
}
|
||||
|
||||
func TestErrorList(t *testing.T) {
|
||||
storage := map[string]rest.Storage{}
|
||||
simpleStorage := SimpleRESTStorage{
|
||||
|
|
|
@ -449,7 +449,7 @@ func (s *GenericAPIServer) init(c *Config) {
|
|||
}
|
||||
|
||||
if c.EnableLogsSupport {
|
||||
apiserver.InstallLogsSupport(s.MuxHelper)
|
||||
apiserver.InstallLogsSupport(s.MuxHelper, s.HandlerContainer)
|
||||
}
|
||||
if c.EnableUISupport {
|
||||
ui.InstallSupport(s.MuxHelper, s.enableSwaggerSupport && s.enableSwaggerUI)
|
||||
|
|
Loading…
Reference in New Issue