2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-03-11 14:41:09 +00:00
|
|
|
"io"
|
2020-04-23 08:34:04 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 11:45:08 +00:00
|
|
|
// getNodesLabels returns labels for k8s nodes obtained from the given cfg
|
|
|
|
func (n *Node) key() string {
|
|
|
|
return n.Metadata.key()
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 14:41:09 +00:00
|
|
|
func parseNodeList(r io.Reader) (map[string]object, ListMeta, error) {
|
2021-02-26 14:54:03 +00:00
|
|
|
var nl NodeList
|
2021-03-11 14:41:09 +00:00
|
|
|
d := json.NewDecoder(r)
|
|
|
|
if err := d.Decode(&nl); err != nil {
|
|
|
|
return nil, nl.Metadata, fmt.Errorf("cannot unmarshal NodeList: %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 _, n := range nl.Items {
|
2021-04-02 11:45:08 +00:00
|
|
|
objectsByKey[n.key()] = n
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
2021-04-02 11:45:08 +00:00
|
|
|
return objectsByKey, nl.Metadata, nil
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseNode(data []byte) (object, error) {
|
|
|
|
var n Node
|
|
|
|
if err := json.Unmarshal(data, &n); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:02:27 +00:00
|
|
|
// NodeList represents NodeList from k8s API.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodelist-v1-core
|
|
|
|
type NodeList struct {
|
2021-02-26 14:54:03 +00:00
|
|
|
Metadata ListMeta
|
|
|
|
Items []*Node
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Node represents Node from k8s API.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#node-v1-core
|
|
|
|
type Node struct {
|
|
|
|
Metadata ObjectMeta
|
|
|
|
Status NodeStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeStatus represents NodeStatus from k8s API.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodestatus-v1-core
|
|
|
|
type NodeStatus struct {
|
|
|
|
Addresses []NodeAddress
|
|
|
|
DaemonEndpoints NodeDaemonEndpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeAddress represents NodeAddress from k8s API.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodeaddress-v1-core
|
|
|
|
type NodeAddress struct {
|
|
|
|
Type string
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeDaemonEndpoints represents NodeDaemonEndpoints from k8s API.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodedaemonendpoints-v1-core
|
|
|
|
type NodeDaemonEndpoints struct {
|
|
|
|
KubeletEndpoint DaemonEndpoint
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:21:27 +00:00
|
|
|
// getTargetLabels returs labels for the given n.
|
2020-04-13 18:02:27 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#node
|
2021-03-14 19:10:35 +00:00
|
|
|
func (n *Node) getTargetLabels(gw *groupWatcher) []map[string]string {
|
2020-04-13 18:02:27 +00:00
|
|
|
addr := getNodeAddr(n.Status.Addresses)
|
|
|
|
if len(addr) == 0 {
|
|
|
|
// Skip node without address
|
2021-02-26 18:21:27 +00:00
|
|
|
return nil
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2020-04-23 08:34:04 +00:00
|
|
|
addr = discoveryutils.JoinHostPort(addr, n.Status.DaemonEndpoints.KubeletEndpoint.Port)
|
2020-04-13 18:02:27 +00:00
|
|
|
m := map[string]string{
|
|
|
|
"__address__": addr,
|
|
|
|
"instance": n.Metadata.Name,
|
|
|
|
"__meta_kubernetes_node_name": n.Metadata.Name,
|
|
|
|
}
|
|
|
|
n.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_node", m)
|
|
|
|
addrTypesUsed := make(map[string]bool, len(n.Status.Addresses))
|
|
|
|
for _, a := range n.Status.Addresses {
|
|
|
|
if addrTypesUsed[a.Type] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrTypesUsed[a.Type] = true
|
2020-04-23 08:34:04 +00:00
|
|
|
ln := discoveryutils.SanitizeLabelName(a.Type)
|
2020-11-07 11:03:44 +00:00
|
|
|
m["__meta_kubernetes_node_address_"+ln] = a.Address
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2021-02-26 18:21:27 +00:00
|
|
|
return []map[string]string{m}
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNodeAddr(nas []NodeAddress) string {
|
|
|
|
if addr := getAddrByType(nas, "InternalIP"); len(addr) > 0 {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
if addr := getAddrByType(nas, "InternalDNS"); len(addr) > 0 {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
if addr := getAddrByType(nas, "ExternalIP"); len(addr) > 0 {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
if addr := getAddrByType(nas, "ExternalDNS"); len(addr) > 0 {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
if addr := getAddrByType(nas, "LegacyHostIP"); len(addr) > 0 {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
if addr := getAddrByType(nas, "Hostname"); len(addr) > 0 {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAddrByType(nas []NodeAddress, typ string) string {
|
|
|
|
for _, na := range nas {
|
|
|
|
if na.Type == typ {
|
|
|
|
return na.Address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|