Skip to content

Commit

Permalink
Kubernetes service discovery: add provider ID label (#9603)
Browse files Browse the repository at this point in the history
When using Kubernetes on cloud providers, nodes will have the
spec.providerID field populated to contain the cloud provider specific
name of the EC2/GCE/...  instance.

Let's expose this information as an additional label, so that it's
easier to annotate metrics and alerts to contain the cloud provider
specific name of the instance to which it pertains.

Signed-off-by: Ed Schouten <eschouten@apple.com>
  • Loading branch information
EdSchouten committed Dec 6, 2021
1 parent 084bd70 commit a3e9628
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions discovery/kubernetes/node.go
Expand Up @@ -149,6 +149,7 @@ func nodeSourceFromName(name string) string {

const (
nodeNameLabel = metaLabelPrefix + "node_name"
nodeProviderIDLabel = metaLabelPrefix + "node_provider_id"
nodeLabelPrefix = metaLabelPrefix + "node_label_"
nodeLabelPresentPrefix = metaLabelPrefix + "node_labelpresent_"
nodeAnnotationPrefix = metaLabelPrefix + "node_annotation_"
Expand All @@ -161,6 +162,7 @@ func nodeLabels(n *apiv1.Node) model.LabelSet {
ls := make(model.LabelSet, 2*(len(n.Labels)+len(n.Annotations))+1)

ls[nodeNameLabel] = lv(n.Name)
ls[nodeProviderIDLabel] = lv(n.Spec.ProviderID)

for k, v := range n.Labels {
ln := strutil.SanitizeLabelName(k)
Expand Down
14 changes: 11 additions & 3 deletions discovery/kubernetes/node_test.go
Expand Up @@ -25,13 +25,16 @@ import (
"github.com/prometheus/prometheus/discovery/targetgroup"
)

func makeNode(name, address string, labels, annotations map[string]string) *v1.Node {
func makeNode(name, address, providerID string, labels, annotations map[string]string) *v1.Node {
return &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
Annotations: annotations,
},
Spec: v1.NodeSpec{
ProviderID: providerID,
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Expand All @@ -49,7 +52,7 @@ func makeNode(name, address string, labels, annotations map[string]string) *v1.N
}

func makeEnumeratedNode(i int) *v1.Node {
return makeNode(fmt.Sprintf("test%d", i), "1.2.3.4", map[string]string{}, map[string]string{})
return makeNode(fmt.Sprintf("test%d", i), "1.2.3.4", fmt.Sprintf("aws:///de-west-3a/i-%d", i), map[string]string{}, map[string]string{})
}

func TestNodeDiscoveryBeforeStart(t *testing.T) {
Expand All @@ -61,6 +64,7 @@ func TestNodeDiscoveryBeforeStart(t *testing.T) {
obj := makeNode(
"test",
"1.2.3.4",
"aws:///nl-north-7b/i-03149834983492827",
map[string]string{"test-label": "testvalue"},
map[string]string{"test-annotation": "testannotationvalue"},
)
Expand All @@ -78,6 +82,7 @@ func TestNodeDiscoveryBeforeStart(t *testing.T) {
},
Labels: model.LabelSet{
"__meta_kubernetes_node_name": "test",
"__meta_kubernetes_node_provider_id": "aws:///nl-north-7b/i-03149834983492827",
"__meta_kubernetes_node_label_test_label": "testvalue",
"__meta_kubernetes_node_labelpresent_test_label": "true",
"__meta_kubernetes_node_annotation_test_annotation": "testannotationvalue",
Expand Down Expand Up @@ -109,7 +114,8 @@ func TestNodeDiscoveryAdd(t *testing.T) {
},
},
Labels: model.LabelSet{
"__meta_kubernetes_node_name": "test1",
"__meta_kubernetes_node_name": "test1",
"__meta_kubernetes_node_provider_id": "aws:///de-west-3a/i-1",
},
Source: "node/test1",
},
Expand Down Expand Up @@ -146,6 +152,7 @@ func TestNodeDiscoveryUpdate(t *testing.T) {
obj2 := makeNode(
"test0",
"1.2.3.4",
"aws:///fr-south-1c/i-49508290343823952",
map[string]string{"Unschedulable": "true"},
map[string]string{},
)
Expand All @@ -165,6 +172,7 @@ func TestNodeDiscoveryUpdate(t *testing.T) {
"__meta_kubernetes_node_label_Unschedulable": "true",
"__meta_kubernetes_node_labelpresent_Unschedulable": "true",
"__meta_kubernetes_node_name": "test0",
"__meta_kubernetes_node_provider_id": "aws:///fr-south-1c/i-49508290343823952",
},
Source: "node/test0",
},
Expand Down
1 change: 1 addition & 0 deletions docs/configuration/configuration.md
Expand Up @@ -1513,6 +1513,7 @@ node object in the address type order of `NodeInternalIP`, `NodeExternalIP`,
Available meta labels:

* `__meta_kubernetes_node_name`: The name of the node object.
* `__meta_kubernetes_node_provider_id`: The cloud provider's name for the node object.
* `__meta_kubernetes_node_label_<labelname>`: Each label from the node object.
* `__meta_kubernetes_node_labelpresent_<labelname>`: `true` for each label from the node object.
* `__meta_kubernetes_node_annotation_<annotationname>`: Each annotation from the node object.
Expand Down

0 comments on commit a3e9628

Please sign in to comment.