Skip to content

Commit 31a0f92

Browse files
authored
Merge pull request #5234 from mxpv/http-dbg
Support HTTP debug in ctr
2 parents 181e2d4 + 22ef69d commit 31a0f92

7 files changed

Lines changed: 104 additions & 37 deletions

File tree

cmd/ctr/commands/commands.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ var (
7878
Name: "tlskey",
7979
Usage: "path to TLS client key",
8080
},
81+
cli.BoolFlag{
82+
Name: "http-dump",
83+
Usage: "dump all HTTP request/responses when interacting with container registry",
84+
},
85+
cli.BoolFlag{
86+
Name: "http-trace",
87+
Usage: "enable HTTP tracing for registry interactions",
88+
},
8189
}
8290

8391
// ContainerFlags are cli flags specifying container options

cmd/ctr/commands/content/fetch.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
"net/http/httptrace"
2324
"os"
2425
"sync"
2526
"text/tabwriter"
@@ -110,6 +111,8 @@ type FetchConfig struct {
110111
AllMetadata bool
111112
// RemoteOpts is not used by ctr, but can be used by other CLI tools
112113
RemoteOpts []containerd.RemoteOpt
114+
// TraceHTTP writes DNS and connection information to the log when dealing with a container registry
115+
TraceHTTP bool
113116
}
114117

