提交 49b55f37 编写于 作者: P Piotr Bryk

Merge branch 'master' into cell-word-break

......@@ -15,20 +15,70 @@
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
restful "github.com/emicklei/go-restful"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
const (
Colon = ":"
RequestLogString = "Incoming %s %s %s request from %s:\n%v"
ResponseLogString = "Outcoming response to %s with %d status code"
)
// Web-service filter function used for request and response logging.
func wsLogger(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
remoteAddr := GetRemoteAddr(req)
log.Printf(FormatRequestLog(req, remoteAddr))
chain.ProcessFilter(req, resp)
log.Printf(FormatResponseLog(resp, remoteAddr))
}
// Returns remote address of the request (without port number).
func GetRemoteAddr(req *restful.Request) string {
if strings.Contains(req.Request.RemoteAddr, Colon) {
return strings.Split(req.Request.RemoteAddr, Colon)[0]
} else {
return req.Request.RemoteAddr
}
}
// Formats request log string.
func FormatRequestLog(req *restful.Request, remoteAddr string) string {
reqBody := make([]byte, req.Request.ContentLength)
if req.Request.Body != nil {
req.Request.Body.Read(reqBody)
}
reqURI := ""
if req.Request.URL != nil {
reqURI = req.Request.URL.RequestURI()
}
return fmt.Sprintf(RequestLogString, req.Request.Proto, req.Request.Method,
reqURI, remoteAddr, reqBody)
}
// Formats response log string.
// TODO(maciaszczykm): Display response content too.
func FormatResponseLog(resp *restful.Response, remoteAddr string) string {
return fmt.Sprintf(ResponseLogString, remoteAddr, resp.StatusCode())
}
// Creates a new HTTP handler that handles all requests to the API of the backend.
func CreateHttpApiHandler(client *client.Client) http.Handler {
apiHandler := ApiHandler{client}
wsContainer := restful.NewContainer()
deployWs := new(restful.WebService)
deployWs.Filter(wsLogger)
deployWs.Path("/api/appdeployments").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
......@@ -45,6 +95,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer.Add(deployWs)
replicaSetWs := new(restful.WebService)
replicaSetWs.Filter(wsLogger)
replicaSetWs.Path("/api/replicasets").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
......@@ -70,6 +121,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer.Add(replicaSetWs)
namespacesWs := new(restful.WebService)
namespacesWs.Filter(wsLogger)
namespacesWs.Path("/api/namespaces").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
......@@ -85,6 +137,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer.Add(namespacesWs)
logsWs := new(restful.WebService)
logsWs.Filter(wsLogger)
logsWs.Path("/api/logs").
Produces(restful.MIME_JSON)
logsWs.Route(
......@@ -98,6 +151,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer.Add(logsWs)
eventsWs := new(restful.WebService)
eventsWs.Filter(wsLogger)
eventsWs.Path("/api/events").
Produces(restful.MIME_JSON)
eventsWs.Route(
......
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net/http"
"reflect"
"testing"
restful "github.com/emicklei/go-restful"
)
func TestGetRemoteAddr(t *testing.T) {
cases := []struct {
request *restful.Request
expected string
}{
{
&restful.Request{
Request: &http.Request{
RemoteAddr: "192.168.1.1:8080",
},
},
"192.168.1.1",
},
{
&restful.Request{
Request: &http.Request{
RemoteAddr: "192.168.1.2",
},
},
"192.168.1.2",
},
}
for _, c := range cases {
actual := GetRemoteAddr(c.request)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("GetRemoteAddr(%#v) == %#v, expected %#v", c.request, actual, c.expected)
}
}
}
func TestFormatRequestLog(t *testing.T) {
cases := []struct {
request *restful.Request
remoteAddr string
expected string
}{
{
&restful.Request{
Request: &http.Request{
RemoteAddr: "192.168.1.1:8080",
Proto: "HTTP 1.1",
Method: "GET",
},
},
"192.168.1.1",
fmt.Sprintf(RequestLogString, "HTTP 1.1", "GET", "", "192.168.1.1", "[]"),
},
}
for _, c := range cases {
actual := FormatRequestLog(c.request, c.remoteAddr)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("GetRemoteAddr(%#v, %#v) == %#v, expected %#v", c.request, c.remoteAddr,
actual, c.expected)
}
}
}
func TestFormatResponseLog(t *testing.T) {
cases := []struct {
response *restful.Response
remoteAddr string
expected string
}{
{
&restful.Response{},
"192.168.1.1",
fmt.Sprintf(ResponseLogString, "192.168.1.1", http.StatusOK),
},
}
for _, c := range cases {
actual := FormatResponseLog(c.response, c.remoteAddr)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("GetRemoteAddr(%#v, %#v) == %#v, expected %#v", c.response, c.remoteAddr,
actual, c.expected)
}
}
}
......@@ -134,7 +134,7 @@ func TestGetReplicaSetList(t *testing.T) {
},
},
[]api.Pod{
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-1",
Labels: map[string]string{"app": "my-name-1"},
......@@ -143,7 +143,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase: api.PodFailed,
},
},
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-1",
Labels: map[string]string{"app": "my-name-1"},
......@@ -152,7 +152,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase: api.PodFailed,
},
},
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-1",
Labels: map[string]string{"app": "my-name-1"},
......@@ -161,7 +161,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase: api.PodPending,
},
},
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-2",
Labels: map[string]string{"app": "my-name-1"},
......@@ -170,7 +170,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase: api.PodPending,
},
},
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-1",
Labels: map[string]string{"app": "my-name-1"},
......@@ -179,7 +179,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase: api.PodRunning,
},
},
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-1",
Labels: map[string]string{"app": "my-name-1"},
......@@ -188,7 +188,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase: api.PodSucceeded,
},
},
api.Pod{
{
ObjectMeta: api.ObjectMeta{
Namespace: "namespace-1",
Labels: map[string]string{"app": "my-name-1"},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册