Using podID+containerName for querying the logs

This commit is contained in:
jhadvig 2014-09-17 21:00:09 +02:00
parent 6da2653b4a
commit f3f5d0200c
3 changed files with 37 additions and 13 deletions

View File

@ -753,8 +753,17 @@ func (kl *Kubelet) statsFromContainerPath(containerPath string, req *info.Contai
} }
// GetKubeletContainerLogs returns logs from the container // GetKubeletContainerLogs returns logs from the container
func (kl *Kubelet) GetKubeletContainerLogs(containerID, tail string, follow bool, writer io.Writer) error { func (kl *Kubelet) GetKubeletContainerLogs(podFullName, containerName, tail string, follow bool, writer io.Writer) error {
return dockertools.GetKubeletDockerContainerLogs(kl.dockerClient, containerID, tail , follow, writer) 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 // GetPodInfo returns information from Docker about the containers in a pod

View File

@ -68,7 +68,7 @@ type HostInterface interface {
GetMachineInfo() (*info.MachineInfo, error) GetMachineInfo() (*info.MachineInfo, error)
GetPodInfo(name, uuid string) (api.PodInfo, error) GetPodInfo(name, uuid string) (api.PodInfo, error)
RunInContainer(name, uuid, container string, cmd []string) ([]byte, 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) ServeLogs(w http.ResponseWriter, req *http.Request)
} }
@ -93,7 +93,7 @@ func (s *Server) InstallDefaultHandlers() {
s.mux.HandleFunc("/logs/", s.handleLogs) s.mux.HandleFunc("/logs/", s.handleLogs)
s.mux.HandleFunc("/spec/", s.handleSpec) s.mux.HandleFunc("/spec/", s.handleSpec)
s.mux.HandleFunc("/run/", s.handleRun) 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. // 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) s.error(w, err)
return return
} }
parts := strings.Split(u.Path, "/")
uriValues := u.Query() var podID, containerName string
containerID := uriValues.Get("containerid") if len(parts) == 4 {
if len(containerID) == 0 { podID = parts[2]
http.Error(w, `{"message": "Missing containerID= query entry."}`, http.StatusBadRequest) containerName = parts[3]
} else {
http.Error(w, "Unexpected path for command running", http.StatusBadRequest)
return 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")) follow, _ := strconv.ParseBool(uriValues.Get("follow"))
tail := uriValues.Get("tail") tail := uriValues.Get("tail")
podFullName := GetPodFullName(&Pod{Name: podID, Namespace: "etcd"})
fw := FlushWriter{writer: w} fw := FlushWriter{writer: w}
if flusher, ok := w.(http.Flusher); ok { if flusher, ok := w.(http.Flusher); ok {
fw.flusher = flusher fw.flusher = flusher
} }
w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
err = s.host.GetKubeletContainerLogs(containerID, tail, follow, &fw) err = s.host.GetKubeletContainerLogs(podFullName, containerName, tail, follow, &fw)
if err != nil { if err != nil {
s.error(w, err) s.error(w, err)
return return

View File

@ -42,7 +42,7 @@ type fakeKubelet struct {
machineInfoFunc func() (*info.MachineInfo, error) machineInfoFunc func() (*info.MachineInfo, error)
logFunc func(w http.ResponseWriter, req *http.Request) logFunc func(w http.ResponseWriter, req *http.Request)
runFunc func(podFullName, uuid, containerName string, cmd []string) ([]byte, error) 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) { 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) fk.logFunc(w, req)
} }
func (fk *fakeKubelet) GetKubeletContainerLogs(containerID, tail string, follow bool, writer io.Writer) error { func (fk *fakeKubelet) GetKubeletContainerLogs(podFullName, containerName, tail string, follow bool, writer io.Writer) error {
return fk.containerLogsFunc(containerID, tail, follow, writer) return fk.containerLogsFunc(podFullName, containerName, tail, follow, writer)
} }
func (fk *fakeKubelet) RunInContainer(podFullName, uuid, containerName string, cmd []string) ([]byte, error) { func (fk *fakeKubelet) RunInContainer(podFullName, uuid, containerName string, cmd []string) ([]byte, error) {