mirror of https://github.com/prometheus/prometheus
Merge pull request #9125 from LeviHarrison/docker_sd-host-networking
docker_sd: Support host network modepull/7229/head
commit
03bee3b5df
|
@ -738,6 +738,7 @@ var expectedConf = &Config{
|
||||||
Filters: []moby.Filter{},
|
Filters: []moby.Filter{},
|
||||||
Host: "unix:///var/run/docker.sock",
|
Host: "unix:///var/run/docker.sock",
|
||||||
Port: 80,
|
Port: 80,
|
||||||
|
HostNetworkingHost: "localhost",
|
||||||
RefreshInterval: model.Duration(60 * time.Second),
|
RefreshInterval: model.Duration(60 * time.Second),
|
||||||
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
||||||
},
|
},
|
||||||
|
|
|
@ -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{},
|
||||||
|
HostNetworkingHost: "localhost",
|
||||||
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +69,7 @@ type DockerSDConfig struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
Filters []Filter `yaml:"filters"`
|
Filters []Filter `yaml:"filters"`
|
||||||
|
HostNetworkingHost string `yaml:"host_networking_host"`
|
||||||
|
|
||||||
RefreshInterval model.Duration `yaml:"refresh_interval"`
|
RefreshInterval model.Duration `yaml:"refresh_interval"`
|
||||||
}
|
}
|
||||||
|
@ -106,6 +108,7 @@ type DockerDiscovery struct {
|
||||||
*refresh.Discovery
|
*refresh.Discovery
|
||||||
client *client.Client
|
client *client.Client
|
||||||
port int
|
port int
|
||||||
|
hostNetworkingHost string
|
||||||
filters filters.Args
|
filters filters.Args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,6 +118,7 @@ func NewDockerDiscovery(conf *DockerSDConfig, logger log.Logger) (*DockerDiscove
|
||||||
|
|
||||||
d := &DockerDiscovery{
|
d := &DockerDiscovery{
|
||||||
port: conf.Port,
|
port: conf.Port,
|
||||||
|
hostNetworkingHost: conf.HostNetworkingHost,
|
||||||
}
|
}
|
||||||
|
|
||||||
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.hostNetworkingHost
|
||||||
|
}
|
||||||
|
|
||||||
labels[model.AddressLabel] = model.LabelValue(addr)
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
||||||
tg.Targets = append(tg.Targets, labels)
|
tg.Targets = append(tg.Targets, labels)
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ host: %s
|
||||||
tg := tgs[0]
|
tg := tgs[0]
|
||||||
require.NotNil(t, tg)
|
require.NotNil(t, tg)
|
||||||
require.NotNil(t, tg.Targets)
|
require.NotNil(t, tg.Targets)
|
||||||
require.Equal(t, 2, len(tg.Targets))
|
require.Equal(t, 3, len(tg.Targets))
|
||||||
|
|
||||||
for i, lbls := range []model.LabelSet{
|
for i, lbls := range []model.LabelSet{
|
||||||
{
|
{
|
||||||
|
@ -93,6 +93,16 @@ host: %s
|
||||||
"__meta_docker_network_name": "dockersd_default",
|
"__meta_docker_network_name": "dockersd_default",
|
||||||
"__meta_docker_network_scope": "local",
|
"__meta_docker_network_scope": "local",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"__address__": "localhost",
|
||||||
|
"__meta_docker_container_id": "54ed6cc5c0988260436cb0e739b7b6c9cad6c439a93b4c4fdbe9753e1c94b189",
|
||||||
|
"__meta_docker_container_label_com_docker_compose_project": "dockersd",
|
||||||
|
"__meta_docker_container_label_com_docker_compose_service": "host_networking",
|
||||||
|
"__meta_docker_container_label_com_docker_compose_version": "1.25.0",
|
||||||
|
"__meta_docker_container_name": "/dockersd_host_networking_1",
|
||||||
|
"__meta_docker_container_network_mode": "host",
|
||||||
|
"__meta_docker_network_ip": "",
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) {
|
t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) {
|
||||||
require.Equal(t, lbls, tg.Targets[i])
|
require.Equal(t, lbls, tg.Targets[i])
|
||||||
|
|
|
@ -89,5 +89,44 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Mounts": []
|
"Mounts": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Id": "54ed6cc5c0988260436cb0e739b7b6c9cad6c439a93b4c4fdbe9753e1c94b189",
|
||||||
|
"Names": [
|
||||||
|
"/dockersd_host_networking_1"
|
||||||
|
],
|
||||||
|
"Image": "httpd",
|
||||||
|
"ImageID": "sha256:73b8cfec11558fe86f565b4357f6d6c8560f4c49a5f15ae970a24da86c9adc93",
|
||||||
|
"Command": "httpd-foreground",
|
||||||
|
"Created": 1627440494,
|
||||||
|
"Ports": [],
|
||||||
|
"Labels": {
|
||||||
|
"com.docker.compose.project": "dockersd",
|
||||||
|
"com.docker.compose.service": "host_networking",
|
||||||
|
"com.docker.compose.version": "1.25.0"
|
||||||
|
},
|
||||||
|
"State": "running",
|
||||||
|
"Status": "Up 3 minutes",
|
||||||
|
"HostConfig": { "NetworkMode": "host" },
|
||||||
|
"NetworkSettings": {
|
||||||
|
"Networks": {
|
||||||
|
"host": {
|
||||||
|
"IPAMConfig": null,
|
||||||
|
"Links": null,
|
||||||
|
"Aliases": null,
|
||||||
|
"NetworkID": "80c4459fa193c5c8b57e90e117d2f899d1a86708e548738149d62e03df0ec35c",
|
||||||
|
"EndpointID": "e9bea4c499c34bd41609b0e1e9b38f9964c69180c1a22130f28b6af802c156d8",
|
||||||
|
"Gateway": "",
|
||||||
|
"IPAddress": "",
|
||||||
|
"IPPrefixLen": 0,
|
||||||
|
"IPv6Gateway": "",
|
||||||
|
"GlobalIPv6Address": "",
|
||||||
|
"GlobalIPv6PrefixLen": 0,
|
||||||
|
"MacAddress": "",
|
||||||
|
"DriverOpts": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Mounts": []
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -640,6 +640,9 @@ tls_config:
|
||||||
# tasks and services that don't have published ports.
|
# tasks and services that don't have published ports.
|
||||||
[ port: <int> | default = 80 ]
|
[ port: <int> | default = 80 ]
|
||||||
|
|
||||||
|
# The host to use if the container is in host networking mode.
|
||||||
|
[ host_networking_host: <string> | default = "localhost" ]
|
||||||
|
|
||||||
# Optional filters to limit the discovery process to a subset of available
|
# Optional filters to limit the discovery process to a subset of available
|
||||||
# resources.
|
# resources.
|
||||||
# The available filters are listed in the upstream documentation:
|
# The available filters are listed in the upstream documentation:
|
||||||
|
|
Loading…
Reference in New Issue