Skip to content

Commit

Permalink
fix(google): Deduplicate disks to export (#144)
Browse files Browse the repository at this point in the history
Introduce a simple map to keep track of seen disks to ensure we do not emit metrics for disks already seen.

- closes #143
  • Loading branch information
Pokom authored Apr 2, 2024
1 parent 225e2b3 commit 7c3eb4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/google/gke/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,18 @@ 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]
region := getRegionFromDisk(disk)

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{
Expand Down
11 changes: 11 additions & 0 deletions pkg/google/gke/gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7c3eb4e

Please sign in to comment.