Skip to content

Commit 2e5cff1

Browse files
committed
🐛 Add GetAnnotatedEventRecorder
Add the missing AnnotatedEventf as a new interface AnnotatedEventRecorder under cluster, manager, and recorder provider
1 parent c8b4b9d commit 2e5cff1

7 files changed

Lines changed: 74 additions & 6 deletions

File tree

pkg/cluster/cluster_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ var _ = Describe("cluster.Cluster", func() {
156156
Expect(c.GetFieldIndexer()).To(Equal(cluster.cache))
157157
})
158158

159+
It("should provide a function to get the AnnotatedEventRecorder", func() {
160+
c, err := New(cfg)
161+
Expect(err).NotTo(HaveOccurred())
162+
Expect(c.GetAnnotatedEventRecorder("test")).NotTo(BeNil())
163+
})
164+
159165
It("should provide a function to get the EventRecorder", func() {
160166
c, err := New(cfg)
161167
Expect(err).NotTo(HaveOccurred())

pkg/cluster/internal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func (c *cluster) GetEventRecorder(name string) events.EventRecorder {
9292
return c.recorderProvider.GetEventRecorder(name)
9393
}
9494

95+
func (c *cluster) GetAnnotatedEventRecorder(name string) events.AnnotatedEventRecorder {
96+
return c.recorderProvider.GetAnnotatedEventRecorder(name)
97+
}
98+
9599
func (c *cluster) GetRESTMapper() meta.RESTMapper {
96100
return c.mapper
97101
}

pkg/internal/recorder/recorder.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,33 @@ func (p *Provider) GetEventRecorder(name string) events.EventRecorder {
169169
}
170170
}
171171

172+
// GetEventRecorder returns an event recorder that broadcasts to this provider's
173+
// broadcaster. All events will be associated with a component of the given name.
174+
func (p *Provider) GetAnnotatedEventRecorder(name string) events.AnnotatedEventRecorder {
175+
return &lazyRecorder{
176+
prov: p,
177+
name: name,
178+
}
179+
}
180+
172181
// lazyRecorder is a recorder that doesn't actually instantiate any underlying
173182
// recorder until the first event is emitted.
174183
type lazyRecorder struct {
175184
prov *Provider
176185
name string
177186

178-
recOnce sync.Once
179-
rec events.EventRecorder
187+
recOnce sync.Once
188+
eventRecorder events.EventRecorder
189+
annotatedEventRecorder events.AnnotatedEventRecorder
180190
}
181191

182192
// ensureRecording ensures that a concrete recorder is populated for this recorder.
183193
func (l *lazyRecorder) ensureRecording() {
184194
l.recOnce.Do(func() {
185195
_, broadcaster := l.prov.getBroadcaster()
186-
l.rec = broadcaster.NewRecorder(l.prov.scheme, l.name)
196+
rec := broadcaster.NewRecorder(l.prov.scheme, l.name)
197+
l.eventRecorder = rec
198+
l.annotatedEventRecorder = rec
187199
})
188200
}
189201

@@ -192,7 +204,17 @@ func (l *lazyRecorder) Eventf(regarding runtime.Object, related runtime.Object,
192204

193205
l.prov.lock.RLock()
194206
if !l.prov.stopped {
195-
l.rec.Eventf(regarding, related, eventtype, reason, action, note, args...)
207+
l.eventRecorder.Eventf(regarding, related, eventtype, reason, action, note, args...)
208+
}
209+
l.prov.lock.RUnlock()
210+
}
211+
212+
func (l *lazyRecorder) AnnotatedEventf(regarding runtime.Object, related runtime.Object, annotations map[string]string, eventtype, reason, action, note string, args ...any) {
213+
l.ensureRecording()
214+
215+
l.prov.lock.RLock()
216+
if !l.prov.stopped {
217+
l.annotatedEventRecorder.AnnotatedEventf(regarding, related, annotations, eventtype, reason, action, note, args...)
196218
}
197219
l.prov.lock.RUnlock()
198220
}

pkg/internal/recorder/recorder_integration_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ var _ = Describe("recorder", func() {
4545
Expect(err).NotTo(HaveOccurred())
4646

4747
By("Creating the Controller")
48-
deprecatedRecorder := cm.GetEventRecorder("test-deprecated-recorder")
48+
deprecatedRecorder := cm.GetEventRecorderFor("test-deprecated-recorder")
4949
recorder := cm.GetEventRecorder("test-recorder")
50+
annotatedRecorder := cm.GetAnnotatedEventRecorder("test-annotated-recorder")
5051
instance, err := controller.New("foo-controller", cm, controller.Options{
5152
Reconciler: reconcile.Func(
5253
func(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
5354
dp, err := clientset.AppsV1().Deployments(request.Namespace).Get(ctx, request.Name, metav1.GetOptions{})
5455
Expect(err).NotTo(HaveOccurred())
55-
deprecatedRecorder.Eventf(dp, nil, corev1.EventTypeNormal, "deprecated-test-reason", "deprecatedAction", "deprecated-test-msg")
56+
deprecatedRecorder.Eventf(dp, corev1.EventTypeNormal, "deprecated-test-reason", "deprecated-test-msg")
5657
recorder.Eventf(dp, nil, corev1.EventTypeNormal, "test-reason", "test-action", "test-note")
58+
annotatedRecorder.AnnotatedEventf(dp, nil, map[string]string{"key": "value"}, corev1.EventTypeNormal, "test-annotated-reason", "test-annotated-action", "test-annotated-note")
5759
return reconcile.Result{}, nil
5860
}),
5961
})
@@ -129,6 +131,24 @@ var _ = Describe("recorder", func() {
129131
Expect(evt.Reason).To(Equal("test-reason"))
130132
Expect(evt.Action).To(Equal("test-action"))
131133
Expect(evt.Note).To(Equal("test-note"))
134+
135+
By("Validate annotated event is published as expected")
136+
annotatedEvtWatcher, err := clientset.EventsV1().Events("default").Watch(ctx,
137+
metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("reason", "test-annotated-reason").String()})
138+
Expect(err).NotTo(HaveOccurred())
139+
140+
resultEvent = <-annotatedEvtWatcher.ResultChan()
141+
142+
Expect(resultEvent.Type).To(Equal(watch.Added))
143+
annotatedEvt, isEvent := resultEvent.Object.(*eventsv1.Event)
144+
Expect(isEvent).To(BeTrue())
145+
146+
Expect(annotatedEvt.Regarding).To(Equal(*dpRef))
147+
Expect(annotatedEvt.Type).To(Equal(corev1.EventTypeNormal))
148+
Expect(annotatedEvt.Reason).To(Equal("test-annotated-reason"))
149+
Expect(annotatedEvt.Action).To(Equal("test-annotated-action"))
150+
Expect(annotatedEvt.Note).To(Equal("test-annotated-note"))
151+
Expect(annotatedEvt.Annotations).To(HaveKeyWithValue("key", "value"))
132152
})
133153
})
134154
})

