Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The function shouldRecordEvent will panic when the value of input obj… #95662

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/kubelet/container/helpers.go
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I THINK it should NOT record the event if the object is nil.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is similar to the behavior that seems to be expected before. But worth paying attention to when reviewing this change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}
if !strings.HasPrefix(ref.FieldPath, ImplicitContainerPrefix) {
return ref, true
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/kubelet/container/helpers_test.go
Expand Up @@ -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")
}