Merge pull request #1169 from nathanleclaire/system_con_fix

Fix system container bug
This commit is contained in:
Nathan LeClaire 2017-02-09 15:38:16 -08:00 committed by GitHub
commit a067dfe6d3
2 changed files with 8 additions and 43 deletions

View File

@ -52,10 +52,6 @@ func dockerHTTPGet(ctx context.Context, url string) (*http.Response, error) {
Transport: &UnixSocketRoundTripper{},
}
return dockerHTTPGetWithClient(ctx, url, client)
}
func dockerHTTPGetWithClient(ctx context.Context, url string, client *http.Client) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
@ -73,24 +69,11 @@ func dockerHTTPGetWithClient(ctx context.Context, url string, client *http.Clien
}
return resp, err
}
// UnixSocketRoundTripper provides a way to make HTTP request to Docker socket
// directly.
type UnixSocketRoundTripper struct {
Stream bool
conn *httputil.ClientConn
}
// Close will close the connection if the caller needs to clean up after
// themselves in a streaming request.
func (u UnixSocketRoundTripper) Close() error {
if u.conn != nil {
return u.conn.Close()
}
return nil
}
type UnixSocketRoundTripper struct{}
// RoundTrip dials the Docker UNIX socket to make a HTTP request.
func (u UnixSocketRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
@ -98,16 +81,9 @@ func (u UnixSocketRoundTripper) RoundTrip(req *http.Request) (*http.Response, er
if err != nil {
return nil, err
}
u.conn = httputil.NewClientConn(dial, nil)
conn := httputil.NewClientConn(dial, nil)
// If the client makes a streaming request (e.g., /container/x/logs)
// it's their responsibility to close the connection, because it needs
// to remain open to stream the response body.
if !u.Stream {
defer u.conn.Close()
}
return u.conn.Do(req)
return conn.Do(req)
}
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints

View File

@ -7,7 +7,6 @@ import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
const (
@ -27,7 +26,10 @@ func (s SystemContainerCapturer) Capture(parentCtx context.Context, w *tar.Write
errCh := make(chan error)
go func() {
resp, err := dockerHTTPGet(ctx, "/containers/json?all=1&label="+systemContainerLabel)
// URL encoded.
// Reads more like this:
// /v1.25/containers/json?all=1&filters={"label":{"com.docker.editions.system":true}}
resp, err := dockerHTTPGet(ctx, "/containers/json?all=1&filters=%7B%22label%22%3A%7B%22"+systemContainerLabel+"%22%3Atrue%7D%7D")
if err != nil {
errCh <- err
return
@ -46,15 +48,7 @@ func (s SystemContainerCapturer) Capture(parentCtx context.Context, w *tar.Write
}
for _, c := range names {
transport := &UnixSocketRoundTripper{
Stream: true,
}
client := &http.Client{
Transport: transport,
}
resp, err := dockerHTTPGetWithClient(ctx, "/containers/"+c.ID+"/logs?stderr=1&stdout=1&timestamps=1&tail=all", client)
resp, err := dockerHTTPGet(ctx, "/containers/"+c.ID+"/logs?stderr=1&stdout=1&timestamps=1&tail=all")
if err != nil {
log.Println("ERROR (get request):", err)
continue
@ -62,11 +56,6 @@ func (s SystemContainerCapturer) Capture(parentCtx context.Context, w *tar.Write
defer resp.Body.Close()
// logs makes streaming request where the original http
// conn is left open so we must clean up after
// ourselves when we're done reading
defer transport.Close()
logLines, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ERROR (reading response):", err)