-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(pvc): Get disk name from k8s labels instead #142
Conversation
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! small comments, nothing to block this
func coalesce(desc map[string]string, keys ...string) string { | ||
for _, key := range keys { | ||
if val, ok := desc[key]; ok { | ||
return val | ||
} | ||
} | ||
return "" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[NIT] - feels like this could go in the utils
package, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not until something else is using it 😉
pkg/google/gke/gke_test.go
Outdated
"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", | ||
}, | ||
want: "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are the same test
}, | ||
want: "", | ||
}, | ||
"Description formatted as json with multiple keys should return the name": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two random extra test cases:
"Description formatted as json with multiple keys should return the name": { | |
"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: "", | |
}, | |
"Description formatted as json with multiple keys should return the name": { |
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 withgke-$cluster
. The problem with this is thatkube-state-metrics
and other metrics only look at what k8s calls them.This change updates how name is sourced.
disk.Description
into a mapdisk.Name
as a final resource