Merge pull request #7604 from roidelapluie/swarmsocket

Docker swarm: enable unix socket
This commit is contained in:
Björn Rabenstein 2020-07-20 13:11:06 +02:00 committed by GitHub
commit 79620c78db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 12 deletions

View File

@ -88,16 +88,12 @@ type Discovery struct {
// NewDiscovery returns a new Discovery which periodically refreshes its targets. // NewDiscovery returns a new Discovery which periodically refreshes its targets.
func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
var err error
d := &Discovery{ d := &Discovery{
port: conf.Port, port: conf.Port,
role: conf.Role, role: conf.Role,
} }
rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false)
if err != nil {
return nil, err
}
// This is used in tests. In normal situations, it is set when Unmarshaling. // This is used in tests. In normal situations, it is set when Unmarshaling.
if conf.url == nil { if conf.url == nil {
conf.url, err = url.Parse(conf.Host) conf.url, err = url.Parse(conf.Host)
@ -106,15 +102,29 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
} }
} }
d.client, err = client.NewClientWithOpts( opts := []client.Opt{
client.WithHost(conf.Host), client.WithHost(conf.Host),
client.WithHTTPClient(&http.Client{
Transport: rt,
Timeout: time.Duration(conf.RefreshInterval),
}),
client.WithScheme(conf.url.Scheme),
client.WithAPIVersionNegotiation(), client.WithAPIVersionNegotiation(),
) }
// There are other protocols than HTTP supported by the Docker daemon, like
// unix, which are not supported by the HTTP client. Passing HTTP client
// options to the Docker client makes those non-HTTP requests fail.
if conf.url.Scheme == "http" || conf.url.Scheme == "https" {
rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false)
if err != nil {
return nil, err
}
opts = append(opts,
client.WithHTTPClient(&http.Client{
Transport: rt,
Timeout: time.Duration(conf.RefreshInterval),
}),
client.WithScheme(conf.url.Scheme),
)
}
d.client, err = client.NewClientWithOpts(opts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("error setting up docker swarm client: %w", err) return nil, fmt.Errorf("error setting up docker swarm client: %w", err)
} }