@@ -17,7 +17,7 @@ limitations under the License.
1717package events
1818
1919import (
20- "bytes "
20+ "context "
2121 "encoding/json"
2222 "errors"
2323 "fmt"
@@ -93,7 +93,7 @@ func NewRecorder(mgr ctrl.Manager, log logr.Logger, webhook, reportingController
9393
9494 httpClient := retryablehttp .NewClient ()
9595 httpClient .HTTPClient .Timeout = 5 * time .Second
96- httpClient .CheckRetry = retryablehttp . ErrorPropagatedRetryPolicy
96+ httpClient .CheckRetry = checkRetry
9797 httpClient .Logger = nil
9898
9999 return & Recorder {
@@ -120,7 +120,7 @@ func NewRecorderForScheme(scheme *runtime.Scheme,
120120
121121 httpClient := retryablehttp .NewClient ()
122122 httpClient .HTTPClient .Timeout = 5 * time .Second
123- httpClient .CheckRetry = retryablehttp . ErrorPropagatedRetryPolicy
123+ httpClient .CheckRetry = checkRetry
124124 httpClient .Logger = nil
125125
126126 return & Recorder {
@@ -133,6 +133,19 @@ func NewRecorderForScheme(scheme *runtime.Scheme,
133133 }, nil
134134}
135135
136+ func checkRetry (ctx context.Context , resp * http.Response , err error ) (bool , error ) {
137+ if resp != nil && responseIsMessageIsDuplicated (resp ) {
138+ return false , nil // Don't retry
139+ }
140+ return retryablehttp .ErrorPropagatedRetryPolicy (ctx , resp , err )
141+ }
142+
143+ // The Notification Controller returns a 429 Too Many Requests response
144+ // when the posted message is a duplicate (within a certain time window).
145+ func responseIsMessageIsDuplicated (resp * http.Response ) bool {
146+ return resp .StatusCode == http .StatusTooManyRequests
147+ }
148+
136149// Event records an event in the webhook address.
137150func (r * Recorder ) Event (object runtime.Object , eventtype , reason , message string ) {
138151 r .AnnotatedEventf (object , nil , eventtype , reason , "%s" , message )
@@ -250,12 +263,6 @@ func (r *Recorder) AnnotatedEventf(
250263 return
251264 }
252265
253- // avoid retrying rate limited requests
254- if res , _ := r .Client .HTTPClient .Post (r .Webhook , "application/json" , bytes .NewReader (body )); res != nil &&
255- (res .StatusCode == http .StatusTooManyRequests || res .StatusCode == http .StatusAccepted ) {
256- return
257- }
258-
259266 if _ , err := r .Client .Post (r .Webhook , "application/json" , body ); err != nil {
260267 log .Error (err , "unable to record event" )
261268 return
0 commit comments