Skip to content

Commit

Permalink
fix(pvc): Get disk name from k8s labels instead (#142)
Browse files Browse the repository at this point in the history
When comparing the output data from opencost and cloudcost exporter, one bit that didn't quite make sense was a mismatch in PVC's between the two. The total costs between the two was small enough to be insignificant, which only made things more confusing.

After analyzing the metrics further, the biggest difference was the name of the disks. `cloudcost-exporter` was pulling the name directly from the disk metadata, which was prepending most pv's with `gke-$cluster`. The problem with this is that `kube-state-metrics` and other metrics only look at what k8s calls them.

This change updates how name is sourced.
1. Loads the `disk.Description` into a map
2. Check for the existence of two labels
3. If either keys are found, return the value
4. Otherwise, fall back to `disk.Name` as a final resource
  • Loading branch information
Pokom authored Apr 1, 2024
1 parent 6315917 commit ad1e8a9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
59 changes: 42 additions & 17 deletions pkg/google/gke/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,13 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
region := getRegionFromDisk(disk)

namespace := getNamespaceFromDisk(disk)
name := getNameFromDisk(disk)
diskType := strings.Split(disk.Type, "/")
storageClass := diskType[len(diskType)-1]
labelValues := []string{
clusterName,
namespace,
disk.Name,
name,
region,
project,
storageClass,
Expand Down Expand Up @@ -247,22 +248,7 @@ func getNamespaceFromDisk(disk *compute.Disk) string {
if err != nil {
return ""
}
for _, key := range keys {
if val, ok := desc[key]; ok {
return val
}
}
return ""
}

func extractLabelsFromDesc(description string, labels map[string]string) error {
if description == "" {
return nil
}
if err := json.Unmarshal([]byte(description), &labels); err != nil {
return err
}
return nil
return coalesce(desc, keys...)
}

func getRegionFromDisk(disk *compute.Disk) string {
Expand All @@ -281,3 +267,42 @@ func getRegionFromDisk(disk *compute.Disk) string {
}
return zone[:strings.LastIndex(zone, "-")]
}

// getNameFromDisk will return the name of the disk. If the disk has a label "kubernetes.io/created-for/pv/name" it will return the value stored in that key.
// otherwise it will return the disk name that is directly associated with the disk.
func getNameFromDisk(disk *compute.Disk) string {
desc := make(map[string]string)
err := extractLabelsFromDesc(disk.Description, desc)
if err != nil {
return disk.Name
}
// first check that the key exists in the map, if it does return the value
name := coalesce(desc, "kubernetes.io/created-for/pv/name", "kubernetes.io-created-for/pv-name")
if name != "" {
return name
}
return disk.Name
}

// coalesce will take a map and a list of keys and return the first value that is found in the map. If no value is found it will return an empty string
func coalesce(desc map[string]string, keys ...string) string {
for _, key := range keys {
if val, ok := desc[key]; ok {
return val
}
}
return ""
}

// extractLabelsFromDesc will take a description string and extract the labels from it. GKE disks store their description as
// a json blob in the description field. This function will extract the labels from that json blob and return them as a map
// Some useful information about the json blob are name, cluster, namespace, and pvc's that the disk is associated with
func extractLabelsFromDesc(description string, labels map[string]string) error {
if description == "" {
return nil
}
if err := json.Unmarshal([]byte(description), &labels); err != nil {
return err
}
return nil
}
52 changes: 52 additions & 0 deletions pkg/google/gke/gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,55 @@ func Test_getRegionFromDisk(t *testing.T) {
})
}
}

func Test_getNameFromDisk(t *testing.T) {
tests := map[string]struct {
disk *computev1.Disk
want string
}{
"Empty description should return an empty string": {
disk: &computev1.Disk{
Description: "",
},
want: "",
},
"Description not formatted as json should return an empty string": {
disk: &computev1.Disk{
Description: "test",
},
want: "",
},
"Description not formatted as json should return the disks name": {
disk: &computev1.Disk{
Description: "test",
Name: "testing123",
},
want: "testing123",
},
"Description formatted as json with multiple keys should return the name": {
disk: &computev1.Disk{
Description: `{"kubernetes.io/created-for/pv/name":"pvc-32613356-4cee-481d-902f-daa7223d14ab","kubernetes.io/created-for/pvc/name":"prometheus","kubernetes.io/created-for/pvc/namespace":"prometheus"}`,
},
want: "pvc-32613356-4cee-481d-902f-daa7223d14ab",
},
"Description formatted as json with one key should return the name": {
disk: &computev1.Disk{
Description: `{"kubernetes.io-created-for/pv-name":"pvc-32613356-4cee-481d-902f-daa7223d14ab"}`,
},
want: "pvc-32613356-4cee-481d-902f-daa7223d14ab",
},
"Description formatted as json with multiple wrong keys should return empty string": {
disk: &computev1.Disk{
Description: `{"kubernetes.io/created-for/pvc/name":"prometheus","kubernetes.io/created-for/pvc/namespace":"prometheus"}`,
},
want: "",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := getNameFromDisk(tt.disk); got != tt.want {
t.Errorf("getNameFromDisk() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit ad1e8a9

Please sign in to comment.