Skip to content

Commit

Permalink
fix PVC detection on microk8s
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed Apr 15, 2024
1 parent 7500d29 commit 0175d1c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 3 additions & 8 deletions common/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import (
)

var (
k8sVolumeDir = regexp.MustCompile(`.+/(volumes/kubernetes.io~([^/]+)|volume-subpaths|k3s/storage)/(pvc-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})`)
k8sVolumeDir = regexp.MustCompile(`.+(pvc-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}).*`)
)

func ParseKubernetesVolumeSource(source string) string {
groups := k8sVolumeDir.FindStringSubmatch(source)
if len(groups) != 4 {
if len(groups) != 2 {
return ""
}
provisioner, volume := groups[2], groups[3]
switch provisioner {
case "secret", "configmap", "empty-dir", "projected":
return ""
}
return volume
return groups[1]
}
4 changes: 4 additions & 0 deletions common/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestParseKubernetesVolumeSource(t *testing.T) {
"pvc-d0c0cc92-ef36-4b4f-90c0-5c5ed489df0b",
ParseKubernetesVolumeSource("/var/lib/rancher/k3s/storage/pvc-d0c0cc92-ef36-4b4f-90c0-5c5ed489df0b_default_mongod-data-mongo-psmdb-db-rs0-0"))

assert.Equal(t,
"pvc-4bf620ab-bb10-4cd6-803a-5be8735ccaf6",
ParseKubernetesVolumeSource("/var/snap/microk8s/common/default-storage/coroot-coroot-data-pvc-4bf620ab-bb10-4cd6-803a-5be8735ccaf6"))

assert.Equal(t,
"",
ParseKubernetesVolumeSource("/var/lib/kubelet/pods/adf669ca-c3f8-49de-9ad4-9dd66721dc0d/volumes/kubernetes.io~projected/kube-api-access-jvvq6"),
Expand Down

0 comments on commit 0175d1c

Please sign in to comment.