Skip to content

Commit

Permalink
Fix panic on nil map entry assignment for custom event metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <[email protected]>
  • Loading branch information
matheuscscp committed Feb 11, 2025
1 parent 7c90c14 commit 51166e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
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

0 comments on commit 51166e7

Please sign in to comment.