2021-06-25 10:20:18 +00:00
|
|
|
package dockerswarm
|
2020-10-12 10:38:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-10-12 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// See https://docs.docker.com/engine/api/v1.40/#tag/Task
|
|
|
|
type task struct {
|
|
|
|
ID string
|
|
|
|
ServiceID string
|
|
|
|
NodeID string
|
|
|
|
DesiredState string
|
|
|
|
NetworksAttachments []struct {
|
|
|
|
Addresses []string
|
|
|
|
Network struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Status struct {
|
|
|
|
State string
|
2020-10-12 13:12:36 +00:00
|
|
|
ContainerStatus struct {
|
2020-10-12 10:38:21 +00:00
|
|
|
ContainerID string
|
|
|
|
}
|
|
|
|
PortStatus struct {
|
|
|
|
Ports []portConfig
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 13:11:26 +00:00
|
|
|
Spec struct {
|
|
|
|
ContainerSpec struct {
|
|
|
|
Labels map[string]string
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 10:38:21 +00:00
|
|
|
Slot int
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func getTasksLabels(cfg *apiConfig) ([]*promutils.Labels, error) {
|
2020-10-12 10:38:21 +00:00
|
|
|
tasks, err := getTasks(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
services, err := getServices(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 10:20:18 +00:00
|
|
|
networkLabels, err := getNetworksLabelsByNetworkID(cfg)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
svcLabels := addServicesLabels(services, networkLabels, cfg.port)
|
|
|
|
nodeLabels, err := getNodesLabels(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addTasksLabels(tasks, nodeLabels, svcLabels, networkLabels, services, cfg.port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTasks(cfg *apiConfig) ([]task, error) {
|
2023-01-12 06:50:34 +00:00
|
|
|
filtersQueryArg := ""
|
2023-01-12 07:02:45 +00:00
|
|
|
if cfg.role == "tasks" {
|
2023-01-12 06:50:34 +00:00
|
|
|
filtersQueryArg = cfg.filtersQueryArg
|
|
|
|
}
|
|
|
|
resp, err := cfg.getAPIResponse("/tasks", filtersQueryArg)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot query dockerswarm api for tasks: %w", err)
|
|
|
|
}
|
|
|
|
return parseTasks(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTasks(data []byte) ([]task, error) {
|
|
|
|
var tasks []task
|
|
|
|
if err := json.Unmarshal(data, &tasks); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse tasks: %w", err)
|
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func addTasksLabels(tasks []task, nodesLabels, servicesLabels []*promutils.Labels, networksLabels map[string]*promutils.Labels, services []service, port int) []*promutils.Labels {
|
|
|
|
var ms []*promutils.Labels
|
2020-10-12 10:38:21 +00:00
|
|
|
for _, task := range tasks {
|
2022-11-30 05:22:12 +00:00
|
|
|
commonLabels := promutils.NewLabels(8)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_task_id", task.ID)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_task_container_id", task.Status.ContainerStatus.ContainerID)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_task_desired_state", task.DesiredState)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_task_slot", strconv.Itoa(task.Slot))
|
|
|
|
commonLabels.Add("__meta_dockerswarm_task_state", task.Status.State)
|
2022-08-05 13:11:26 +00:00
|
|
|
for k, v := range task.Spec.ContainerSpec.Labels {
|
2022-11-30 05:22:12 +00:00
|
|
|
commonLabels.Add(discoveryutils.SanitizeLabelName("__meta_dockerswarm_container_label_"+k), v)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
var svcPorts []portConfig
|
|
|
|
for i, v := range services {
|
|
|
|
if v.ID == task.ServiceID {
|
|
|
|
svcPorts = services[i].Endpoint.Ports
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 13:12:36 +00:00
|
|
|
addLabels(commonLabels, servicesLabels, "__meta_dockerswarm_service_id", task.ServiceID)
|
|
|
|
addLabels(commonLabels, nodesLabels, "__meta_dockerswarm_node_id", task.NodeID)
|
2020-10-12 10:38:21 +00:00
|
|
|
|
|
|
|
for _, port := range task.Status.PortStatus.Ports {
|
|
|
|
if port.Protocol != "tcp" {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(10)
|
|
|
|
m.AddFrom(commonLabels)
|
|
|
|
m.Add("__address__", discoveryutils.JoinHostPort(commonLabels.Get("__meta_dockerswarm_node_address"), port.PublishedPort))
|
|
|
|
m.Add("__meta_dockerswarm_task_port_publish_mode", port.PublishMode)
|
|
|
|
m.RemoveDuplicates()
|
2020-10-12 13:12:36 +00:00
|
|
|
ms = append(ms, m)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
for _, na := range task.NetworksAttachments {
|
2022-01-11 07:20:08 +00:00
|
|
|
networkLabels := networksLabels[na.Network.ID]
|
2020-10-12 10:38:21 +00:00
|
|
|
for _, address := range na.Addresses {
|
|
|
|
ip, _, err := net.ParseCIDR(address)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot parse task network attachments address: %s as net CIDR: %v", address, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-10-12 13:12:36 +00:00
|
|
|
added := false
|
|
|
|
for _, ep := range svcPorts {
|
|
|
|
if ep.Protocol != "tcp" {
|
2020-10-12 10:38:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(20)
|
|
|
|
m.AddFrom(commonLabels)
|
|
|
|
m.AddFrom(networkLabels)
|
|
|
|
m.Add("__address__", discoveryutils.JoinHostPort(ip.String(), ep.PublishedPort))
|
|
|
|
m.Add("__meta_dockerswarm_task_port_publish_mode", ep.PublishMode)
|
|
|
|
m.RemoveDuplicates()
|
2020-10-12 13:12:36 +00:00
|
|
|
ms = append(ms, m)
|
2020-10-12 10:38:21 +00:00
|
|
|
added = true
|
|
|
|
}
|
|
|
|
if !added {
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(20)
|
|
|
|
m.AddFrom(commonLabels)
|
|
|
|
m.AddFrom(networkLabels)
|
|
|
|
m.Add("__address__", discoveryutils.JoinHostPort(ip.String(), port))
|
|
|
|
m.RemoveDuplicates()
|
2020-10-12 13:12:36 +00:00
|
|
|
ms = append(ms, m)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
2020-10-12 13:12:36 +00:00
|
|
|
|
|
|
|
// addLabels adds lables from src to dst if they contain the given `key: value` pair.
|
2022-11-30 05:22:12 +00:00
|
|
|
func addLabels(dst *promutils.Labels, src []*promutils.Labels, key, value string) {
|
2020-10-12 13:12:36 +00:00
|
|
|
for _, m := range src {
|
2022-11-30 05:22:12 +00:00
|
|
|
if m.Get(key) != value {
|
2020-10-12 13:12:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
for _, label := range m.GetLabels() {
|
|
|
|
dst.Add(label.Name, label.Value)
|
2020-10-12 13:12:36 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|