Skip to content

Commit

Permalink
Merge pull request #95 from grafana/paula/add-pv-name-to-disk-size-me…
Browse files Browse the repository at this point in the history
…tric

Add created_for_pv label to unused_disk_size_bytes metric
  • Loading branch information
paulajulve authored May 21, 2024
2 parents a8a24fd + a35e73a commit 8e72cb9
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/unused-exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func registerExporter(ctx context.Context, providers []unused.Provider, cfg conf
ds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "disk", "size_bytes"),
"Disk size in bytes",
append(labels, []string{"disk", "k8s_namespace", "type", "region", "zone"}...),
append(labels, []string{"disk", "created_for_pv", "k8s_namespace", "type", "region", "zone"}...),
nil),

size: prometheus.NewDesc(
Expand Down Expand Up @@ -186,7 +186,7 @@ func (e *exporter) pollProvider(p unused.Provider) {
}

addMetric(&ms, p, e.dlu, lastUsedTS(d), d.ID(), m.CreatedForPV(), m.CreatedForPVC(), m.Zone())
addMetric(&ms, p, e.ds, d.SizeBytes(), d.ID(), ns, string(d.DiskType()), getRegionFromZone(p, m.Zone()), m.Zone())
addMetric(&ms, p, e.ds, d.SizeBytes(), d.ID(), m.CreatedForPV(), ns, string(d.DiskType()), getRegionFromZone(p, m.Zone()), m.Zone())
}

addMetric(&ms, p, e.info, 1)
Expand Down
4 changes: 0 additions & 4 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ func (m Meta) CreatedForNamespace() string {
return m.coalesce("kubernetes.io/created-for/pvc/namespace", "kubernetes.io-created-for-pvc-namespace")
}

func (m Meta) CreatedBy() string {
return m.coalesce("storage.gke.io/created-by", "created-by")
}

func (m Meta) Zone() string {
return m.coalesce("zone", "location")
}
Expand Down
128 changes: 128 additions & 0 deletions meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,131 @@ func TestEquals(t *testing.T) {
})
}
}

func TestCreatedForPV(t *testing.T) {
tests := []struct {
name string
m Meta
expected string
}{
{
name: "GCP disk",
m: Meta{"kubernetes.io/created-for/pv/name": "pvc-c898536e-1601-4357-af13-01bbe82f3055"},
expected: "pvc-c898536e-1601-4357-af13-01bbe82f3055",
},
{
name: "AWS disk",
m: Meta{"kubernetes.io/created-for/pv/name": "pvc-b78d13ec-426f-4ec6-80aa-231a7d4e7db9"},
expected: "pvc-b78d13ec-426f-4ec6-80aa-231a7d4e7db9",
},
{
name: "Azure disk",
m: Meta{"kubernetes.io-created-for-pv-name": "pvc-10df52de-2b9d-44a2-8901-4cbfc4871f8c"},
expected: "pvc-10df52de-2b9d-44a2-8901-4cbfc4871f8c",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.m.CreatedForPV()
if tt.expected != actual {
t.Fatalf("expected %v but got %v", tt.expected, actual)
}
})
}
}

func TestCreatedForPVC(t *testing.T) {
tests := []struct {
name string
m Meta
expected string
}{
{
name: "GCP disk",
m: Meta{"kubernetes.io/created-for/pvc/name": "qwerty"},
expected: "qwerty",
},
{
name: "AWS disk",
m: Meta{"kubernetes.io/created-for/pvc/name": "asdf"},
expected: "asdf",
},
{
name: "Azure disk",
m: Meta{"kubernetes.io-created-for-pvc-name": "zxcv"},
expected: "zxcv",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.m.CreatedForPVC()
if tt.expected != actual {
t.Fatalf("expected %v but got %v", tt.expected, actual)
}
})
}
}

func TestCreatedForNamespace(t *testing.T) {
tests := []struct {
name string
m Meta
expected string
}{
{
name: "GCP disk",
m: Meta{"kubernetes.io/created-for/pvc/namespace": "ns1"},
expected: "ns1",
},
{
name: "AWS disk",
m: Meta{"kubernetes.io/created-for/pvc/namespace": "ns2"},
expected: "ns2",
},
{
name: "Azure disk",
m: Meta{"kubernetes.io-created-for-pvc-namespace": "ns3"},
expected: "ns3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.m.CreatedForNamespace()
if tt.expected != actual {
t.Fatalf("expected %v but got %v", tt.expected, actual)
}
})
}
}

func TestZone(t *testing.T) {
tests := []struct {
name string
m Meta
expected string
}{
{
name: "GCP disk",
m: Meta{"zone": "asia-south1-a"},
expected: "asia-south1-a",
},
{
name: "AWS disk",
m: Meta{"zone": "us-east-2a"},
expected: "us-east-2a",
},
{
name: "Azure disk",
m: Meta{"location": "Central US"},
expected: "Central US",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.m.Zone()
if tt.expected != actual {
t.Fatalf("expected %v but got %v", tt.expected, actual)
}
})
}
}

0 comments on commit 8e72cb9

Please sign in to comment.