From 5e806e09a6760cacaef80b2281891eabf021582b Mon Sep 17 00:00:00 2001 From: olagacek Date: Wed, 7 Aug 2019 01:19:39 +0200 Subject: [PATCH] Added mapping for kubernetes pod resource (#174) --- resource.go | 22 +++++++++++++--------- resource_test.go | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/resource.go b/resource.go index 5e2fbce..855007e 100644 --- a/resource.go +++ b/resource.go @@ -90,18 +90,22 @@ func defaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResourc if res == nil || res.Labels == nil { return result } - if res.Type == resourcekeys.ContainerType { + + switch { + case res.Type == resourcekeys.ContainerType: result.Type = "k8s_container" match = k8sResourceMap - } else if v, ok := res.Labels[resourcekeys.CloudKeyProvider]; ok { - if v == resourcekeys.CloudProviderGCP { - result.Type = "gce_instance" - match = gcpResourceMap - } else if v == resourcekeys.CloudProviderAWS { - result.Type = "aws_ec2_instance" - match = awsResourceMap - } + case res.Type == resourcekeys.K8SType: + result.Type = "k8s_pod" + match = k8sResourceMap + case res.Labels[resourcekeys.CloudKeyProvider] == resourcekeys.CloudProviderGCP: + result.Type = "gce_instance" + match = gcpResourceMap + case res.Labels[resourcekeys.CloudKeyProvider] == resourcekeys.CloudProviderAWS: + result.Type = "aws_ec2_instance" + match = awsResourceMap } + result.Labels = transformResource(match, res.Labels) if result.Type == "aws_ec2_instance" { if v, ok := result.Labels["region"]; ok { diff --git a/resource_test.go b/resource_test.go index d99dfa5..4072649 100644 --- a/resource_test.go +++ b/resource_test.go @@ -58,6 +58,28 @@ func TestDefaultMapResource(t *testing.T) { }, }, }, + { + input: &resource.Resource{ + Type: resourcekeys.K8SType, + Labels: map[string]string{ + stackdriverProjectID: "proj1", + resourcekeys.K8SKeyClusterName: "cluster1", + resourcekeys.K8SKeyPodName: "pod1", + resourcekeys.K8SKeyNamespaceName: "namespace1", + resourcekeys.CloudKeyZone: "zone1", + }, + }, + want: &monitoredrespb.MonitoredResource{ + Type: "k8s_pod", + Labels: map[string]string{ + "project_id": "proj1", + "location": "zone1", + "cluster_name": "cluster1", + "namespace_name": "namespace1", + "pod_name": "pod1", + }, + }, + }, { input: &resource.Resource{ Type: resourcekeys.CloudType,