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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ""
}
Comment on lines +288 to +295
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 😉


// 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
}
39 changes: 39 additions & 0 deletions pkg/google/gke/gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,42 @@ 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",
},
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

},
"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": {

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",
},
}
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)
}
})
}
}