2020-09-11 09:16:45 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-03-11 14:41:09 +00:00
|
|
|
"io"
|
2020-09-11 09:16:45 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-09-11 09:16:45 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 11:45:08 +00:00
|
|
|
func (eps *EndpointSlice) key() string {
|
|
|
|
return eps.Metadata.key()
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 14:41:09 +00:00
|
|
|
func parseEndpointSliceList(r io.Reader) (map[string]object, ListMeta, error) {
|
2021-02-26 14:54:03 +00:00
|
|
|
var epsl EndpointSliceList
|
2021-03-11 14:41:09 +00:00
|
|
|
d := json.NewDecoder(r)
|
|
|
|
if err := d.Decode(&epsl); err != nil {
|
|
|
|
return nil, epsl.Metadata, fmt.Errorf("cannot unmarshal EndpointSliceList: %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 14:54:03 +00:00
|
|
|
for _, eps := range epsl.Items {
|
2021-04-02 11:45:08 +00:00
|
|
|
objectsByKey[eps.key()] = eps
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
2021-04-02 11:45:08 +00:00
|
|
|
return objectsByKey, epsl.Metadata, nil
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
2020-09-11 09:16:45 +00:00
|
|
|
|
2021-02-26 14:54:03 +00:00
|
|
|
func parseEndpointSlice(data []byte) (object, error) {
|
|
|
|
var eps EndpointSlice
|
|
|
|
if err := json.Unmarshal(data, &eps); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &eps, nil
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:21:27 +00:00
|
|
|
// getTargetLabels returns labels for eps.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#endpointslices
|
2022-11-30 05:22:12 +00:00
|
|
|
func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []*promutils.Labels {
|
2022-07-06 21:32:24 +00:00
|
|
|
// The associated service name is stored in kubernetes.io/service-name label.
|
|
|
|
// See https://kubernetes.io/docs/reference/labels-annotations-taints/#kubernetesioservice-name
|
2022-11-30 05:22:12 +00:00
|
|
|
svcName := eps.Metadata.Labels.Get("kubernetes.io/service-name")
|
2021-02-26 14:46:13 +00:00
|
|
|
var svc *Service
|
2022-07-06 21:32:24 +00:00
|
|
|
if o := gw.getObjectByRoleLocked("service", eps.Metadata.Namespace, svcName); o != nil {
|
2021-02-26 14:54:03 +00:00
|
|
|
svc = o.(*Service)
|
2021-02-26 14:46:13 +00:00
|
|
|
}
|
2020-09-11 09:16:45 +00:00
|
|
|
podPortsSeen := make(map[*Pod][]int)
|
2022-11-30 05:22:12 +00:00
|
|
|
var ms []*promutils.Labels
|
2020-09-11 09:16:45 +00:00
|
|
|
for _, ess := range eps.Endpoints {
|
2021-02-26 14:54:03 +00:00
|
|
|
var p *Pod
|
2021-04-29 07:14:24 +00:00
|
|
|
if o := gw.getObjectByRoleLocked("pod", ess.TargetRef.Namespace, ess.TargetRef.Name); o != nil {
|
2021-02-26 14:54:03 +00:00
|
|
|
p = o.(*Pod)
|
2021-02-26 14:46:13 +00:00
|
|
|
}
|
2020-09-11 09:16:45 +00:00
|
|
|
for _, epp := range eps.Ports {
|
|
|
|
for _, addr := range ess.Addresses {
|
2022-04-22 16:39:34 +00:00
|
|
|
m := getEndpointSliceLabelsForAddressAndPort(gw, podPortsSeen, addr, eps, ess, epp, p, svc)
|
2023-02-23 01:01:01 +00:00
|
|
|
// Remove possible duplicate labels, which can appear after getEndpointSliceLabelsForAddressAndPort() call
|
2022-11-30 05:22:12 +00:00
|
|
|
m.RemoveDuplicates()
|
2022-04-26 12:25:58 +00:00
|
|
|
ms = append(ms, m)
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append labels for skipped ports on seen pods.
|
|
|
|
portSeen := func(port int, ports []int) bool {
|
|
|
|
for _, p := range ports {
|
|
|
|
if p == port {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for p, ports := range podPortsSeen {
|
|
|
|
for _, c := range p.Spec.Containers {
|
|
|
|
for _, cp := range c.Ports {
|
|
|
|
if portSeen(cp.ContainerPort, ports) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := discoveryutils.JoinHostPort(p.Status.PodIP, cp.ContainerPort)
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.GetLabels()
|
|
|
|
m.Add("__address__", addr)
|
2022-04-26 12:25:58 +00:00
|
|
|
p.appendCommonLabels(m, gw)
|
2020-09-11 09:16:45 +00:00
|
|
|
p.appendContainerLabels(m, c, &cp)
|
2020-12-24 09:26:14 +00:00
|
|
|
if svc != nil {
|
|
|
|
svc.appendCommonLabels(m)
|
|
|
|
}
|
2023-02-23 01:01:01 +00:00
|
|
|
// Remove possible duplicate labels, which can appear after appendCommonLabels() calls
|
2022-11-30 05:22:12 +00:00
|
|
|
m.RemoveDuplicates()
|
2020-09-11 09:16:45 +00:00
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
// getEndpointSliceLabelsForAddressAndPort gets labels for endpointSlice
|
|
|
|
// from address, Endpoint and EndpointPort
|
|
|
|
// enriches labels with TargetRef
|
2021-02-26 14:54:03 +00:00
|
|
|
// p appended to seen Ports
|
2020-09-11 09:16:45 +00:00
|
|
|
// if TargetRef matches
|
2022-04-22 16:39:34 +00:00
|
|
|
func getEndpointSliceLabelsForAddressAndPort(gw *groupWatcher, podPortsSeen map[*Pod][]int, addr string, eps *EndpointSlice, ea Endpoint, epp EndpointPort,
|
2022-11-30 05:22:12 +00:00
|
|
|
p *Pod, svc *Service) *promutils.Labels {
|
2020-09-11 09:16:45 +00:00
|
|
|
m := getEndpointSliceLabels(eps, addr, ea, epp)
|
|
|
|
if svc != nil {
|
|
|
|
svc.appendCommonLabels(m)
|
|
|
|
}
|
2022-02-11 12:54:47 +00:00
|
|
|
// See https://github.com/prometheus/prometheus/issues/10284
|
|
|
|
eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpointslice", m)
|
2020-09-11 09:16:45 +00:00
|
|
|
if ea.TargetRef.Kind != "Pod" || p == nil {
|
|
|
|
return m
|
|
|
|
}
|
2022-02-11 11:34:22 +00:00
|
|
|
// always add pod targetRef, even if epp port doesn't match container port.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2134
|
|
|
|
if _, ok := podPortsSeen[p]; !ok {
|
|
|
|
podPortsSeen[p] = []int{}
|
|
|
|
}
|
2022-04-26 12:25:58 +00:00
|
|
|
p.appendCommonLabels(m, gw)
|
2020-09-11 09:16:45 +00:00
|
|
|
for _, c := range p.Spec.Containers {
|
|
|
|
for _, cp := range c.Ports {
|
|
|
|
if cp.ContainerPort == epp.Port {
|
|
|
|
podPortsSeen[p] = append(podPortsSeen[p], cp.ContainerPort)
|
2022-02-11 11:34:22 +00:00
|
|
|
p.appendContainerLabels(m, c, &cp)
|
2020-09-11 09:16:45 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// //getEndpointSliceLabels builds labels for given EndpointSlice
|
2022-11-30 05:22:12 +00:00
|
|
|
func getEndpointSliceLabels(eps *EndpointSlice, addr string, ea Endpoint, epp EndpointPort) *promutils.Labels {
|
2020-09-11 09:16:45 +00:00
|
|
|
addr = discoveryutils.JoinHostPort(addr, epp.Port)
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.GetLabels()
|
|
|
|
m.Add("__address__", addr)
|
|
|
|
m.Add("__meta_kubernetes_namespace", eps.Metadata.Namespace)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_name", eps.Metadata.Name)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_address_type", eps.AddressType)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_endpoint_conditions_ready", strconv.FormatBool(ea.Conditions.Ready))
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_port_name", epp.Name)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_port_protocol", epp.Protocol)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_port", strconv.Itoa(epp.Port))
|
2020-09-11 09:16:45 +00:00
|
|
|
if epp.AppProtocol != "" {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add("__meta_kubernetes_endpointslice_port_app_protocol", epp.AppProtocol)
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
if ea.TargetRef.Kind != "" {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add("__meta_kubernetes_endpointslice_address_target_kind", ea.TargetRef.Kind)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_address_target_name", ea.TargetRef.Name)
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
if ea.Hostname != "" {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add("__meta_kubernetes_endpointslice_endpoint_hostname", ea.Hostname)
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
for k, v := range ea.Topology {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(discoveryutils.SanitizeLabelName("__meta_kubernetes_endpointslice_endpoint_topology_"+k), v)
|
|
|
|
m.Add(discoveryutils.SanitizeLabelName("__meta_kubernetes_endpointslice_endpoint_topology_present_"+k), "true")
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-08-29 08:16:40 +00:00
|
|
|
// EndpointSliceList - implements kubernetes endpoint slice list object, that groups service endpoints slices.
|
|
|
|
//
|
|
|
|
// See https://v1-21.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#endpointslicelist-v1-discovery-k8s-io
|
2020-09-11 09:16:45 +00:00
|
|
|
type EndpointSliceList struct {
|
2021-02-26 14:54:03 +00:00
|
|
|
Metadata ListMeta
|
|
|
|
Items []*EndpointSlice
|
2020-09-11 09:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointSlice - implements kubernetes endpoint slice.
|
2021-08-29 08:16:40 +00:00
|
|
|
//
|
|
|
|
// See https://v1-21.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#endpointslice-v1-discovery-k8s-io
|
2020-09-11 09:16:45 +00:00
|
|
|
type EndpointSlice struct {
|
|
|
|
Metadata ObjectMeta
|
|
|
|
Endpoints []Endpoint
|
|
|
|
AddressType string
|
|
|
|
Ports []EndpointPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoint implements kubernetes object endpoint for endpoint slice.
|
2021-08-29 08:16:40 +00:00
|
|
|
//
|
|
|
|
// See https://v1-21.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#endpoint-v1-discovery-k8s-io
|
2020-09-11 09:16:45 +00:00
|
|
|
type Endpoint struct {
|
|
|
|
Addresses []string
|
|
|
|
Conditions EndpointConditions
|
|
|
|
Hostname string
|
|
|
|
TargetRef ObjectReference
|
|
|
|
Topology map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointConditions implements kubernetes endpoint condition.
|
2021-08-29 08:16:40 +00:00
|
|
|
//
|
|
|
|
// See https://v1-21.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#endpointconditions-v1-discovery-k8s-io
|
2020-09-11 09:16:45 +00:00
|
|
|
type EndpointConditions struct {
|
|
|
|
Ready bool
|
|
|
|
}
|