2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-03-11 14:41:09 +00:00
|
|
|
"io"
|
2022-08-14 22:05:12 +00:00
|
|
|
"strconv"
|
2020-04-23 08:34:04 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 11:45:08 +00:00
|
|
|
func (s *Service) key() string {
|
|
|
|
return s.Metadata.key()
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 14:41:09 +00:00
|
|
|
func parseServiceList(r io.Reader) (map[string]object, ListMeta, error) {
|
2021-02-26 14:54:03 +00:00
|
|
|
var sl ServiceList
|
2021-03-11 14:41:09 +00:00
|
|
|
d := json.NewDecoder(r)
|
|
|
|
if err := d.Decode(&sl); err != nil {
|
|
|
|
return nil, sl.Metadata, fmt.Errorf("cannot unmarshal ServiceList: %w", err)
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
2021-04-02 11:45:08 +00:00
|
|
|
objectsByKey := make(map[string]object)
|
2021-02-26 15:00:08 +00:00
|
|
|
for _, s := range sl.Items {
|
2021-04-02 11:45:08 +00:00
|
|
|
objectsByKey[s.key()] = s
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
2021-04-02 11:45:08 +00:00
|
|
|
return objectsByKey, sl.Metadata, nil
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseService(data []byte) (object, error) {
|
2021-02-26 15:00:08 +00:00
|
|
|
var s Service
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
2021-02-26 14:54:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-26 15:00:08 +00:00
|
|
|
return &s, nil
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:02:27 +00:00
|
|
|
// ServiceList is k8s service list.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#servicelist-v1-core
|
|
|
|
type ServiceList struct {
|
2021-02-26 14:54:03 +00:00
|
|
|
Metadata ListMeta
|
|
|
|
Items []*Service
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Service is k8s service.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#service-v1-core
|
|
|
|
type Service struct {
|
|
|
|
Metadata ObjectMeta
|
|
|
|
Spec ServiceSpec
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceSpec is k8s service spec.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#servicespec-v1-core
|
|
|
|
type ServiceSpec struct {
|
|
|
|
ClusterIP string
|
|
|
|
ExternalName string
|
|
|
|
Type string
|
|
|
|
Ports []ServicePort
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServicePort is k8s service port.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#serviceport-v1-core
|
|
|
|
type ServicePort struct {
|
|
|
|
Name string
|
|
|
|
Protocol string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:21:27 +00:00
|
|
|
// getTargetLabels returns labels for each port of the given s.
|
2020-04-13 18:02:27 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#service
|
2023-09-01 07:34:16 +00:00
|
|
|
func (s *Service) getTargetLabels(_ *groupWatcher) []*promutils.Labels {
|
2020-04-13 18:02:27 +00:00
|
|
|
host := fmt.Sprintf("%s.%s.svc", s.Metadata.Name, s.Metadata.Namespace)
|
2022-11-30 05:22:12 +00:00
|
|
|
var ms []*promutils.Labels
|
2020-04-13 18:02:27 +00:00
|
|
|
for _, sp := range s.Spec.Ports {
|
2020-04-23 08:34:04 +00:00
|
|
|
addr := discoveryutils.JoinHostPort(host, sp.Port)
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.GetLabels()
|
|
|
|
m.Add("__address__", addr)
|
|
|
|
m.Add("__meta_kubernetes_service_port_name", sp.Name)
|
|
|
|
m.Add("__meta_kubernetes_service_port_number", strconv.Itoa(sp.Port))
|
|
|
|
m.Add("__meta_kubernetes_service_port_protocol", sp.Protocol)
|
2020-04-13 18:02:27 +00:00
|
|
|
s.appendCommonLabels(m)
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func (s *Service) appendCommonLabels(m *promutils.Labels) {
|
|
|
|
m.Add("__meta_kubernetes_namespace", s.Metadata.Namespace)
|
|
|
|
m.Add("__meta_kubernetes_service_name", s.Metadata.Name)
|
|
|
|
m.Add("__meta_kubernetes_service_type", s.Spec.Type)
|
2020-04-13 18:02:27 +00:00
|
|
|
if s.Spec.Type != "ExternalName" {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add("__meta_kubernetes_service_cluster_ip", s.Spec.ClusterIP)
|
2020-04-13 18:02:27 +00:00
|
|
|
} else {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add("__meta_kubernetes_service_external_name", s.Spec.ExternalName)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
s.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_service", m)
|
|
|
|
}
|