From 45644c82f632ca5e3db5286a6bdc814d8535246e Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 17 Jul 2020 17:38:02 +0200 Subject: [PATCH 1/3] Docker swarm: enable unix socket Fixes #7603 Signed-off-by: Julien Pivotto --- discovery/dockerswarm/dockerswarm.go | 34 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/discovery/dockerswarm/dockerswarm.go b/discovery/dockerswarm/dockerswarm.go index 418eb9d8e..0073f1516 100644 --- a/discovery/dockerswarm/dockerswarm.go +++ b/discovery/dockerswarm/dockerswarm.go @@ -88,16 +88,12 @@ type Discovery struct { // NewDiscovery returns a new Discovery which periodically refreshes its targets. func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { + var err error d := &Discovery{ port: conf.Port, 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. if conf.url == nil { 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.WithHTTPClient(&http.Client{ - Transport: rt, - Timeout: time.Duration(conf.RefreshInterval), - }), - client.WithScheme(conf.url.Scheme), client.WithAPIVersionNegotiation(), - ) + } + + // There are other protocols that HTTP supported by the Docker daemon, like + // unix, which do not require an http client. Passing an http client 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 { return nil, fmt.Errorf("error setting up docker swarm client: %w", err) } From 968c86d642e4f7db0994cb35d90d6a7f4e64749e Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 17 Jul 2020 17:41:02 +0200 Subject: [PATCH 2/3] Fix comment Signed-off-by: Julien Pivotto --- discovery/dockerswarm/dockerswarm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/discovery/dockerswarm/dockerswarm.go b/discovery/dockerswarm/dockerswarm.go index 0073f1516..32b0cf9e3 100644 --- a/discovery/dockerswarm/dockerswarm.go +++ b/discovery/dockerswarm/dockerswarm.go @@ -108,8 +108,8 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { } // There are other protocols that HTTP supported by the Docker daemon, like - // unix, which do not require an http client. Passing an http client to the - // docker client makes those non-http requests fail. + // 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 { From 49f48d8f652edcb709873580c626871f4ccc96ed Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 17 Jul 2020 17:48:05 +0200 Subject: [PATCH 3/3] Fix comment Signed-off-by: Julien Pivotto --- discovery/dockerswarm/dockerswarm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discovery/dockerswarm/dockerswarm.go b/discovery/dockerswarm/dockerswarm.go index 32b0cf9e3..0464f032f 100644 --- a/discovery/dockerswarm/dockerswarm.go +++ b/discovery/dockerswarm/dockerswarm.go @@ -107,7 +107,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { client.WithAPIVersionNegotiation(), } - // There are other protocols that HTTP supported by the Docker daemon, like + // 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" {