Support HTTP debug in ctr

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko
2021-03-22 09:32:34 -07:00
parent 4e919ffaba
commit 22ef69d77d
7 changed files with 104 additions and 37 deletions

View File

@@ -22,10 +22,15 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
"net/http/httputil"
"strings"
"github.com/containerd/console"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
"github.com/containerd/containerd/remotes/docker/config"
@@ -96,6 +101,16 @@ func GetResolver(ctx gocontext.Context, clicontext *cli.Context) (remotes.Resolv
hostOptions.HostDir = config.HostDirFromRoot(hostDir)
}
if clicontext.Bool("http-dump") {
hostOptions.UpdateClient = func(client *http.Client) error {
client.Transport = &DebugTransport{
transport: client.Transport,
writer: log.G(ctx).Writer(),
}
return nil
}
}
options.Hosts = config.ConfigureHosts(ctx, hostOptions)
return docker.NewResolver(options), nil
@@ -135,3 +150,57 @@ func resolverDefaultTLS(clicontext *cli.Context) (*tls.Config, error) {
return config, nil
}
// DebugTransport wraps the underlying http.RoundTripper interface and dumps all requests/responses to the writer.
type DebugTransport struct {
transport http.RoundTripper
writer io.Writer
}
// RoundTrip dumps request/responses and executes the request using the underlying transport.
func (t DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
in, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, errors.Wrap(err, "failed to dump request")
}
if _, err := t.writer.Write(in); err != nil {
return nil, err
}
resp, err := t.transport.RoundTrip(req)
if err != nil {
return nil, err
}
out, err := httputil.DumpResponse(resp, true)
if err != nil {
return nil, errors.Wrap(err, "failed to dump response")
}
if _, err := t.writer.Write(out); err != nil {
return nil, err
}
return resp, err
}
// NewDebugClientTrace returns a Go http trace client predefined to write DNS and connection
// information to the log. This is used via the --http-trace flag on push and pull operations in ctr.
func NewDebugClientTrace(ctx gocontext.Context) *httptrace.ClientTrace {
return &httptrace.ClientTrace{
DNSStart: func(dnsInfo httptrace.DNSStartInfo) {
log.G(ctx).WithField("host", dnsInfo.Host).Debugf("DNS lookup")
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
if dnsInfo.Err != nil {
log.G(ctx).WithField("lookup_err", dnsInfo.Err).Debugf("DNS lookup error")
} else {
log.G(ctx).WithField("result", dnsInfo.Addrs[0].String()).WithField("coalesced", dnsInfo.Coalesced).Debugf("DNS lookup complete")
}
},
GotConn: func(connInfo httptrace.GotConnInfo) {
log.G(ctx).WithField("reused", connInfo.Reused).WithField("remote_addr", connInfo.Conn.RemoteAddr().String()).Debugf("Connection successful")
},
}
}