@@ -38,6 +38,7 @@ limitations under the License.
3838
3939import (
4040 "context"
41+ "errors"
4142 "fmt"
4243
4344 cgocache "k8s.io/client-go/tools/cache"
@@ -94,8 +95,11 @@ func (e *EventHandler[object, request]) OnAdd(obj interface{}) {
9495 if o , ok := obj .(object ); ok {
9596 c .Object = o
9697 } else {
97- log .Error (nil , "OnAdd missing Object" ,
98- "object" , obj , "type" , fmt .Sprintf ("%T" , obj ))
98+ log .Error (errors .New ("failed to cast object" ),
99+ "OnAdd missing Object" ,
100+ "expected_type" , fmt .Sprintf ("%T" , c .Object ),
101+ "received_type" , fmt .Sprintf ("%T" , obj ),
102+ "object" , obj )
99103 return
100104 }
101105
@@ -118,20 +122,27 @@ func (e *EventHandler[object, request]) OnUpdate(oldObj, newObj interface{}) {
118122 if o , ok := oldObj .(object ); ok {
119123 u .ObjectOld = o
120124 } else {
121- log .Error (nil , "OnUpdate missing ObjectOld" ,
122- "object" , oldObj , "type" , fmt .Sprintf ("%T" , oldObj ))
125+ log .Error (errors .New ("failed to cast old object" ),
126+ "OnUpdate missing ObjectOld" ,
127+ "object" , oldObj ,
128+ "expected_type" , fmt .Sprintf ("%T" , u .ObjectOld ),
129+ "received_type" , fmt .Sprintf ("%T" , oldObj ))
123130 return
124131 }
125132
126133 // Pull Object out of the object
127134 if o , ok := newObj .(object ); ok {
128135 u .ObjectNew = o
129136 } else {
130- log .Error (nil , "OnUpdate missing ObjectNew" ,
131- "object" , newObj , "type" , fmt .Sprintf ("%T" , newObj ))
137+ log .Error (errors .New ("failed to cast new object" ),
138+ "OnUpdate missing ObjectNew" ,
139+ "object" , newObj ,
140+ "expected_type" , fmt .Sprintf ("%T" , u .ObjectNew ),
141+ "received_type" , fmt .Sprintf ("%T" , newObj ))
132142 return
133143 }
134144
145+ // Run predicates before proceeding
135146 for _ , p := range e .predicates {
136147 if ! p .Update (u ) {
137148 return
@@ -148,18 +159,25 @@ func (e *EventHandler[object, request]) OnUpdate(oldObj, newObj interface{}) {
148159func (e * EventHandler [object , request ]) OnDelete (obj interface {}) {
149160 d := event.TypedDeleteEvent [object ]{}
150161
162+ // Handle tombstone events (cache.DeletedFinalStateUnknown)
163+ if obj == nil {
164+ log .Error (errors .New ("received nil object" ),
165+ "OnDelete received a nil object, ignoring event" )
166+ return
167+ }
168+
151169 // Deal with tombstone events by pulling the object out. Tombstone events wrap the object in a
152170 // DeleteFinalStateUnknown struct, so the object needs to be pulled out.
153171 // Copied from sample-controller
154172 // This should never happen if we aren't missing events, which we have concluded that we are not
155173 // and made decisions off of this belief. Maybe this shouldn't be here?
156- var ok bool
157- if _ , ok = obj .(client.Object ); ! ok {
174+ if _ , ok := obj .(client.Object ); ! ok {
158175 // If the object doesn't have Metadata, assume it is a tombstone object of type DeletedFinalStateUnknown
159176 tombstone , ok := obj .(cgocache.DeletedFinalStateUnknown )
160177 if ! ok {
161- log .Error (nil , "Error decoding objects. Expected cache.DeletedFinalStateUnknown" ,
162- "type" , fmt .Sprintf ("%T" , obj ),
178+ log .Error (errors .New ("unexpected object type" ),
179+ "Error decoding objects, expected cache.DeletedFinalStateUnknown" ,
180+ "received_type" , fmt .Sprintf ("%T" , obj ),
163181 "object" , obj )
164182 return
165183 }
@@ -175,8 +193,11 @@ func (e *EventHandler[object, request]) OnDelete(obj interface{}) {
175193 if o , ok := obj .(object ); ok {
176194 d .Object = o
177195 } else {
178- log .Error (nil , "OnDelete missing Object" ,
179- "object" , obj , "type" , fmt .Sprintf ("%T" , obj ))
196+ log .Error (errors .New ("failed to cast object" ),
197+ "OnDelete missing Object" ,
198+ "expected_type" , fmt .Sprintf ("%T" , d .Object ),
199+ "received_type" , fmt .Sprintf ("%T" , obj ),
200+ "object" , obj )
180201 return
181202 }
182203
0 commit comments