mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-23 02:51:55 +00:00
Merge pull request #1169 from nathanleclaire/system_con_fix
Fix system container bug
This commit is contained in:
commit
a067dfe6d3
@ -52,10 +52,6 @@ func dockerHTTPGet(ctx context.Context, url string) (*http.Response, error) {
|
|||||||
Transport: &UnixSocketRoundTripper{},
|
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)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -73,24 +69,11 @@ func dockerHTTPGetWithClient(ctx context.Context, url string, client *http.Clien
|
|||||||
}
|
}
|
||||||
|
|
||||||
return resp, err
|
return resp, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnixSocketRoundTripper provides a way to make HTTP request to Docker socket
|
// UnixSocketRoundTripper provides a way to make HTTP request to Docker socket
|
||||||
// directly.
|
// directly.
|
||||||
type UnixSocketRoundTripper struct {
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// RoundTrip dials the Docker UNIX socket to make a HTTP request.
|
// RoundTrip dials the Docker UNIX socket to make a HTTP request.
|
||||||
func (u UnixSocketRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
return conn.Do(req)
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints
|
// Listen starts the HTTPDiagnosticListener and sets up handlers for its endpoints
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -27,7 +26,10 @@ func (s SystemContainerCapturer) Capture(parentCtx context.Context, w *tar.Write
|
|||||||
errCh := make(chan error)
|
errCh := make(chan error)
|
||||||
|
|
||||||
go func() {
|
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 {
|
if err != nil {
|
||||||
errCh <- err
|
errCh <- err
|
||||||
return
|
return
|
||||||
@ -46,15 +48,7 @@ func (s SystemContainerCapturer) Capture(parentCtx context.Context, w *tar.Write
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range names {
|
for _, c := range names {
|
||||||
transport := &UnixSocketRoundTripper{
|
resp, err := dockerHTTPGet(ctx, "/containers/"+c.ID+"/logs?stderr=1&stdout=1×tamps=1&tail=all")
|
||||||
Stream: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{
|
|
||||||
Transport: transport,
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := dockerHTTPGetWithClient(ctx, "/containers/"+c.ID+"/logs?stderr=1&stdout=1×tamps=1&tail=all", client)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR (get request):", err)
|
log.Println("ERROR (get request):", err)
|
||||||
continue
|
continue
|
||||||
@ -62,11 +56,6 @@ func (s SystemContainerCapturer) Capture(parentCtx context.Context, w *tar.Write
|
|||||||
|
|
||||||
defer resp.Body.Close()
|
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)
|
logLines, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR (reading response):", err)
|
log.Println("ERROR (reading response):", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user