mirror of https://github.com/docker/cli.git
Merge pull request #916 from dnephin/use-client-with-ops
Use new APIClient interface
This commit is contained in:
commit
fe067a0877
|
@ -22,7 +22,6 @@ import (
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
registrytypes "github.com/docker/docker/api/types/registry"
|
registrytypes "github.com/docker/docker/api/types/registry"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/docker/go-connections/sockets"
|
|
||||||
"github.com/docker/go-connections/tlsconfig"
|
"github.com/docker/go-connections/tlsconfig"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -260,12 +259,12 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
|
||||||
verStr = tmpStr
|
verStr = tmpStr
|
||||||
}
|
}
|
||||||
|
|
||||||
httpClient, err := newHTTPClient(host, opts.TLSOptions)
|
return client.NewClientWithOpts(
|
||||||
if err != nil {
|
withHTTPClient(opts.TLSOptions),
|
||||||
return &client.Client{}, err
|
client.WithHTTPHeaders(customHeaders),
|
||||||
}
|
client.WithVersion(verStr),
|
||||||
|
client.WithHost(host),
|
||||||
return client.NewClient(host, verStr, httpClient, customHeaders)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (string, error) {
|
func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (string, error) {
|
||||||
|
@ -282,35 +281,32 @@ func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (string, error
|
||||||
return dopts.ParseHost(tlsOptions != nil, host)
|
return dopts.ParseHost(tlsOptions != nil, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
|
func withHTTPClient(tlsOpts *tlsconfig.Options) func(*client.Client) error {
|
||||||
if tlsOptions == nil {
|
return func(c *client.Client) error {
|
||||||
// let the api client configure the default transport.
|
if tlsOpts == nil {
|
||||||
return nil, nil
|
// Use the default HTTPClient
|
||||||
}
|
return nil
|
||||||
opts := *tlsOptions
|
}
|
||||||
opts.ExclusiveRootPools = true
|
|
||||||
config, err := tlsconfig.Client(opts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: config,
|
|
||||||
DialContext: (&net.Dialer{
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
}).DialContext,
|
|
||||||
}
|
|
||||||
hostURL, err := client.ParseHostURL(host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sockets.ConfigureTransport(tr, hostURL.Scheme, hostURL.Host)
|
opts := *tlsOpts
|
||||||
|
opts.ExclusiveRootPools = true
|
||||||
|
tlsConfig, err := tlsconfig.Client(opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return &http.Client{
|
httpClient := &http.Client{
|
||||||
Transport: tr,
|
Transport: &http.Transport{
|
||||||
CheckRedirect: client.CheckRedirect,
|
TLSClientConfig: tlsConfig,
|
||||||
}, nil
|
DialContext: (&net.Dialer{
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
}).DialContext,
|
||||||
|
},
|
||||||
|
CheckRedirect: client.CheckRedirect,
|
||||||
|
}
|
||||||
|
return client.WithHTTPClient(httpClient)(c)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserAgent returns the user agent string used for making API requests
|
// UserAgent returns the user agent string used for making API requests
|
||||||
|
|
Loading…
Reference in New Issue