115118
// NewFetchConfig returns the default FetchConfig from cli flags
@@ -119,8 +122,9 @@ func NewFetchConfig(ctx context.Context, clicontext *cli.Context) (*FetchConfig,
119122
return nil, err
120123
}
121124
config := &FetchConfig{
122-
Resolver: resolver,
123-
Labels: clicontext.StringSlice("label"),
125+
Resolver: resolver,
126+
Labels: clicontext.StringSlice("label"),
127+
TraceHTTP: clicontext.Bool("http-trace"),
124128
}
125129
if !clicontext.GlobalBool("debug") {
126130
config.ProgressOutput = os.Stdout
@@ -148,6 +152,10 @@ func NewFetchConfig(ctx context.Context, clicontext *cli.Context) (*FetchConfig,
148152
func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) {
149153
ongoing := NewJobs(ref)
150154

155+
if config.TraceHTTP {
156+
ctx = httptrace.WithClientTrace(ctx, commands.NewDebugClientTrace(ctx))
157+
}
158+
151159
pctx, stopProgress := context.WithCancel(ctx)
152160
progress := make(chan struct{})
153161

cmd/ctr/commands/images/images.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
package images
1818

1919
import (
20-
"context"
2120
"fmt"
22-
"net/http/httptrace"
2321
"os"
2422
"sort"
2523
"strings"
@@ -334,23 +332,3 @@ var removeCommand = cli.Command{
334332
return exitErr
335333
},
336334
}
337-
338-
// NewDebugClientTrace returns a Go http trace client predefined to write DNS and connection
339-
// information to the log. This is used via the --trace flag on push and pull operations in ctr.
340-
func NewDebugClientTrace(ctx context.Context) *httptrace.ClientTrace {
341-
return &httptrace.ClientTrace{
342-
DNSStart: func(dnsInfo httptrace.DNSStartInfo) {
343-
log.G(ctx).WithField("host", dnsInfo.Host).Debugf("DNS lookup")
344-
},
345-
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
346-
if dnsInfo.Err != nil {
347-
log.G(ctx).WithField("lookup_err", dnsInfo.Err).Debugf("DNS lookup error")
348-
} else {
349-
log.G(ctx).WithField("result", dnsInfo.Addrs[0].String()).WithField("coalesced", dnsInfo.Coalesced).Debugf("DNS lookup complete")
350-
}
351-
},
352-
GotConn: func(connInfo httptrace.GotConnInfo) {
353-
log.G(ctx).WithField("reused", connInfo.Reused).WithField("remote_addr", connInfo.Conn.RemoteAddr().String()).Debugf("Connection successful")
354-
},
355-
}
356-
}

cmd/ctr/commands/images/pull.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package images
1818

1919
import (
2020
"fmt"
21-
"net/http/httptrace"
2221
"time"
2322

2423
"github.com/containerd/containerd"
@@ -56,10 +55,6 @@ command. As part of this process, we do the following:
5655
Name: "all-platforms",
5756
Usage: "pull content and metadata from all platforms",
5857
},
59-
cli.BoolFlag{
60-
Name: "trace",
61-
Usage: "enable HTTP tracing for registry interactions",
62-
},
6358
cli.BoolFlag{
6459
Name: "all-metadata",
6560
Usage: "Pull metadata for all platforms",
@@ -94,9 +89,6 @@ command. As part of this process, we do the following:
9489
return err
9590
}
9691

97-
if context.Bool("trace") {
98-
ctx = httptrace.WithClientTrace(ctx, NewDebugClientTrace(ctx))
99-
}
10092
img, err := content.Fetch(ctx, client, ref, config)
10193
if err != nil {
10294
return err

cmd/ctr/commands/images/push.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ var pushCommand = cli.Command{
6060
Name: "manifest-type",
6161
Usage: "media type of manifest digest",
6262
Value: ocispec.MediaTypeImageManifest,
63-
}, cli.BoolFlag{
64-
Name: "trace",
65-
Usage: "enable HTTP tracing for registry interactions",
6663
}, cli.StringSliceFlag{
6764
Name: "platform",
6865
Usage: "push content from a specific platform",
@@ -123,8 +120,8 @@ var pushCommand = cli.Command{
123120
}
124121
}
125122

126-
if context.Bool("trace") {
127-
ctx = httptrace.WithClientTrace(ctx, NewDebugClientTrace(ctx))
123+
if context.Bool("http-trace") {
124+
ctx = httptrace.WithClientTrace(ctx, commands.NewDebugClientTrace(ctx))
128125
}
129126
resolver, err := commands.GetResolver(ctx, context)
130127
if err != nil {

cmd/ctr/commands/resolver.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ import (
2222
"crypto/tls"
2323
"crypto/x509"
2424
"fmt"
25+
"io"
2526
"io/ioutil"
27+
"net/http"
28+
"net/http/httptrace"
29+
"net/http/httputil"
2630
"strings"
2731

2832
"github.com/containerd/console"
33+
"github.com/containerd/containerd/log"
2934
"github.com/containerd/containerd/remotes"
3035
"github.com/containerd/containerd/remotes/docker"
3136
"github.com/containerd/containerd/remotes/docker/config"
@@ -96,6 +101,16 @@ func GetResolver(ctx gocontext.Context, clicontext *cli.Context) (remotes.Resolv
96101
hostOptions.HostDir = config.HostDirFromRoot(hostDir)
97102
}
98103

104+
if clicontext.Bool("http-dump") {
105+
hostOptions.UpdateClient = func(client *http.Client) error {
106+
client.Transport = &DebugTransport{
107+
transport: client.Transport,
108+
writer: log.G(ctx).Writer(),
109+
}
110+
return nil
111+
}
112+
}
113+
99114
options.Hosts = config.ConfigureHosts(ctx, hostOptions)
100115

101116
return docker.NewResolver(options), nil
@@ -135,3 +150,57 @@ func resolverDefaultTLS(clicontext *cli.Context) (*tls.Config, error) {
135150

136151
return config, nil
137152
}
153+
154+
// DebugTransport wraps the underlying http.RoundTripper interface and dumps all requests/responses to the writer.
155+
type DebugTransport struct {
156+
transport http.RoundTripper
157+
writer io.Writer
158+
}
159+
160+
// RoundTrip dumps request/responses and executes the request using the underlying transport.
161+
func (t DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
162+
in, err := httputil.DumpRequest(req, true)
163+
if err != nil {
164+
return nil, errors.Wrap(err, "failed to dump request")
165+
}
166+
167+
if _, err := t.writer.Write(in); err != nil {
168+
return nil, err
169+
}
170+
171+
resp, err := t.transport.RoundTrip(req)
172+
if err != nil {
173+
return nil, err
174+
}
175+
176+
out, err := httputil.DumpResponse(resp, true)
177+
if err != nil {
178+
return nil, errors.Wrap(err, "failed to dump response")
179+
}
180+
181+
if _, err := t.writer.Write(out); err != nil {
182+
return nil, err
183+
}
184+
185+
return resp, err
186+
}
187+
188+
// NewDebugClientTrace returns a Go http trace client predefined to write DNS and connection
189+
// information to the log. This is used via the --http-trace flag on push and pull operations in ctr.
190+
func NewDebugClientTrace(ctx gocontext.Context) *httptrace.ClientTrace {
191+
return &httptrace.ClientTrace{
192+
DNSStart: func(dnsInfo httptrace.DNSStartInfo) {
193+
log.G(ctx).WithField("host", dnsInfo.Host).Debugf("DNS lookup")
194+
},
195+
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
196+
if dnsInfo.Err != nil {
197+
log.G(ctx).WithField("lookup_err", dnsInfo.Err).Debugf("DNS lookup error")
198+
} else {
199+
log.G(ctx).WithField("result", dnsInfo.Addrs[0].String()).WithField("coalesced", dnsInfo.Coalesced).Debugf("DNS lookup complete")
200+
}
201+
},
202+
GotConn: func(connInfo httptrace.GotConnInfo) {
203+
log.G(ctx).WithField("reused", connInfo.Reused).WithField("remote_addr", connInfo.Conn.RemoteAddr().String()).Debugf("Connection successful")
204+
},
205+
}
206+
}

remotes/docker/config/hosts.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import (
3737
"github.com/pkg/errors"
3838
)
3939

40+
// UpdateClientFunc is a function that lets you to amend http Client behavior used by registry clients.
41+
type UpdateClientFunc func(client *http.Client) error
42+
4043
type hostConfig struct {
4144
scheme string
4245
host string
@@ -61,6 +64,8 @@ type HostOptions struct {
6164
Credentials func(host string) (string, string, error)
6265
DefaultTLS *tls.Config
6366
DefaultScheme string
67+
// UpdateClient will be called after creating http.Client object, so clients can provide extra configuration
68+
UpdateClient UpdateClientFunc
6469
}
6570

6671
// ConfigureHosts creates a registry hosts function from the provided
@@ -130,6 +135,11 @@ func ConfigureHosts(ctx context.Context, options HostOptions) docker.RegistryHos
130135
client := &http.Client{
131136
Transport: defaultTransport,
132137
}
138+
if options.UpdateClient != nil {
139+
if err := options.UpdateClient(client); err != nil {
140+
return nil, err
141+
}
142+
}
133143

134144
authOpts := []docker.AuthorizerOpt{docker.WithAuthClient(client)}
135145
if options.Credentials != nil {
@@ -198,6 +208,11 @@ func ConfigureHosts(ctx context.Context, options HostOptions) docker.RegistryHos
198208

199209
c := *client
200210
c.Transport = tr
211+
if options.UpdateClient != nil {
212+
if err := options.UpdateClient(&c); err != nil {
213+
return nil, err
214+
}
215+
}
201216

202217
rhosts[i].Client = &c
203218
rhosts[i].Authorizer = docker.NewDockerAuthorizer(append(authOpts, docker.WithAuthClient(&c))...)

0 commit comments

Comments
 (0)