From 3ef9e404c1408551ec4922ca401ce69193c6da6b Mon Sep 17 00:00:00 2001 From: Vishnu Kannan Date: Sat, 3 Jan 2015 02:17:25 +0000 Subject: [PATCH] Update "/stats" endpoint on kubelet to take namespace as an argument as part of the API. --- pkg/kubelet/server.go | 14 ++++++-------- pkg/kubelet/server_test.go | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/pkg/kubelet/server.go b/pkg/kubelet/server.go index 37207f8074f..f036f0bcc45 100644 --- a/pkg/kubelet/server.go +++ b/pkg/kubelet/server.go @@ -325,7 +325,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { // serveStats implements stats logic. func (s *Server) serveStats(w http.ResponseWriter, req *http.Request) { - // /stats// or /stats/// + // /stats// or /stats//// components := strings.Split(strings.TrimPrefix(path.Clean(req.URL.Path), "/"), "/") var stats *info.ContainerInfo var err error @@ -347,23 +347,21 @@ func (s *Server) serveStats(w http.ResponseWriter, req *http.Request) { // Backward compatibility without uuid information podFullName := GetPodFullName(&api.BoundPod{ ObjectMeta: api.ObjectMeta{ - Name: components[1], - // TODO: I am broken + Name: components[1], Namespace: api.NamespaceDefault, Annotations: map[string]string{ConfigSourceAnnotationKey: "etcd"}, }, }) stats, err = s.host.GetContainerInfo(podFullName, "", components[2], &query) - case 4: + case 5: podFullName := GetPodFullName(&api.BoundPod{ ObjectMeta: api.ObjectMeta{ - Name: components[1], - // TODO: I am broken - Namespace: "", + Name: components[2], + Namespace: components[1], Annotations: map[string]string{ConfigSourceAnnotationKey: "etcd"}, }, }) - stats, err = s.host.GetContainerInfo(podFullName, components[2], components[2], &query) + stats, err = s.host.GetContainerInfo(podFullName, components[3], components[4], &query) default: http.Error(w, "unknown resource.", http.StatusNotFound) return diff --git a/pkg/kubelet/server_test.go b/pkg/kubelet/server_test.go index 5b672bdfed7..3eb22e8b447 100644 --- a/pkg/kubelet/server_test.go +++ b/pkg/kubelet/server_test.go @@ -34,7 +34,7 @@ import ( type fakeKubelet struct { infoFunc func(name string) (api.PodInfo, error) - containerInfoFunc func(podFullName, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) + containerInfoFunc func(podFullName, uid, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) rootInfoFunc func(query *info.ContainerInfoRequest) (*info.ContainerInfo, error) machineInfoFunc func() (*info.MachineInfo, error) boundPodsFunc func() ([]api.BoundPod, error) @@ -48,7 +48,7 @@ func (fk *fakeKubelet) GetPodInfo(name, uuid string) (api.PodInfo, error) { } func (fk *fakeKubelet) GetContainerInfo(podFullName, uuid, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) { - return fk.containerInfoFunc(podFullName, containerName, req) + return fk.containerInfoFunc(podFullName, uuid, containerName, req) } func (fk *fakeKubelet) GetRootInfo(req *info.ContainerInfoRequest) (*info.ContainerInfo, error) { @@ -144,7 +144,7 @@ func TestContainerInfo(t *testing.T) { podID := "somepod" expectedPodID := "somepod" + ".default.etcd" expectedContainerName := "goodcontainer" - fw.fakeKubelet.containerInfoFunc = func(podID, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) { + fw.fakeKubelet.containerInfoFunc = func(podID, uid, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) { if podID != expectedPodID || containerName != expectedContainerName { return nil, fmt.Errorf("bad podID or containerName: podID=%v; containerName=%v", podID, containerName) } @@ -166,6 +166,36 @@ func TestContainerInfo(t *testing.T) { } } +func TestContainerInfoWithUidNamespace(t *testing.T) { + fw := newServerTest() + expectedInfo := &info.ContainerInfo{} + podID := "somepod" + expectedNamespace := "custom" + expectedPodID := "somepod" + "." + expectedNamespace + ".etcd" + expectedContainerName := "goodcontainer" + expectedUid := "9b01b80f-8fb4-11e4-95ab-4200af06647" + fw.fakeKubelet.containerInfoFunc = func(podID, uid, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error) { + if podID != expectedPodID || uid != expectedUid || containerName != expectedContainerName { + return nil, fmt.Errorf("bad podID or uid or containerName: podID=%v; uid=%v; containerName=%v", podID, uid, containerName) + } + return expectedInfo, nil + } + + resp, err := http.Get(fw.testHTTPServer.URL + fmt.Sprintf("/stats/%v/%v/%v/%v", expectedNamespace, podID, expectedUid, expectedContainerName)) + if err != nil { + t.Fatalf("Got error GETing: %v", err) + } + defer resp.Body.Close() + var receivedInfo info.ContainerInfo + err = json.NewDecoder(resp.Body).Decode(&receivedInfo) + if err != nil { + t.Fatalf("received invalid json data: %v", err) + } + if !reflect.DeepEqual(&receivedInfo, expectedInfo) { + t.Errorf("received wrong data: %#v", receivedInfo) + } +} + func TestRootInfo(t *testing.T) { fw := newServerTest() expectedInfo := &info.ContainerInfo{}