mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #95662 from SergeyKanzhelev/nilInterfaceInShouldRecordEvent
The function shouldRecordEvent will panic when the value of input obj…
This commit is contained in:
commit
1cb1005437
@ -191,10 +191,11 @@ type innerEventRecorder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (irecorder *innerEventRecorder) shouldRecordEvent(object runtime.Object) (*v1.ObjectReference, bool) {
|
func (irecorder *innerEventRecorder) shouldRecordEvent(object runtime.Object) (*v1.ObjectReference, bool) {
|
||||||
if object == nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
if ref, ok := object.(*v1.ObjectReference); ok {
|
if ref, ok := object.(*v1.ObjectReference); ok {
|
||||||
|
// this check is needed AFTER the cast. See https://github.com/kubernetes/kubernetes/issues/95552
|
||||||
|
if ref == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
if !strings.HasPrefix(ref.FieldPath, ImplicitContainerPrefix) {
|
if !strings.HasPrefix(ref.FieldPath, ImplicitContainerPrefix) {
|
||||||
return ref, true
|
return ref, true
|
||||||
}
|
}
|
||||||
|
@ -676,3 +676,26 @@ func TestHashContainer(t *testing.T) {
|
|||||||
assert.Equal(t, tc.expectedHash, hashVal, "the hash value here should not be changed.")
|
assert.Equal(t, tc.expectedHash, hashVal, "the hash value here should not be changed.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShouldRecordEvent(t *testing.T) {
|
||||||
|
var innerEventRecorder = &innerEventRecorder{
|
||||||
|
recorder: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, actual := innerEventRecorder.shouldRecordEvent(nil)
|
||||||
|
assert.Equal(t, false, actual)
|
||||||
|
|
||||||
|
var obj = &v1.ObjectReference{Namespace: "claimrefns", Name: "claimrefname"}
|
||||||
|
|
||||||
|
_, actual = innerEventRecorder.shouldRecordEvent(obj)
|
||||||
|
assert.Equal(t, true, actual)
|
||||||
|
|
||||||
|
obj = &v1.ObjectReference{Namespace: "system", Name: "infra", FieldPath: "implicitly required container "}
|
||||||
|
|
||||||
|
_, actual = innerEventRecorder.shouldRecordEvent(obj)
|
||||||
|
assert.Equal(t, false, actual)
|
||||||
|
|
||||||
|
var nilObj *v1.ObjectReference = nil
|
||||||
|
_, actual = innerEventRecorder.shouldRecordEvent(nilObj)
|
||||||
|
assert.Equal(t, false, actual, "should not panic if the typed nil was used, see https://github.com/kubernetes/kubernetes/issues/95552")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user