Skip to content

Commit

Permalink
Fixed case where unclaimed persistent volume causes panic
Browse files Browse the repository at this point in the history
Signed-off-by: GuessWhoSamFoo <[email protected]>
  • Loading branch information
GuessWhoSamFoo committed Apr 29, 2020
1 parent 7ea0fbe commit 4f37316
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/889-GuessWhoSamFoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed case where unclaimed persistent volumes causes panic
7 changes: 7 additions & 0 deletions internal/printer/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ func getBoundPersistentVolumeClaim(ctx context.Context, pv *corev1.PersistentVol
pvc := &corev1.PersistentVolumeClaim{}

cr := pv.Spec.ClaimRef
if cr == nil {
return nil, nil
}

key := store.Key{
APIVersion: cr.APIVersion,
Kind: cr.Kind,
Expand Down Expand Up @@ -244,6 +248,9 @@ func createBoundPersistentVolumeClaimLink(ctx context.Context, pv *corev1.Persis
}

cr := pv.Spec.ClaimRef
if cr == nil {
return component.NewLink("", "", ""), nil
}
claimText := fmt.Sprintf("%s/%s", cr.Namespace, cr.Name)
claimLink, err := options.Link.ForObject(pvc, claimText)
if err != nil {
Expand Down
28 changes: 26 additions & 2 deletions internal/printer/persistentvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func Test_PersistentVolumeListHandler(t *testing.T) {
Items: []corev1.PersistentVolume{*object},
}

unbound := testutil.CreatePersistentVolume("unboundPersistentVolume")
unbound.CreationTimestamp = metav1.Time{Time: now}
unboundList := &corev1.PersistentVolumeList{
Items: []corev1.PersistentVolume{*unbound},
}

cases := []struct {
name string
list *corev1.PersistentVolumeList
Expand All @@ -61,6 +67,24 @@ func Test_PersistentVolumeListHandler(t *testing.T) {
},
}),
},
{
name: "unclaimed",
list: unboundList,
expected: component.NewTableWithRows("Persistent Volumes", "We couldn't find any persistent volumes!", cols,
[]component.TableRow{
{
"Name": component.NewLink("", "unboundPersistentVolume", "/unboundPersistentVolume"),
"Capacity": component.NewText("0"),
"Access Modes": component.NewText(""),
"Reclaim Policy": component.NewText(""),
"Status": component.NewText("Bound"),
"Claim": component.NewLink("", "", ""),
"Storage Class": component.NewText(""),
"Reason": component.NewText(""),
"Age": component.NewTimestamp(now),
},
}),
},
{
name: "list is nil",
list: nil,
Expand All @@ -79,13 +103,13 @@ func Test_PersistentVolumeListHandler(t *testing.T) {
ctx := context.Background()

if tc.list != nil {
tpo.PathForObject(&tc.list.Items[0], tc.list.Items[0].Name, "/persistentVolume")
tpo.PathForObject(&tc.list.Items[0], tc.list.Items[0].Name, "/"+tc.list.Items[0].Name)

pvcKey, err := store.KeyFromObject(pvcObject)
require.NoError(t, err)

tpo.objectStore.EXPECT().Get(ctx, pvcKey).
Return(testutil.ToUnstructured(t, pvcObject), nil)
Return(testutil.ToUnstructured(t, pvcObject), nil).AnyTimes()

tpo.PathForObject(pvcObject, pvcObject.Namespace+"/"+pvcObject.Name, "/pvc")
}
Expand Down

0 comments on commit 4f37316

Please sign in to comment.