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/ephemeral pvcs #403

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading