Datasets:
Download instances/cloudnative-controller-runtime-3509/gold/controller-runtime.patch from CrossSWE/CrossSWE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/instances/cloudnative-controller-runtime-3509/gold/controller-runtime.patch
- Command line
-
hf download hf://datasets/CrossSWE/CrossSWE-Bench/instances/cloudnative-controller-runtime-3509/gold/controller-runtime.patch
-
curl -L -o controller-runtime.patch https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/instances/cloudnative-controller-runtime-3509/gold/controller-runtime.patch
10.5 kB
| diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go | |
| index e076faf3..f8b8e8ca 100644 | |
| --- a/pkg/cluster/cluster_test.go | |
| +++ b/pkg/cluster/cluster_test.go | |
| import ( | |
| utilnet "k8s.io/apimachinery/pkg/util/net" | |
| "k8s.io/client-go/rest" | |
| + "k8s.io/client-go/tools/events" | |
| "sigs.k8s.io/controller-runtime/pkg/cache" | |
| "sigs.k8s.io/controller-runtime/pkg/client" | |
| intrec "sigs.k8s.io/controller-runtime/pkg/internal/recorder" | |
| var _ = Describe("cluster.Cluster", func() { | |
| It("should provide a function to get the EventRecorder", func() { | |
| c, err := New(cfg) | |
| Expect(err).NotTo(HaveOccurred()) | |
| - Expect(c.GetEventRecorder("test")).NotTo(BeNil()) | |
| + recorder := c.GetEventRecorder("test") | |
| + Expect(recorder).NotTo(BeNil()) | |
| + // The returned recorder should support both Eventf and AnnotatedEventf | |
| + var _ events.EventRecorder = recorder | |
| + var _ events.AnnotatedEventRecorder = recorder | |
| }) | |
| It("should provide a function to get the deprecated EventRecorder", func() { | |
| diff --git a/pkg/cluster/internal.go b/pkg/cluster/internal.go | |
| index 755f83b5..e01f0726 100644 | |
| --- a/pkg/cluster/internal.go | |
| +++ b/pkg/cluster/internal.go | |
| import ( | |
| "k8s.io/apimachinery/pkg/api/meta" | |
| "k8s.io/apimachinery/pkg/runtime" | |
| "k8s.io/client-go/rest" | |
| - "k8s.io/client-go/tools/events" | |
| "k8s.io/client-go/tools/record" | |
| "sigs.k8s.io/controller-runtime/pkg/cache" | |
| "sigs.k8s.io/controller-runtime/pkg/client" | |
| intrec "sigs.k8s.io/controller-runtime/pkg/internal/recorder" | |
| + "sigs.k8s.io/controller-runtime/pkg/recorder" | |
| ) | |
| type cluster struct { | |
| func (c *cluster) GetEventRecorderFor(name string) record.EventRecorder { | |
| return c.recorderProvider.GetEventRecorderFor(name) | |
| } | |
| -func (c *cluster) GetEventRecorder(name string) events.EventRecorder { | |
| +func (c *cluster) GetEventRecorder(name string) recorder.EventRecorder { | |
| return c.recorderProvider.GetEventRecorder(name) | |
| } | |
| diff --git a/pkg/internal/recorder/recorder.go b/pkg/internal/recorder/recorder.go | |
| index 097d2dee..1f2738e5 100644 | |
| --- a/pkg/internal/recorder/recorder.go | |
| +++ b/pkg/internal/recorder/recorder.go | |
| import ( | |
| "k8s.io/client-go/tools/events" | |
| "k8s.io/client-go/tools/record" | |
| "sigs.k8s.io/controller-runtime/pkg/log" | |
| + "sigs.k8s.io/controller-runtime/pkg/recorder" | |
| ) | |
| // EventBroadcasterProducer makes an event broadcaster, returning | |
| func (p *Provider) GetEventRecorderFor(name string) record.EventRecorder { | |
| // broadcaster. All events will be associated with the given reportingController | |
| // name. client-go appends "-" and the hostname to derive reportingInstance, | |
| // which must fit within the events.k8s.io/v1 128-character limit. | |
| -func (p *Provider) GetEventRecorder(name string) events.EventRecorder { | |
| +// The returned recorder supports both Eventf and AnnotatedEventf. | |
| +func (p *Provider) GetEventRecorder(name string) recorder.EventRecorder { | |
| return &lazyRecorder{ | |
| prov: p, | |
| name: name, | |
| func (p *Provider) GetEventRecorder(name string) events.EventRecorder { | |
| // lazyRecorder is a recorder that doesn't actually instantiate any underlying | |
| // recorder until the first event is emitted. | |
| +var _ recorder.EventRecorder = (*lazyRecorder)(nil) | |
| + | |
| type lazyRecorder struct { | |
| prov *Provider | |
| name string | |
| - recOnce sync.Once | |
| - rec events.EventRecorder | |
| + recOnce sync.Once | |
| + eventRecorder events.EventRecorder | |
| + annotatedEventRecorder events.AnnotatedEventRecorder | |
| } | |
| // ensureRecording ensures that a concrete recorder is populated for this recorder. | |
| func (l *lazyRecorder) ensureRecording() { | |
| l.recOnce.Do(func() { | |
| _, broadcaster := l.prov.getBroadcaster() | |
| - l.rec = broadcaster.NewRecorder(l.prov.scheme, l.name) | |
| + rec := broadcaster.NewRecorder(l.prov.scheme, l.name) | |
| + l.eventRecorder = rec | |
| + l.annotatedEventRecorder = rec.(events.AnnotatedEventRecorder) | |
| }) | |
| } | |
| func (l *lazyRecorder) Eventf(regarding runtime.Object, related runtime.Object, | |
| l.prov.lock.RLock() | |
| if !l.prov.stopped { | |
| - l.rec.Eventf(regarding, related, eventtype, reason, action, note, args...) | |
| + l.eventRecorder.Eventf(regarding, related, eventtype, reason, action, note, args...) | |
| + } | |
| + l.prov.lock.RUnlock() | |
| +} | |
| + | |
| +func (l *lazyRecorder) AnnotatedEventf(regarding runtime.Object, related runtime.Object, annotations map[string]string, eventtype, reason, action, note string, args ...any) { | |
| + l.ensureRecording() | |
| + | |
| + l.prov.lock.RLock() | |
| + if !l.prov.stopped { | |
| + l.annotatedEventRecorder.AnnotatedEventf(regarding, related, annotations, eventtype, reason, action, note, args...) | |
| } | |
| l.prov.lock.RUnlock() | |
| } | |
| diff --git a/pkg/internal/recorder/recorder_integration_test.go b/pkg/internal/recorder/recorder_integration_test.go | |
| index 72c8335c..1f3950af 100644 | |
| --- a/pkg/internal/recorder/recorder_integration_test.go | |
| +++ b/pkg/internal/recorder/recorder_integration_test.go | |
| var _ = Describe("recorder", func() { | |
| Expect(err).NotTo(HaveOccurred()) | |
| By("Creating the Controller") | |
| - deprecatedRecorder := cm.GetEventRecorder("test-deprecated-recorder") | |
| + deprecatedRecorder := cm.GetEventRecorderFor("test-deprecated-recorder") //nolint:staticcheck // testing deprecated API | |
| recorder := cm.GetEventRecorder("test-recorder") | |
| instance, err := controller.New("foo-controller", cm, controller.Options{ | |
| Reconciler: reconcile.Func( | |
| func(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | |
| dp, err := clientset.AppsV1().Deployments(request.Namespace).Get(ctx, request.Name, metav1.GetOptions{}) | |
| Expect(err).NotTo(HaveOccurred()) | |
| - deprecatedRecorder.Eventf(dp, nil, corev1.EventTypeNormal, "deprecated-test-reason", "deprecatedAction", "deprecated-test-msg") | |
| + deprecatedRecorder.Eventf(dp, corev1.EventTypeNormal, "deprecated-test-reason", "deprecated-test-msg") | |
| recorder.Eventf(dp, nil, corev1.EventTypeNormal, "test-reason", "test-action", "test-note") | |
| + recorder.AnnotatedEventf(dp, nil, map[string]string{"key": "value"}, corev1.EventTypeNormal, "test-annotated-reason", "test-annotated-action", "test-annotated-note") | |
| return reconcile.Result{}, nil | |
| }), | |
| }) | |
| var _ = Describe("recorder", func() { | |
| Expect(evt.Reason).To(Equal("test-reason")) | |
| Expect(evt.Action).To(Equal("test-action")) | |
| Expect(evt.Note).To(Equal("test-note")) | |
| + | |
| + By("Validate annotated event is published as expected") | |
| + annotatedEvtWatcher, err := clientset.EventsV1().Events("default").Watch(ctx, | |
| + metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("reason", "test-annotated-reason").String()}) | |
| + Expect(err).NotTo(HaveOccurred()) | |
| + | |
| + resultEvent = <-annotatedEvtWatcher.ResultChan() | |
| + | |
| + Expect(resultEvent.Type).To(Equal(watch.Added)) | |
| + annotatedEvt, isEvent := resultEvent.Object.(*eventsv1.Event) | |
| + Expect(isEvent).To(BeTrue()) | |
| + | |
| + Expect(annotatedEvt.Regarding).To(Equal(*dpRef)) | |
| + Expect(annotatedEvt.Type).To(Equal(corev1.EventTypeNormal)) | |
| + Expect(annotatedEvt.Reason).To(Equal("test-annotated-reason")) | |
| + Expect(annotatedEvt.Action).To(Equal("test-annotated-action")) | |
| + Expect(annotatedEvt.Note).To(Equal("test-annotated-note")) | |
| + Expect(annotatedEvt.Annotations).To(HaveKeyWithValue("key", "value")) | |
| }) | |
| }) | |
| }) | |
| diff --git a/pkg/internal/recorder/recorder_test.go b/pkg/internal/recorder/recorder_test.go | |
| index e592a1e1..50fd5979 100644 | |
| --- a/pkg/internal/recorder/recorder_test.go | |
| +++ b/pkg/internal/recorder/recorder_test.go | |
| var _ = Describe("recorder.Provider", func() { | |
| }) | |
| }) | |
| Describe("GetEventRecorder", func() { | |
| - It("should return a recorder instance.", func() { | |
| + It("should return a recorder instance that supports both Eventf and AnnotatedEventf.", func() { | |
| provider, err := recorder.NewProvider(cfg, httpClient, scheme.Scheme, logr.Discard(), makeBroadcaster()) | |
| Expect(err).NotTo(HaveOccurred()) | |
| - recorder := provider.GetEventRecorder("test") | |
| - Expect(recorder).NotTo(BeNil()) | |
| + rec := provider.GetEventRecorder("test") | |
| + Expect(rec).NotTo(BeNil()) | |
| + // Verify it satisfies both interfaces | |
| + var _ events.EventRecorder = rec | |
| + var _ events.AnnotatedEventRecorder = rec | |
| }) | |
| }) | |
| }) | |
| diff --git a/pkg/manager/internal.go b/pkg/manager/internal.go | |
| index ceb1450d..55fa2fca 100644 | |
| --- a/pkg/manager/internal.go | |
| +++ b/pkg/manager/internal.go | |
| import ( | |
| "k8s.io/apimachinery/pkg/runtime" | |
| kerrors "k8s.io/apimachinery/pkg/util/errors" | |
| "k8s.io/client-go/rest" | |
| - "k8s.io/client-go/tools/events" | |
| "k8s.io/client-go/tools/leaderelection" | |
| "k8s.io/client-go/tools/leaderelection/resourcelock" | |
| "k8s.io/client-go/tools/record" | |
| import ( | |
| "sigs.k8s.io/controller-runtime/pkg/internal/httpserver" | |
| intrec "sigs.k8s.io/controller-runtime/pkg/internal/recorder" | |
| metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | |
| + "sigs.k8s.io/controller-runtime/pkg/recorder" | |
| "sigs.k8s.io/controller-runtime/pkg/webhook" | |
| ) | |
| func (cm *controllerManager) GetEventRecorderFor(name string) record.EventRecord | |
| return cm.cluster.GetEventRecorderFor(name) //nolint:staticcheck | |
| } | |
| -func (cm *controllerManager) GetEventRecorder(name string) events.EventRecorder { | |
| +func (cm *controllerManager) GetEventRecorder(name string) recorder.EventRecorder { | |
| return cm.cluster.GetEventRecorder(name) | |
| } | |
| diff --git a/pkg/recorder/recorder.go b/pkg/recorder/recorder.go | |
| index 896098ad..945202f8 100644 | |
| --- a/pkg/recorder/recorder.go | |
| +++ b/pkg/recorder/recorder.go | |
| import ( | |
| "k8s.io/client-go/tools/record" | |
| ) | |
| +// EventRecorder combines both events.EventRecorder and events.AnnotatedEventRecorder | |
| +// so that callers only need a single recorder to emit both regular and annotated events. | |
| +type EventRecorder interface { | |
| + events.EventRecorder | |
| + events.AnnotatedEventRecorder | |
| +} | |
| + | |
| // Provider knows how to generate new event recorders with given name. | |
| type Provider interface { | |
| // GetEventRecorderFor returns an EventRecorder for the old events API. | |
| type Provider interface { | |
| // reportingController. reportingInstance must be no more than 128 | |
| // characters, so callers should ensure that len(name) + 1 + len(hostname) | |
| // is at most 128. | |
| - GetEventRecorder(name string) events.EventRecorder | |
| + // The returned recorder supports both Eventf and AnnotatedEventf. | |
| + GetEventRecorder(name string) EventRecorder | |
| } | |