diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2024-06-20 22:08:50 +0300 |
---|---|---|
committer | Austin S. Hemmelgarn <ahferroin7@gmail.com> | 2024-07-10 08:47:30 -0400 |
commit | 4a1a1a430c9ec11b40ce7afed7840ad5e5799d9b (patch) | |
tree | f9e91899dddedcb55bbe2e8a9b396766f913cde4 | |
parent | c695e8f4b039e7c58a0113c9bef6f315fef4f1a7 (diff) |
go.d docker respect DOCKER_HOST env var (#17979)
(cherry picked from commit 74d42b50fa1e36fbaaecff7d68f1dd531e9fb427)
3 files changed, 34 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/dockerd/docker.go b/src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/dockerd/docker.go index d3ff9f333d..cca6b658e3 100644 --- a/src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/dockerd/docker.go +++ b/src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/dockerd/docker.go @@ -13,6 +13,7 @@ import ( "github.com/netdata/netdata/go/go.d.plugin/agent/discovery/sd/model" "github.com/netdata/netdata/go/go.d.plugin/logger" + "github.com/netdata/netdata/go/go.d.plugin/pkg/dockerhost" "github.com/netdata/netdata/go/go.d.plugin/pkg/web" "github.com/docker/docker/api/types" @@ -43,6 +44,11 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) { started: make(chan struct{}), } + if addr := dockerhost.FromEnv(); addr != "" && d.addr == docker.DefaultDockerHost { + d.Infof("using docker host from environment: %s ", addr) + d.addr = addr + } + d.Tags().Merge(tags) if cfg.Timeout.Duration().Seconds() != 0 { diff --git a/src/go/collectors/go.d.plugin/modules/docker/docker.go b/src/go/collectors/go.d.plugin/modules/docker/docker.go index ecf34c6b7e..68662fd4c8 100644 --- a/src/go/collectors/go.d.plugin/modules/docker/docker.go +++ b/src/go/collectors/go.d.plugin/modules/docker/docker.go @@ -9,6 +9,7 @@ import ( "time" "github.com/netdata/netdata/go/go.d.plugin/agent/module" + "github.com/netdata/netdata/go/go.d.plugin/pkg/dockerhost" "github.com/netdata/netdata/go/go.d.plugin/pkg/web" "github.com/docker/docker/api/types" @@ -79,6 +80,10 @@ func (d *Docker) Configuration() any { } func (d *Docker) Init() error { + if addr := dockerhost.FromEnv(); addr != "" && d.Address == docker.DefaultDockerHost { + d.Infof("using docker host from environment: %s ", addr) + d.Address = addr + } return nil } diff --git a/src/go/collectors/go.d.plugin/pkg/dockerhost/dockerhost.go b/src/go/collectors/go.d.plugin/pkg/dockerhost/dockerhost.go new file mode 100644 index 0000000000..eb26b18fa4 --- /dev/null +++ b/src/go/collectors/go.d.plugin/pkg/dockerhost/dockerhost.go @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package dockerhost + +import ( + "fmt" + "os" + "strings" +) + +func FromEnv() string { + addr := os.Getenv("DOCKER_HOST") + if addr == "" { + return "" + } + if strings.HasPrefix(addr, "tcp://") || strings.HasPrefix(addr, "unix://") { + return addr + } + if strings.HasPrefix(addr, "/") { + return fmt.Sprintf("unix://%s", addr) + } + return fmt.Sprintf("tcp://%s", addr) +} |