Skip to content

Commit

Permalink
Fix: ephemeral pvcs (#403)
Browse files Browse the repository at this point in the history
* added missing nindent arg in imagePullSecrets

* added missing nindent arg in imagePullSecrets

* bumped chart version

* fixed reporting ephemeral pvcs

* added tests

---------

Co-authored-by: Jan Nemcik <[email protected]>
  • Loading branch information
nemcikjan and johnike15 authored Jan 29, 2025
1 parent 38d6532 commit afe957b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
20 changes: 20 additions & 0 deletions pkg/kor/create_test_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
Expand Down Expand Up @@ -96,6 +97,25 @@ func CreateTestVolume(name, pvcName string) *corev1.Volume {

}

func CreateEphemeralVolumeDefinition(name, size string) *corev1.Volume {
return &corev1.Volume{
Name: name,
VolumeSource: corev1.VolumeSource{
Ephemeral: &corev1.EphemeralVolumeSource{
VolumeClaimTemplate: &corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse(size),
},
},
},
},
},
},
}
}

func CreateTestServiceAccount(namespace, name string, labels map[string]string) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: v1.ObjectMeta{
Expand Down
5 changes: 5 additions & 0 deletions pkg/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func retrieveUsedPvcs(clientset kubernetes.Interface, namespace string) ([]strin
if volume.PersistentVolumeClaim != nil {
usedPvcs = append(usedPvcs, volume.PersistentVolumeClaim.ClaimName)
}
// Include ephemeral PVC
if volume.Ephemeral != nil && volume.Ephemeral.VolumeClaimTemplate != nil {
// https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#persistentvolumeclaim-naming
usedPvcs = append(usedPvcs, pod.GetObjectMeta().GetName()+"-"+volume.Name)
}
}
}
return usedPvcs, err
Expand Down
15 changes: 13 additions & 2 deletions pkg/kor/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ func createTestPvcs(t *testing.T) *fake.Clientset {
volumeList = append(volumeList, *testVolume)
testPod := CreateTestPod(testNamespace, "test-pod", "test-sa", volumeList, AppLabels)

ephVolume := CreateEphemeralVolumeDefinition("test-ephemeral-volume", "1Gi")
testPodWEphemeralStorage := CreateTestPod(testNamespace, "test-pod-ephemeral-storage", "test-sa", []corev1.Volume{*ephVolume}, AppLabels)

_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), testPod, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake %s: %v", "Pvc", err)
}
_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), testPodWEphemeralStorage, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake %s: %v", "Pvc", err)
}

return clientset
}
Expand All @@ -72,13 +79,17 @@ func TestRetrieveUsedPvcs(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}

if len(usedPvcs) != 1 {
t.Errorf("Expected 1 used pvc, got %d", len(usedPvcs))
if len(usedPvcs) != 2 {
t.Errorf("Expected 2 used pvc, got %d", len(usedPvcs))
}

if usedPvcs[0] != "test-pvc1" {
t.Errorf("Expected 'test-pvc1', got %s", usedPvcs[0])
}

if usedPvcs[1] != "test-pod-ephemeral-storage-test-ephemeral-volume" {
t.Errorf("Expected 'test-pod-ephemeral-storage-test-ephemeral-volume', got %s", usedPvcs[1])
}
}

func TestProcessNamespacePvcs(t *testing.T) {
Expand Down

0 comments on commit afe957b

Please sign in to comment.