mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 18:24:07 +00:00
Check --show-events in kubectl describe pvc (#120380)
* Check --show-events arg before fetching events * Remove unnecessary else statement * Add test for false show events
This commit is contained in:
parent
0241da314e
commit
a919079ff3
@ -1650,7 +1650,10 @@ func (d *PersistentVolumeClaimDescriber) Describe(namespace, name string, descri
|
|||||||
return "", err
|
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)
|
return describePersistentVolumeClaim(pvc, events, pods)
|
||||||
}
|
}
|
||||||
|
@ -1765,9 +1765,11 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
deletionTimestamp := metav1.Time{Time: time.Now().UTC().AddDate(-10, 0, 0)}
|
deletionTimestamp := metav1.Time{Time: time.Now().UTC().AddDate(-10, 0, 0)}
|
||||||
snapshotAPIGroup := "snapshot.storage.k8s.io"
|
snapshotAPIGroup := "snapshot.storage.k8s.io"
|
||||||
|
defaultDescriberSettings := &DescriberSettings{ShowEvents: true}
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
pvc *corev1.PersistentVolumeClaim
|
pvc *corev1.PersistentVolumeClaim
|
||||||
|
describerSettings *DescriberSettings
|
||||||
expectedElements []string
|
expectedElements []string
|
||||||
unexpectedElements []string
|
unexpectedElements []string
|
||||||
}{
|
}{
|
||||||
@ -1783,6 +1785,7 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
|
|||||||
Phase: corev1.ClaimBound,
|
Phase: corev1.ClaimBound,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
expectedElements: []string{"Events"},
|
||||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -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"},
|
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 {
|
for _, test := range testCases {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
fake := fake.NewSimpleClientset(test.pvc)
|
fake := fake.NewSimpleClientset(test.pvc)
|
||||||
c := PersistentVolumeClaimDescriber{fake}
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error for test %s: %v", test.name, err)
|
t.Errorf("Unexpected error for test %s: %v", test.name, err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user