Merge pull request #95662 from SergeyKanzhelev/nilInterfaceInShouldRecordEvent

The function shouldRecordEvent will panic when the value of input obj…
This commit is contained in:
Kubernetes Prow Robot 2020-10-27 10:36:28 -07:00 committed by GitHub
commit 1cb1005437
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -191,10 +191,11 @@ type innerEventRecorder struct {
}
func (irecorder *innerEventRecorder) shouldRecordEvent(object runtime.Object) (*v1.ObjectReference, bool) {
if object == nil {
return nil, false
}
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) {
return ref, true
}

View File

@ -676,3 +676,26 @@ func TestHashContainer(t *testing.T) {
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")
}