Skip to content

Commit afe957b

Browse files
nemcikjanjohnike15
andauthored
Fix: ephemeral pvcs (#403)
* 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 <jan.nemcik@ataccama.com>
1 parent 38d6532 commit afe957b

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

pkg/kor/create_test_resources.go

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
policyv1 "k8s.io/api/policy/v1"
1010
rbacv1 "k8s.io/api/rbac/v1"
1111
storagev1 "k8s.io/api/storage/v1"
12+
"k8s.io/apimachinery/pkg/api/resource"
1213
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1415
)
@@ -96,6 +97,25 @@ func CreateTestVolume(name, pvcName string) *corev1.Volume {
9697

9798
}
9899

100+
func CreateEphemeralVolumeDefinition(name, size string) *corev1.Volume {
101+
return &corev1.Volume{
102+
Name: name,
103+
VolumeSource: corev1.VolumeSource{
104+
Ephemeral: &corev1.EphemeralVolumeSource{
105+
VolumeClaimTemplate: &corev1.PersistentVolumeClaimTemplate{
106+
Spec: corev1.PersistentVolumeClaimSpec{
107+
Resources: corev1.VolumeResourceRequirements{
108+
Requests: corev1.ResourceList{
109+
corev1.ResourceStorage: resource.MustParse(size),
110+
},
111+
},
112+
},
113+
},
114+
},
115+
},
116+
}
117+
}
118+
99119
func CreateTestServiceAccount(namespace, name string, labels map[string]string) *corev1.ServiceAccount {
100120
return &corev1.ServiceAccount{
101121
ObjectMeta: v1.ObjectMeta{

pkg/kor/pvc.go

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func retrieveUsedPvcs(clientset kubernetes.Interface, namespace string) ([]strin
2828
if volume.PersistentVolumeClaim != nil {
2929
usedPvcs = append(usedPvcs, volume.PersistentVolumeClaim.ClaimName)
3030
}
31+
// Include ephemeral PVC
32+
if volume.Ephemeral != nil && volume.Ephemeral.VolumeClaimTemplate != nil {
33+
// https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#persistentvolumeclaim-naming
34+
usedPvcs = append(usedPvcs, pod.GetObjectMeta().GetName()+"-"+volume.Name)
35+
}
3136
}
3237
}
3338
return usedPvcs, err

pkg/kor/pvc_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ func createTestPvcs(t *testing.T) *fake.Clientset {
5757
volumeList = append(volumeList, *testVolume)
5858
testPod := CreateTestPod(testNamespace, "test-pod", "test-sa", volumeList, AppLabels)
5959

60+
ephVolume := CreateEphemeralVolumeDefinition("test-ephemeral-volume", "1Gi")
61+
testPodWEphemeralStorage := CreateTestPod(testNamespace, "test-pod-ephemeral-storage", "test-sa", []corev1.Volume{*ephVolume}, AppLabels)
62+
6063
_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), testPod, v1.CreateOptions{})
6164
if err != nil {
6265
t.Fatalf("Error creating fake %s: %v", "Pvc", err)
6366
}
67+
_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), testPodWEphemeralStorage, v1.CreateOptions{})
68+
if err != nil {
69+
t.Fatalf("Error creating fake %s: %v", "Pvc", err)
70+
}
6471

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

75-
if len(usedPvcs) != 1 {
76-
t.Errorf("Expected 1 used pvc, got %d", len(usedPvcs))
82+
if len(usedPvcs) != 2 {
83+
t.Errorf("Expected 2 used pvc, got %d", len(usedPvcs))
7784
}
7885

7986
if usedPvcs[0] != "test-pvc1" {
8087
t.Errorf("Expected 'test-pvc1', got %s", usedPvcs[0])
8188
}
89+
90+
if usedPvcs[1] != "test-pod-ephemeral-storage-test-ephemeral-volume" {
91+
t.Errorf("Expected 'test-pod-ephemeral-storage-test-ephemeral-volume', got %s", usedPvcs[1])
92+
}
8293
}
8394

8495
func TestProcessNamespacePvcs(t *testing.T) {

0 commit comments

Comments
 (0)