2021-06-25 10:42:47 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// See https://github.com/moby/moby/blob/314759dc2f4745925d8dec6d15acc7761c6e5c92/docs/api/v1.41.yaml#L4024
|
|
|
|
type container struct {
|
2021-06-29 12:11:24 +02:00
|
|
|
ID string
|
2021-06-25 10:42:47 +02:00
|
|
|
Names []string
|
|
|
|
Labels map[string]string
|
|
|
|
Ports []struct {
|
|
|
|
IP string
|
|
|
|
PrivatePort int
|
|
|
|
PublicPort int
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
HostConfig struct {
|
|
|
|
NetworkMode string
|
|
|
|
}
|
|
|
|
NetworkSettings struct {
|
|
|
|
Networks map[string]struct {
|
|
|
|
IPAddress string
|
|
|
|
NetworkID string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainersLabels(cfg *apiConfig) ([]map[string]string, error) {
|
2021-06-25 12:20:18 +02:00
|
|
|
networkLabels, err := getNetworksLabelsByNetworkID(cfg)
|
2021-06-25 10:42:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
containers, err := getContainers(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-13 12:30:13 +02:00
|
|
|
return addContainersLabels(containers, networkLabels, cfg.port, cfg.hostNetworkingHost), nil
|
2021-06-25 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getContainers(cfg *apiConfig) ([]container, error) {
|
|
|
|
resp, err := cfg.getAPIResponse("/containers/json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot query dockerd api for containers: %w", err)
|
|
|
|
}
|
|
|
|
return parseContainers(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseContainers(data []byte) ([]container, error) {
|
|
|
|
var containers []container
|
|
|
|
if err := json.Unmarshal(data, &containers); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse containers: %w", err)
|
|
|
|
}
|
|
|
|
return containers, nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 12:30:13 +02:00
|
|
|
func addContainersLabels(containers []container, networkLabels map[string]map[string]string, defaultPort int, hostNetworkingHost string) []map[string]string {
|
2021-06-25 10:42:47 +02:00
|
|
|
var ms []map[string]string
|
2021-06-25 12:20:18 +02:00
|
|
|
for i := range containers {
|
|
|
|
c := &containers[i]
|
|
|
|
if len(c.Names) == 0 {
|
2021-06-25 10:42:47 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
for _, n := range c.NetworkSettings.Networks {
|
2021-06-25 10:42:47 +02:00
|
|
|
var added bool
|
2021-06-25 12:20:18 +02:00
|
|
|
for _, p := range c.Ports {
|
|
|
|
if p.Type != "tcp" {
|
2021-06-25 10:42:47 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
m := map[string]string{
|
|
|
|
"__address__": discoveryutils.JoinHostPort(n.IPAddress, p.PrivatePort),
|
|
|
|
"__meta_docker_network_ip": n.IPAddress,
|
|
|
|
"__meta_docker_port_private": strconv.Itoa(p.PrivatePort),
|
2021-06-25 10:42:47 +02:00
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
if p.PublicPort > 0 {
|
|
|
|
m["__meta_docker_port_public"] = strconv.Itoa(p.PublicPort)
|
|
|
|
m["__meta_docker_port_public_ip"] = p.IP
|
2021-06-25 10:42:47 +02:00
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
addCommonLabels(m, c, networkLabels[n.NetworkID])
|
|
|
|
ms = append(ms, m)
|
2021-06-25 10:42:47 +02:00
|
|
|
added = true
|
|
|
|
}
|
|
|
|
if !added {
|
|
|
|
// Use fallback port when no exposed ports are available or if all are non-TCP
|
2021-09-13 12:30:13 +02:00
|
|
|
addr := hostNetworkingHost
|
|
|
|
if c.HostConfig.NetworkMode != "host" {
|
|
|
|
addr = discoveryutils.JoinHostPort(n.IPAddress, defaultPort)
|
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
m := map[string]string{
|
2021-09-13 12:30:13 +02:00
|
|
|
"__address__": addr,
|
2021-06-25 12:20:18 +02:00
|
|
|
"__meta_docker_network_ip": n.IPAddress,
|
2021-06-25 10:42:47 +02:00
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
addCommonLabels(m, c, networkLabels[n.NetworkID])
|
|
|
|
ms = append(ms, m)
|
2021-06-25 10:42:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
2021-06-25 12:20:18 +02:00
|
|
|
|
|
|
|
func addCommonLabels(m map[string]string, c *container, networkLabels map[string]string) {
|
2021-06-29 12:11:24 +02:00
|
|
|
m["__meta_docker_container_id"] = c.ID
|
2021-06-25 12:20:18 +02:00
|
|
|
m["__meta_docker_container_name"] = c.Names[0]
|
|
|
|
m["__meta_docker_container_network_mode"] = c.HostConfig.NetworkMode
|
|
|
|
for k, v := range c.Labels {
|
2022-08-26 23:12:39 +02:00
|
|
|
m[discoveryutils.SanitizeLabelName("__meta_docker_container_label_"+k)] = v
|
2021-06-25 12:20:18 +02:00
|
|
|
}
|
|
|
|
for k, v := range networkLabels {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
}
|