Skip to content

Commit ea6543d

Browse files
committed
Simplify, and better document, newDockerClient
Use a switch instead of nested ifs. Update the documentation, both to be a bit more specific about the mechanism, and also to say that we keep the HTTP special-case now that it exists, and document an alternative. Signed-off-by: Miloslav Trmač <[email protected]>
1 parent e528c8a commit ea6543d

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

docker/daemon/client.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,41 @@ func newDockerClient(sys *types.SystemContext) (*dockerclient.Client, error) {
2626
dockerclient.WithVersion(defaultAPIVersion),
2727
}
2828

29-
// Sadly, unix:// sockets don't work transparently with dockerclient.NewClient.
30-
// They work fine with a nil httpClient; with a non-nil httpClient, the transport’s
31-
// TLSClientConfig must be nil (or the client will try using HTTPS over the PF_UNIX socket
32-
// regardless of the values in the *tls.Config), and we would have to call sockets.ConfigureTransport.
29+
// We conditionalize building the TLS configuration only to TLS sockets:
3330
//
34-
// We don't really want to configure anything for unix:// sockets, so just pass a nil *http.Client.
31+
// The dockerclient.Client implementation differentiates between
32+
// - Client.proto, which is ~how the connection is establishe (IP / AF_UNIX/Windows)
33+
// - Client.scheme, which is what is sent over the connection (HTTP with/without TLS).
3534
//
36-
// Similarly, if we want to communicate over plain HTTP on a TCP socket, we also need to set
37-
// TLSClientConfig to nil. This can be achieved by using the form `http://`
35+
// Only Client.proto is set from the URL in dockerclient.WithHost(),
36+
// Client.scheme is detected based on a http.Client.TLSClientConfig presence;
37+
// dockerclient.WithHTTPClient with a client that has TLSClientConfig set
38+
// will, by default, trigger an attempt to use TLS.
39+
//
40+
// So, don’t use WithHTTPClient for unix:// sockets at all.
41+
//
42+
// Similarly, if we want to communicate over plain HTTP on a TCP socket (http://),
43+
// we also should not set TLSClientConfig. We continue to use WithHTTPClient
44+
// with our slightly non-default settings to avoid a behavior change on updates of c/image.
45+
//
46+
// Alternatively we could use dockerclient.WithScheme to drive the TLS/non-TLS logic
47+
// explicitly, but we would still want to set WithHTTPClient (differently) for https:// and http:// ;
48+
// so that would not be any simpler.
3849
serverURL, err := dockerclient.ParseHostURL(host)
3950
if err != nil {
4051
return nil, err
4152
}
42-
if serverURL.Scheme != "unix" {
43-
if serverURL.Scheme == "http" {
44-
hc := httpConfig()
45-
opts = append(opts, dockerclient.WithHTTPClient(hc))
46-
} else {
47-
hc, err := tlsConfig(sys)
48-
if err != nil {
49-
return nil, err
50-
}
51-
opts = append(opts, dockerclient.WithHTTPClient(hc))
53+
switch serverURL.Scheme {
54+
case "unix": // Nothing
55+
case "http":
56+
hc := httpConfig()
57+
opts = append(opts, dockerclient.WithHTTPClient(hc))
58+
default:
59+
hc, err := tlsConfig(sys)
60+
if err != nil {
61+
return nil, err
5262
}
63+
opts = append(opts, dockerclient.WithHTTPClient(hc))
5364
}
5465

5566
return dockerclient.NewClientWithOpts(opts...)

0 commit comments

Comments
 (0)