From 7c3eb4e653360e451ceffb331bd0fb01149a5f82 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 2 Apr 2024 15:01:58 -0400 Subject: [PATCH] fix(google): Deduplicate disks to export (#144) Introduce a simple map to keep track of seen disks to ensure we do not emit metrics for disks already seen. - closes #143 --- pkg/google/gke/gke.go | 5 +++++ pkg/google/gke/gke_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/google/gke/gke.go b/pkg/google/gke/gke.go index 09327111..c03f10d3 100644 --- a/pkg/google/gke/gke.go +++ b/pkg/google/gke/gke.go @@ -169,6 +169,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error { ) } } + seenDisks := make(map[string]bool) for group := range disks { for _, disk := range group { clusterName := disk.Labels[gcpCompute.GkeClusterLabel] @@ -176,6 +177,10 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error { namespace := getNamespaceFromDisk(disk) name := getNameFromDisk(disk) + if _, ok := seenDisks[name]; ok { + continue + } + seenDisks[name] = true diskType := strings.Split(disk.Type, "/") storageClass := diskType[len(diskType)-1] labelValues := []string{ diff --git a/pkg/google/gke/gke_test.go b/pkg/google/gke/gke_test.go index 1a2ad855..d36083ea 100644 --- a/pkg/google/gke/gke_test.go +++ b/pkg/google/gke/gke_test.go @@ -382,6 +382,17 @@ func TestCollector_Collect(t *testing.T) { Type: "pd-ssd", SizeGb: 600, }, + { + // Introduce a duplicated disk to ensure the duplicate doesn't cause an issue emitting metrics + Name: "test-ssd-disk", + Zone: "testing/us-east4", + Labels: map[string]string{ + compute.GkeClusterLabel: "test", + }, + Description: `{"kubernetes.io/created-for/pvc/namespace":"cloudcost-exporter"}`, + Type: "pd-ssd", + SizeGb: 600, + }, }, } default: