Implemented host_network_host setting

Signed-off-by: Levi Harrison <git@leviharrison.dev>
This commit is contained in:
Levi Harrison 2021-07-27 23:20:04 -04:00
parent 60804c5a09
commit 41be43647b
No known key found for this signature in database
GPG key ID: 23A7BDEAE860B57D

View file

@ -54,6 +54,7 @@ var DefaultDockerSDConfig = DockerSDConfig{
RefreshInterval: model.Duration(60 * time.Second), RefreshInterval: model.Duration(60 * time.Second),
Port: 80, Port: 80,
Filters: []Filter{}, Filters: []Filter{},
HostNetworkHost: "localhost",
HTTPClientConfig: config.DefaultHTTPClientConfig, HTTPClientConfig: config.DefaultHTTPClientConfig,
} }
@ -65,9 +66,10 @@ func init() {
type DockerSDConfig struct { type DockerSDConfig struct {
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"` HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
Host string `yaml:"host"` Host string `yaml:"host"`
Port int `yaml:"port"` Port int `yaml:"port"`
Filters []Filter `yaml:"filters"` Filters []Filter `yaml:"filters"`
HostNetworkHost string `yaml:"host_network_host"`
RefreshInterval model.Duration `yaml:"refresh_interval"` RefreshInterval model.Duration `yaml:"refresh_interval"`
} }
@ -104,9 +106,10 @@ func (c *DockerSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
type DockerDiscovery struct { type DockerDiscovery struct {
*refresh.Discovery *refresh.Discovery
client *client.Client client *client.Client
port int port int
filters filters.Args hostNetworkHost string
filters filters.Args
} }
// NewDockerDiscovery returns a new DockerDiscovery which periodically refreshes its targets. // NewDockerDiscovery returns a new DockerDiscovery which periodically refreshes its targets.
@ -114,7 +117,8 @@ func NewDockerDiscovery(conf *DockerSDConfig, logger log.Logger) (*DockerDiscove
var err error var err error
d := &DockerDiscovery{ d := &DockerDiscovery{
port: conf.Port, port: conf.Port,
hostNetworkHost: conf.HostNetworkHost,
} }
hostURL, err := url.Parse(conf.Host) hostURL, err := url.Parse(conf.Host)
@ -245,7 +249,15 @@ func (d *DockerDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group, er
labels[model.LabelName(k)] = model.LabelValue(v) labels[model.LabelName(k)] = model.LabelValue(v)
} }
addr := net.JoinHostPort(n.IPAddress, strconv.FormatUint(uint64(d.port), 10)) // Containers in host networking mode don't have ports,
// so they only end up here, not in the previous loop.
var addr string
if c.HostConfig.NetworkMode != "host" {
addr = net.JoinHostPort(n.IPAddress, strconv.FormatUint(uint64(d.port), 10))
} else {
addr = d.hostNetworkHost
}
labels[model.AddressLabel] = model.LabelValue(addr) labels[model.AddressLabel] = model.LabelValue(addr)
tg.Targets = append(tg.Targets, labels) tg.Targets = append(tg.Targets, labels)
} }