2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-23 08:34:04 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
2021-02-26 14:54:03 +00:00
|
|
|
func (eps *Endpoints) key() string {
|
|
|
|
return eps.Metadata.key()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEndpointsList(data []byte) (map[string]object, ListMeta, error) {
|
|
|
|
var epsl EndpointsList
|
|
|
|
if err := json.Unmarshal(data, &epsl); err != nil {
|
|
|
|
return nil, epsl.Metadata, fmt.Errorf("cannot unmarshal EndpointsList from %q: %w", data, err)
|
|
|
|
}
|
|
|
|
objectsByKey := make(map[string]object)
|
|
|
|
for _, eps := range epsl.Items {
|
|
|
|
objectsByKey[eps.key()] = eps
|
|
|
|
}
|
|
|
|
return objectsByKey, epsl.Metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEndpoints(data []byte) (object, error) {
|
|
|
|
var eps Endpoints
|
|
|
|
if err := json.Unmarshal(data, &eps); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &eps, nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:02:27 +00:00
|
|
|
// EndpointsList implements k8s endpoints list.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpointslist-v1-core
|
|
|
|
type EndpointsList struct {
|
2021-02-26 14:54:03 +00:00
|
|
|
Metadata ListMeta
|
|
|
|
Items []*Endpoints
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Endpoints implements k8s endpoints.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpoints-v1-core
|
|
|
|
type Endpoints struct {
|
|
|
|
Metadata ObjectMeta
|
|
|
|
Subsets []EndpointSubset
|
|
|
|
}
|
|
|
|
|
2021-03-10 13:06:33 +00:00
|
|
|
func (eps *Endpoints) resourceVersion() string {
|
|
|
|
return eps.Metadata.ResourceVersion
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:02:27 +00:00
|
|
|
// EndpointSubset implements k8s endpoint subset.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpointsubset-v1-core
|
|
|
|
type EndpointSubset struct {
|
|
|
|
Addresses []EndpointAddress
|
|
|
|
NotReadyAddresses []EndpointAddress
|
|
|
|
Ports []EndpointPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointAddress implements k8s endpoint address.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpointaddress-v1-core
|
|
|
|
type EndpointAddress struct {
|
|
|
|
Hostname string
|
|
|
|
IP string
|
|
|
|
NodeName string
|
|
|
|
TargetRef ObjectReference
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectReference implements k8s object reference.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#objectreference-v1-core
|
|
|
|
type ObjectReference struct {
|
|
|
|
Kind string
|
|
|
|
Name string
|
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointPort implements k8s endpoint port.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpointport-v1beta1-discovery-k8s-io
|
|
|
|
type EndpointPort struct {
|
|
|
|
AppProtocol string
|
|
|
|
Name string
|
|
|
|
Port int
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:21:27 +00:00
|
|
|
// getTargetLabels returns labels for each endpoint in eps.
|
2020-04-13 18:02:27 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#endpoints
|
2021-02-26 18:21:27 +00:00
|
|
|
func (eps *Endpoints) getTargetLabels(aw *apiWatcher) []map[string]string {
|
2021-02-26 14:46:13 +00:00
|
|
|
var svc *Service
|
2021-02-26 14:54:03 +00:00
|
|
|
if o := aw.getObjectByRole("service", eps.Metadata.Namespace, eps.Metadata.Name); o != nil {
|
|
|
|
svc = o.(*Service)
|
2021-02-26 14:46:13 +00:00
|
|
|
}
|
2020-04-13 18:02:27 +00:00
|
|
|
podPortsSeen := make(map[*Pod][]int)
|
2021-02-26 18:21:27 +00:00
|
|
|
var ms []map[string]string
|
2020-04-13 18:02:27 +00:00
|
|
|
for _, ess := range eps.Subsets {
|
|
|
|
for _, epp := range ess.Ports {
|
2021-02-26 14:54:03 +00:00
|
|
|
ms = appendEndpointLabelsForAddresses(ms, aw, podPortsSeen, eps, ess.Addresses, epp, svc, "true")
|
|
|
|
ms = appendEndpointLabelsForAddresses(ms, aw, podPortsSeen, eps, ess.NotReadyAddresses, epp, svc, "false")
|
2020-04-13 18:02:27 +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
|
|
|
|
}
|
2020-04-23 08:34:04 +00:00
|
|
|
addr := discoveryutils.JoinHostPort(p.Status.PodIP, cp.ContainerPort)
|
2020-04-13 18:02:27 +00:00
|
|
|
m := map[string]string{
|
|
|
|
"__address__": addr,
|
|
|
|
}
|
|
|
|
p.appendCommonLabels(m)
|
|
|
|
p.appendContainerLabels(m, c, &cp)
|
2020-12-24 09:26:14 +00:00
|
|
|
if svc != nil {
|
|
|
|
svc.appendCommonLabels(m)
|
|
|
|
}
|
2020-04-13 18:02:27 +00:00
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:54:03 +00:00
|
|
|
func appendEndpointLabelsForAddresses(ms []map[string]string, aw *apiWatcher, podPortsSeen map[*Pod][]int, eps *Endpoints,
|
|
|
|
eas []EndpointAddress, epp EndpointPort, svc *Service, ready string) []map[string]string {
|
2020-04-13 18:02:27 +00:00
|
|
|
for _, ea := range eas {
|
2021-02-26 14:46:13 +00:00
|
|
|
var p *Pod
|
2021-02-26 14:54:03 +00:00
|
|
|
if o := aw.getObjectByRole("pod", ea.TargetRef.Namespace, ea.TargetRef.Name); o != nil {
|
|
|
|
p = o.(*Pod)
|
2021-02-26 14:46:13 +00:00
|
|
|
}
|
2020-04-13 18:02:27 +00:00
|
|
|
m := getEndpointLabelsForAddressAndPort(podPortsSeen, eps, ea, epp, p, svc, ready)
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEndpointLabelsForAddressAndPort(podPortsSeen map[*Pod][]int, eps *Endpoints, ea EndpointAddress, epp EndpointPort, p *Pod, svc *Service, ready string) map[string]string {
|
|
|
|
m := getEndpointLabels(eps.Metadata, ea, epp, ready)
|
|
|
|
if svc != nil {
|
|
|
|
svc.appendCommonLabels(m)
|
|
|
|
}
|
2021-02-15 00:46:14 +00:00
|
|
|
eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpoints", m)
|
2020-04-13 18:02:27 +00:00
|
|
|
if ea.TargetRef.Kind != "Pod" || p == nil {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
p.appendCommonLabels(m)
|
|
|
|
for _, c := range p.Spec.Containers {
|
|
|
|
for _, cp := range c.Ports {
|
|
|
|
if cp.ContainerPort == epp.Port {
|
|
|
|
p.appendContainerLabels(m, c, &cp)
|
|
|
|
podPortsSeen[p] = append(podPortsSeen[p], cp.ContainerPort)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEndpointLabels(om ObjectMeta, ea EndpointAddress, epp EndpointPort, ready string) map[string]string {
|
2020-04-23 08:34:04 +00:00
|
|
|
addr := discoveryutils.JoinHostPort(ea.IP, epp.Port)
|
2020-04-13 18:02:27 +00:00
|
|
|
m := map[string]string{
|
|
|
|
"__address__": addr,
|
|
|
|
"__meta_kubernetes_namespace": om.Namespace,
|
|
|
|
"__meta_kubernetes_endpoints_name": om.Name,
|
|
|
|
|
|
|
|
"__meta_kubernetes_endpoint_ready": ready,
|
|
|
|
"__meta_kubernetes_endpoint_port_name": epp.Name,
|
|
|
|
"__meta_kubernetes_endpoint_port_protocol": epp.Protocol,
|
|
|
|
}
|
|
|
|
if ea.TargetRef.Kind != "" {
|
|
|
|
m["__meta_kubernetes_endpoint_address_target_kind"] = ea.TargetRef.Kind
|
|
|
|
m["__meta_kubernetes_endpoint_address_target_name"] = ea.TargetRef.Name
|
|
|
|
}
|
|
|
|
if ea.NodeName != "" {
|
|
|
|
m["__meta_kubernetes_endpoint_node_name"] = ea.NodeName
|
|
|
|
}
|
|
|
|
if ea.Hostname != "" {
|
|
|
|
m["__meta_kubernetes_endpoint_hostname"] = ea.Hostname
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|