Repository: yonahd/kor (go)

Issue:
PVCs created as ephemeral volumes are being reported as unused
**Describe the bug**
PVCs created as ephemeral volumes are being reported as unused

**Expected behavior**
<!--- Tell us what should happen -->
These PVCs shouldn't get reported

e.g. Darwin x86_64
Kor version: 0.5.8


Pull request description:
Fix/ephemeral pvcs
<!-- Please read [CONTRIBUTING.md](CONTRIBUTING.md) for additional information on contributing to this repository! -->

<!--
  !!!! README !!!! Please fill this out.

  Please follow conventional commit naming conventions:

  https://www.conventionalcommits.org/en/v1.0.0/#summary
-->

<!-- A short description of what your PR does and what it solves. -->

## What this PR does / why we need it?
Fixes reporting PVC created as ephemeral to be reported

## PR Checklist

- [x] This PR adds K8s exceptions (false positives)
- [x] This PR adds new code
- [x] This PR includes tests for new/existing code
- [ ] This PR adds docs
<!-- - [ ] This PR does something else -->

## GitHub Issue

Closes #402 

<!-- Notes that may be helpful for anyone reviewing this PR -->

## Notes for your reviewers


Patch:
diff --git a/pkg/kor/pvc.go b/pkg/kor/pvc.go
index 94e3bc9..7278776 100644
--- a/pkg/kor/pvc.go
+++ b/pkg/kor/pvc.go
@@ -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


Test patch:
diff --git a/pkg/kor/create_test_resources.go b/pkg/kor/create_test_resources.go
index 8e2c2ce..4e4c493 100644
--- a/pkg/kor/create_test_resources.go
+++ b/pkg/kor/create_test_resources.go
@@ -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"
 )
@@ -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{
diff --git a/pkg/kor/pvc_test.go b/pkg/kor/pvc_test.go
index 2abdcf7..1242397 100644
--- a/pkg/kor/pvc_test.go
+++ b/pkg/kor/pvc_test.go
@@ -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
 }
@@ -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) {