@cryptotaxi247 / netdata-1 / commits / 74d42b50f

go.d docker respect DOCKER_HOST env var (#17979)

Ilya Mashchenko committed Jun 20, 2024 at 22:08 UTC 74d42b50fa1e36fbaaecff7d68f1dd531e9fb427
3 files changed +34
src/go/collectors/go.d.plugin/agent/discovery/sd/discoverer/dockerd/docker.go
+6
@@ -13,6 +13,7 @@ import (
13
14 "github.com/netdata/netdata/go/go.d.plugin/agent/discovery/sd/model"
15 "github.com/netdata/netdata/go/go.d.plugin/logger"
16 + "github.com/netdata/netdata/go/go.d.plugin/pkg/dockerhost"
17 "github.com/netdata/netdata/go/go.d.plugin/pkg/web"
18
19 "github.com/docker/docker/api/types"
@@ -43,6 +44,11 @@ func NewDiscoverer(cfg Config) (*Discoverer, error) {
44 started: make(chan struct{}),
45 }
46
47 + if addr := dockerhost.FromEnv(); addr != "" && d.addr == docker.DefaultDockerHost {
48 + d.Infof("using docker host from environment: %s ", addr)
49 + d.addr = addr
50 + }
51 +
52 d.Tags().Merge(tags)
53
54 if cfg.Timeout.Duration().Seconds() != 0 {
src/go/collectors/go.d.plugin/modules/docker/docker.go
+5
@@ -9,6 +9,7 @@ import (
9 "time"
10
11 "github.com/netdata/netdata/go/go.d.plugin/agent/module"
12 + "github.com/netdata/netdata/go/go.d.plugin/pkg/dockerhost"
13 "github.com/netdata/netdata/go/go.d.plugin/pkg/web"
14
15 "github.com/docker/docker/api/types"
@@ -79,6 +80,10 @@ func (d *Docker) Configuration() any {
80 }
81
82 func (d *Docker) Init() error {
83 + if addr := dockerhost.FromEnv(); addr != "" && d.Address == docker.DefaultDockerHost {
84 + d.Infof("using docker host from environment: %s ", addr)
85 + d.Address = addr
86 + }
87 return nil
88 }
89
src/go/collectors/go.d.plugin/pkg/dockerhost/dockerhost.go new
+23
@@ -0,0 +1,23 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +package dockerhost
4 +
5 +import (
6 + "fmt"
7 + "os"
8 + "strings"
9 +)
10 +
11 +func FromEnv() string {
12 + addr := os.Getenv("DOCKER_HOST")
13 + if addr == "" {
14 + return ""
15 + }
16 + if strings.HasPrefix(addr, "tcp://") || strings.HasPrefix(addr, "unix://") {
17 + return addr
18 + }
19 + if strings.HasPrefix(addr, "/") {
20 + return fmt.Sprintf("unix://%s", addr)
21 + }
22 + return fmt.Sprintf("tcp://%s", addr)
23 +}