Skip to content

Commit

Permalink
Check --show-events in kubectl describe pvc (kubernetes#120380)
Browse files Browse the repository at this point in the history
* Check --show-events arg before fetching events

* Remove unnecessary else statement

* Add test for false show events
  • Loading branch information
MaGaroo authored and Sharpz7 committed Oct 27, 2023
1 parent 34f8cc0 commit 7161650
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 4 additions & 1 deletion staging/src/k8s.io/kubectl/pkg/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,10 @@ func (d *PersistentVolumeClaimDescriber) Describe(namespace, name string, descri
return "", err
}

events, _ := searchEvents(d.CoreV1(), pvc, describerSettings.ChunkSize)
var events *corev1.EventList
if describerSettings.ShowEvents {
events, _ = searchEvents(d.CoreV1(), pvc, describerSettings.ChunkSize)
}

return describePersistentVolumeClaim(pvc, events, pods)
}
Expand Down
28 changes: 27 additions & 1 deletion staging/src/k8s.io/kubectl/pkg/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1765,9 +1765,11 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
now := time.Now()
deletionTimestamp := metav1.Time{Time: time.Now().UTC().AddDate(-10, 0, 0)}
snapshotAPIGroup := "snapshot.storage.k8s.io"
defaultDescriberSettings := &DescriberSettings{ShowEvents: true}
testCases := []struct {
name string
pvc *corev1.PersistentVolumeClaim
describerSettings *DescriberSettings
expectedElements []string
unexpectedElements []string
}{
Expand All @@ -1783,6 +1785,7 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
Phase: corev1.ClaimBound,
},
},
expectedElements: []string{"Events"},
unexpectedElements: []string{"VolumeMode", "Filesystem"},
},
{
Expand Down Expand Up @@ -1967,13 +1970,36 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
},
expectedElements: []string{"DataSource:\n APIGroup: snapshot.storage.k8s.io\n Kind: VolumeSnapshot\n Name: src-snapshot\n"},
},
{
name: "no-show-events",
pvc: &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "volume1",
StorageClassName: &goldClassName,
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimBound,
},
},
unexpectedElements: []string{"Events"},
describerSettings: &DescriberSettings{ShowEvents: false},
},
}

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
fake := fake.NewSimpleClientset(test.pvc)
c := PersistentVolumeClaimDescriber{fake}
str, err := c.Describe("foo", "bar", DescriberSettings{ShowEvents: true})

var describerSettings DescriberSettings
if test.describerSettings != nil {
describerSettings = *test.describerSettings
} else {
describerSettings = *defaultDescriberSettings
}

str, err := c.Describe("foo", "bar", describerSettings)
if err != nil {
t.Errorf("Unexpected error for test %s: %v", test.name, err)
}
Expand Down

0 comments on commit 7161650

Please sign in to comment.