2021-06-25 10:20:18 +00:00
|
|
|
package dockerswarm
|
2020-10-12 10:38:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// https://docs.docker.com/engine/api/v1.40/#tag/Service
|
|
|
|
type service struct {
|
|
|
|
ID string
|
|
|
|
Spec struct {
|
|
|
|
Labels map[string]string
|
|
|
|
Name string
|
|
|
|
TaskTemplate struct {
|
|
|
|
ContainerSpec struct {
|
|
|
|
Hostname string
|
|
|
|
Image string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Mode struct {
|
|
|
|
Global interface{}
|
|
|
|
Replicated interface{}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 13:12:36 +00:00
|
|
|
UpdateStatus struct {
|
2020-10-12 10:38:21 +00:00
|
|
|
State string
|
|
|
|
}
|
|
|
|
Endpoint struct {
|
|
|
|
Ports []portConfig
|
|
|
|
VirtualIPs []struct {
|
|
|
|
NetworkID string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type portConfig struct {
|
|
|
|
Protocol string
|
|
|
|
Name string
|
|
|
|
PublishMode string
|
|
|
|
PublishedPort int
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func getServicesLabels(cfg *apiConfig) ([]*promutils.Labels, error) {
|
2020-10-12 10:38:21 +00:00
|
|
|
services, err := getServices(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 10:20:18 +00:00
|
|
|
networksLabels, err := getNetworksLabelsByNetworkID(cfg)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addServicesLabels(services, networksLabels, cfg.port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getServices(cfg *apiConfig) ([]service, error) {
|
2023-01-12 06:50:34 +00:00
|
|
|
filtersQueryArg := ""
|
2023-01-12 07:02:45 +00:00
|
|
|
if cfg.role == "services" {
|
2023-01-12 06:50:34 +00:00
|
|
|
filtersQueryArg = cfg.filtersQueryArg
|
|
|
|
}
|
|
|
|
data, err := cfg.getAPIResponse("/services", filtersQueryArg)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot query dockerswarm api for services: %w", err)
|
|
|
|
}
|
|
|
|
return parseServicesResponse(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseServicesResponse(data []byte) ([]service, error) {
|
|
|
|
var services []service
|
|
|
|
if err := json.Unmarshal(data, &services); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse services: %w", err)
|
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getServiceMode(svc service) string {
|
|
|
|
if svc.Spec.Mode.Global != nil {
|
|
|
|
return "global"
|
|
|
|
}
|
|
|
|
if svc.Spec.Mode.Replicated != nil {
|
|
|
|
return "replicated"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func addServicesLabels(services []service, networksLabels map[string]*promutils.Labels, port int) []*promutils.Labels {
|
|
|
|
var ms []*promutils.Labels
|
2020-10-12 10:38:21 +00:00
|
|
|
for _, service := range services {
|
2022-11-30 05:22:12 +00:00
|
|
|
commonLabels := promutils.NewLabels(10)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_service_id", service.ID)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_service_name", service.Spec.Name)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_service_mode", getServiceMode(service))
|
|
|
|
commonLabels.Add("__meta_dockerswarm_service_task_container_hostname", service.Spec.TaskTemplate.ContainerSpec.Hostname)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_service_task_container_image", service.Spec.TaskTemplate.ContainerSpec.Image)
|
|
|
|
commonLabels.Add("__meta_dockerswarm_service_updating_status", service.UpdateStatus.State)
|
2020-10-12 10:38:21 +00:00
|
|
|
for k, v := range service.Spec.Labels {
|
2022-11-30 05:22:12 +00:00
|
|
|
commonLabels.Add(discoveryutils.SanitizeLabelName("__meta_dockerswarm_service_label_"+k), v)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
for _, vip := range service.Endpoint.VirtualIPs {
|
2021-02-04 13:56:42 +00:00
|
|
|
// skip services without virtual address.
|
|
|
|
// usually its host services.
|
|
|
|
if vip.Addr == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-12 10:38:21 +00:00
|
|
|
ip, _, err := net.ParseCIDR(vip.Addr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot parse: %q as cidr for service label add, err: %v", vip.Addr, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-10-12 13:12:36 +00:00
|
|
|
added := false
|
2020-10-12 10:38:21 +00:00
|
|
|
for _, ep := range service.Endpoint.Ports {
|
|
|
|
if ep.Protocol != "tcp" {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(24)
|
|
|
|
m.Add("__address__", discoveryutils.JoinHostPort(ip.String(), ep.PublishedPort))
|
|
|
|
m.Add("__meta_dockerswarm_service_endpoint_port_name", ep.Name)
|
|
|
|
m.Add("__meta_dockerswarm_service_endpoint_port_publish_mode", ep.PublishMode)
|
|
|
|
m.AddFrom(commonLabels)
|
|
|
|
m.AddFrom(networksLabels[vip.NetworkID])
|
2023-02-23 01:01:01 +00:00
|
|
|
// Remove possible duplicate labels, which can appear after AddFrom() calls
|
2022-11-30 05:22:12 +00:00
|
|
|
m.RemoveDuplicates()
|
2020-10-12 10:38:21 +00:00
|
|
|
added = true
|
2020-10-12 13:12:36 +00:00
|
|
|
ms = append(ms, m)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
if !added {
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(24)
|
|
|
|
m.Add("__address__", discoveryutils.JoinHostPort(ip.String(), port))
|
|
|
|
m.AddFrom(commonLabels)
|
|
|
|
m.AddFrom(networksLabels[vip.NetworkID])
|
2023-02-23 01:01:01 +00:00
|
|
|
// Remove possible duplicate labels, which can appear after AddFrom() calls
|
2022-11-30 05:22:12 +00:00
|
|
|
m.RemoveDuplicates()
|
2020-10-12 13:12:36 +00:00
|
|
|
ms = append(ms, m)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|