pkg/internal/recorder/recorder_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ var _ = Describe("recorder.Provider", func() {
6262
Expect(recorder).NotTo(BeNil())
6363
})
6464
})
65+
Describe("GetAnnotatedEventRecorder", func() {
66+
It("should return a recorder instance.", func() {
67+
provider, err := recorder.NewProvider(cfg, httpClient, scheme.Scheme, logr.Discard(), makeBroadcaster())
68+
Expect(err).NotTo(HaveOccurred())
69+
70+
recorder := provider.GetAnnotatedEventRecorder("test")
71+
Expect(recorder).NotTo(BeNil())
72+
})
73+
})
6574
})
6675

6776
func makeBroadcaster() func() (record.EventBroadcaster, events.EventBroadcaster, bool) {

pkg/manager/internal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ func (cm *controllerManager) GetEventRecorder(name string) events.EventRecorder
268268
return cm.cluster.GetEventRecorder(name)
269269
}
270270

271+
func (cm *controllerManager) GetAnnotatedEventRecorder(name string) events.AnnotatedEventRecorder {
272+
return cm.cluster.GetAnnotatedEventRecorder(name)
273+
}
274+
271275
func (cm *controllerManager) GetRESTMapper() meta.RESTMapper {
272276
return cm.cluster.GetRESTMapper()
273277
}

pkg/recorder/recorder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ type Provider interface {
3333
GetEventRecorderFor(name string) record.EventRecorder
3434
// GetEventRecorder returns a EventRecorder with given name.
3535
GetEventRecorder(name string) events.EventRecorder
36+
// GetAnnotatedEventRecorder returns an AnnotatedEventRecorder that supports
37+
// attaching annotations to events.
38+
GetAnnotatedEventRecorder(name string) events.AnnotatedEventRecorder
3639
}

0 commit comments

Comments
 (0)