From a761cb3fbe1145ad4a9273137522b886e094ec21 Mon Sep 17 00:00:00 2001 From: mju Date: Mon, 16 Aug 2021 11:40:41 -0700 Subject: [PATCH] Add Connection resource map (#289) --- retrieval/resource_map.go | 11 +++++++++++ retrieval/resource_map_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/retrieval/resource_map.go b/retrieval/resource_map.go index 853934ae..f5e10d96 100644 --- a/retrieval/resource_map.go +++ b/retrieval/resource_map.go @@ -147,6 +147,16 @@ var TargetV2ResourceMap = ResourceMap{ }, } +var ConnectionResourceMap = ResourceMap{ + Type: "connectors.googleapis.com/Connection", + MatchLabel: "__meta_kubernetes_pod_label_type_connection", + LabelMap: map[string]labelTranslation{ + ProjectIDLabel: constValue("resource_container"), + KubernetesLocationLabel: constValue("location"), + "connection": constValue("connection"), + }, +} + var AnthosL4LBMap = ResourceMap{ Type: "anthos_l4lb", MatchLabel: "__meta_kubernetes_service_annotation_gke_googleapis_com_anthos_l4lb_type", @@ -164,6 +174,7 @@ type ResourceMapList []ResourceMap // When you add new elements, you also probably want to update TestResourceMappingsOrder. var ResourceMappings = ResourceMapList{ AnthosL4LBMap, + ConnectionResourceMap, TargetV2ResourceMap, ProxyV2ResourceMap, ProxyResourceMap, diff --git a/retrieval/resource_map_test.go b/retrieval/resource_map_test.go index a52a0583..3f8c4d33 100644 --- a/retrieval/resource_map_test.go +++ b/retrieval/resource_map_test.go @@ -285,6 +285,36 @@ func TestTranslateProxyV2(t *testing.T) { } } +func TestTranslateConnection(t *testing.T) { + discoveredLabels := labels.Labels{ + {"__meta_kubernetes_pod_label_type_connection", "true"}, + {ProjectIDLabel, "my-project"}, + {KubernetesLocationLabel, "us-central1-a"}, + } + metricLabels := labels.Labels{ + {"connection", "my-connection-name"}, + {"extra_label", "my-label"}, + } + expectedLabels := map[string]string{ + "resource_container": "my-project", + "location": "us-central1-a", + "connection": "my-connection-name", + } + expectedFinalLabels := labels.Labels{ + {"extra_label", "my-label"}, + } + if labels, finalLabels := ConnectionResourceMap.Translate(discoveredLabels, metricLabels); labels == nil { + t.Errorf("Expected %v, actual nil", expectedLabels) + } else { + if diff := cmp.Diff(expectedLabels, labels); len(diff) > 0 { + t.Error(diff) + } + if diff := cmp.Diff(expectedFinalLabels, finalLabels); len(diff) > 0 { + t.Error(diff) + } + } +} + func TestTranslateAnthosL4LB(t *testing.T) { discoveredLabels := labels.Labels{ {ProjectIDLabel, "my-project"}, @@ -357,6 +387,7 @@ func TestResourceMappingsOrder(t *testing.T) { {"k8s_node", "gce_instance"}, {"k8s_node", "aws_ec2_instance"}, {"anthos_l4lb", "k8s_container"}, + {"connectors.googleapis.com/Connection", "k8s_container"}, {"apigee.googleapis.com/TargetV2", "k8s_container"}, {"apigee.googleapis.com/ProxyV2", "k8s_container"}, {"apigee.googleapis.com/Proxy", "k8s_container"},