Skip to content
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

Merged
merged 2 commits into from
Apr 1, 2024

Conversation

Pokom
Copy link
Contributor

@Pokom Pokom commented Apr 1, 2024

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

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
@Pokom Pokom requested a review from logyball April 1, 2024 19:36
Copy link
Contributor

@logyball logyball left a 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

Comment on lines +288 to +295
func coalesce(desc map[string]string, keys ...string) string {
for _, key := range keys {
if val, ok := desc[key]; ok {
return val
}
}
return ""
}
Copy link
Contributor

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?

Copy link
Contributor Author

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 😉

Comment on lines 574 to 584
"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: "",
Copy link
Contributor

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": {
Copy link
Contributor

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:

Suggested change
"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": {

@Pokom Pokom merged commit ad1e8a9 into main Apr 1, 2024
1 check passed
@Pokom Pokom deleted the fix/change-exported-pvc-name branch April 1, 2024 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants