From f3f5d0200c867b8cba07373e2876024c802c3928 Mon Sep 17 00:00:00 2001 From: jhadvig Date: Wed, 17 Sep 2014 21:00:09 +0200 Subject: [PATCH] Using podID+containerName for querying the logs --- pkg/kubelet/kubelet.go | 13 +++++++++++-- pkg/kubelet/server.go | 31 +++++++++++++++++++++++-------- pkg/kubelet/server_test.go | 6 +++--- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index d6e342454e5..a75d12e1067 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -753,8 +753,17 @@ func (kl *Kubelet) statsFromContainerPath(containerPath string, req *info.Contai } // GetKubeletContainerLogs returns logs from the container -func (kl *Kubelet) GetKubeletContainerLogs(containerID, tail string, follow bool, writer io.Writer) error { - return dockertools.GetKubeletDockerContainerLogs(kl.dockerClient, containerID, tail , follow, writer) +func (kl *Kubelet) GetKubeletContainerLogs(podFullName, containerName, tail string, follow bool, writer io.Writer) error { + dockerContainers, err := dockertools.GetKubeletDockerContainers(kl.dockerClient) + if err != nil { + return err + } + var uuid string + dockerContainer, found, _ := dockerContainers.FindPodContainer(podFullName, uuid, containerName) + if !found { + return fmt.Errorf("container not found (%s)\n", containerName) + } + return dockertools.GetKubeletDockerContainerLogs(kl.dockerClient, dockerContainer.ID, tail , follow, writer) } // GetPodInfo returns information from Docker about the containers in a pod diff --git a/pkg/kubelet/server.go b/pkg/kubelet/server.go index 0174de37341..077640ab486 100644 --- a/pkg/kubelet/server.go +++ b/pkg/kubelet/server.go @@ -68,7 +68,7 @@ type HostInterface interface { GetMachineInfo() (*info.MachineInfo, error) GetPodInfo(name, uuid string) (api.PodInfo, error) RunInContainer(name, uuid, container string, cmd []string) ([]byte, error) - GetKubeletContainerLogs(containerID, tail string, follow bool, writer io.Writer) error + GetKubeletContainerLogs(podFullName, containerName, tail string, follow bool, writer io.Writer) error ServeLogs(w http.ResponseWriter, req *http.Request) } @@ -93,7 +93,7 @@ func (s *Server) InstallDefaultHandlers() { s.mux.HandleFunc("/logs/", s.handleLogs) s.mux.HandleFunc("/spec/", s.handleSpec) s.mux.HandleFunc("/run/", s.handleRun) - s.mux.HandleFunc("/containerLogs", s.handleContainerLogs) + s.mux.HandleFunc("/containerLogs/", s.handleContainerLogs) } // error serializes an error object into an HTTP response. @@ -153,24 +153,39 @@ func (s *Server) handleContainerLogs(w http.ResponseWriter, req *http.Request) { s.error(w, err) return } + parts := strings.Split(u.Path, "/") - uriValues := u.Query() - containerID := uriValues.Get("containerid") - if len(containerID) == 0 { - http.Error(w, `{"message": "Missing containerID= query entry."}`, http.StatusBadRequest) + var podID, containerName string + if len(parts) == 4 { + podID = parts[2] + containerName = parts[3] + } else { + http.Error(w, "Unexpected path for command running", http.StatusBadRequest) return } + + if len(podID) == 0 { + http.Error(w, `{"message": "Missing podID."}`, http.StatusBadRequest) + return + } + if len(containerName) == 0 { + http.Error(w, `{"message": "Missing container name."}`, http.StatusBadRequest) + return + } + + uriValues := u.Query() follow, _ := strconv.ParseBool(uriValues.Get("follow")) tail := uriValues.Get("tail") + podFullName := GetPodFullName(&Pod{Name: podID, Namespace: "etcd"}) + fw := FlushWriter{writer: w} if flusher, ok := w.(http.Flusher); ok { fw.flusher = flusher } - w.Header().Set("Transfer-Encoding", "chunked") w.WriteHeader(http.StatusOK) - err = s.host.GetKubeletContainerLogs(containerID, tail, follow, &fw) + err = s.host.GetKubeletContainerLogs(podFullName, containerName, tail, follow, &fw) if err != nil { s.error(w, err) return diff --git a/pkg/kubelet/server_test.go b/pkg/kubelet/server_test.go index 8c7abe410f6..2f07bc9d72c 100644 --- a/pkg/kubelet/server_test.go +++ b/pkg/kubelet/server_test.go @@ -42,7 +42,7 @@ type fakeKubelet struct { machineInfoFunc func() (*info.MachineInfo, error) logFunc func(w http.ResponseWriter, req *http.Request) runFunc func(podFullName, uuid, containerName string, cmd []string) ([]byte, error) - containerLogsFunc func(containerID, tail string, follow bool, writer io.Writer) error + containerLogsFunc func(podFullName, containerName, tail string, follow bool, writer io.Writer) error } func (fk *fakeKubelet) GetPodInfo(name, uuid string) (api.PodInfo, error) { @@ -65,8 +65,8 @@ func (fk *fakeKubelet) ServeLogs(w http.ResponseWriter, req *http.Request) { fk.logFunc(w, req) } -func (fk *fakeKubelet) GetKubeletContainerLogs(containerID, tail string, follow bool, writer io.Writer) error { - return fk.containerLogsFunc(containerID, tail, follow, writer) +func (fk *fakeKubelet) GetKubeletContainerLogs(podFullName, containerName, tail string, follow bool, writer io.Writer) error { + return fk.containerLogsFunc(podFullName, containerName, tail, follow, writer) } func (fk *fakeKubelet) RunInContainer(podFullName, uuid, containerName string, cmd []string) ([]byte, error) {