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

Fix panic on nil map entry assignment for custom event metadata #868

Merged
merged 1 commit into from
Feb 11, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions runtime/events/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func (r *Recorder) AnnotatedEventf(
if annotatedObject, ok := object.(interface{ GetAnnotations() map[string]string }); ok {
for k, v := range annotatedObject.GetAnnotations() {
if strings.HasPrefix(k, eventv1.Group+"/") {
if annotations == nil {
annotations = make(map[string]string)
}
annotations[k] = v
}
}
Expand Down
33 changes: 24 additions & 9 deletions runtime/events/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestEventRecorder_AnnotatedEventf(t *testing.T) {
for _, tt := range []struct {
name string
object runtime.Object
inputAnnotations map[string]string
expectedMetadata map[string]string
}{
{
Expand All @@ -50,22 +51,40 @@ func TestEventRecorder_AnnotatedEventf(t *testing.T) {
},
},
},
inputAnnotations: map[string]string{"test": "true"},
expectedMetadata: map[string]string{
"test": "true",
"event.toolkit.fluxcd.io/deploymentID": "e076e315-5a48-41c3-81c8-8d8bdee7d74d",
"event.toolkit.fluxcd.io/image": "ghcr.io/stefanprodan/podinfo:6.5.0",
},
},
{
name: "event with ConfigMap does not panic with nil inputAnnotations",
object: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "webapp",
Namespace: "gitops-system",
Annotations: map[string]string{
"event.toolkit.fluxcd.io/deploymentID": "e076e315-5a48-41c3-81c8-8d8bdee7d74d",
"event.toolkit.fluxcd.io/image": "ghcr.io/stefanprodan/podinfo:6.5.0",
},
},
},
inputAnnotations: nil,
expectedMetadata: map[string]string{
"event.toolkit.fluxcd.io/deploymentID": "e076e315-5a48-41c3-81c8-8d8bdee7d74d",
"event.toolkit.fluxcd.io/image": "ghcr.io/stefanprodan/podinfo:6.5.0",
},
},
{
name: "event with ObjectReference for ConfigMap (does not panic with runtime.Object without annotations)",
object: &corev1.ObjectReference{
Name: "webapp",
Namespace: "gitops-system",
Kind: "ConfigMap",
},
expectedMetadata: map[string]string{
"test": "true",
},
inputAnnotations: map[string]string{"test": "true"},
expectedMetadata: map[string]string{"test": "true"},
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -96,17 +115,13 @@ func TestEventRecorder_AnnotatedEventf(t *testing.T) {

obj := tt.object

meta := map[string]string{
"test": "true",
}

const msg = "sync object"

eventRecorder.AnnotatedEventf(obj, meta, corev1.EventTypeNormal, "sync", "%s", msg)
eventRecorder.AnnotatedEventf(obj, tt.inputAnnotations, corev1.EventTypeNormal, "sync", "%s", msg)
require.Equal(t, 2, requestCount)

// When a trace event is sent, it's dropped, no new request.
eventRecorder.AnnotatedEventf(obj, meta, eventv1.EventTypeTrace, "sync", "%s", msg)
eventRecorder.AnnotatedEventf(obj, tt.inputAnnotations, eventv1.EventTypeTrace, "sync", "%s", msg)
require.Equal(t, 2, requestCount)
})
}
Expand Down
Loading