@@ -38,6 +38,7 @@ limitations under the License.
38
38
39
39
import (
40
40
"context"
41
+ "errors"
41
42
"fmt"
42
43
43
44
cgocache "k8s.io/client-go/tools/cache"
@@ -94,8 +95,11 @@ func (e *EventHandler[object, request]) OnAdd(obj interface{}) {
94
95
if o , ok := obj .(object ); ok {
95
96
c .Object = o
96
97
} 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 )
99
103
return
100
104
}
101
105
@@ -118,20 +122,27 @@ func (e *EventHandler[object, request]) OnUpdate(oldObj, newObj interface{}) {
118
122
if o , ok := oldObj .(object ); ok {
119
123
u .ObjectOld = o
120
124
} 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 ))
123
130
return
124
131
}
125
132
126
133
// Pull Object out of the object
127
134
if o , ok := newObj .(object ); ok {
128
135
u .ObjectNew = o
129
136
} 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 ))
132
142
return
133
143
}
134
144
145
+ // Run predicates before proceeding
135
146
for _ , p := range e .predicates {
136
147
if ! p .Update (u ) {
137
148
return
@@ -148,18 +159,25 @@ func (e *EventHandler[object, request]) OnUpdate(oldObj, newObj interface{}) {
148
159
func (e * EventHandler [object , request ]) OnDelete (obj interface {}) {
149
160
d := event.TypedDeleteEvent [object ]{}
150
161
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
+
151
169
// Deal with tombstone events by pulling the object out. Tombstone events wrap the object in a
152
170
// DeleteFinalStateUnknown struct, so the object needs to be pulled out.
153
171
// Copied from sample-controller
154
172
// This should never happen if we aren't missing events, which we have concluded that we are not
155
173
// 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 {
158
175
// If the object doesn't have Metadata, assume it is a tombstone object of type DeletedFinalStateUnknown
159
176
tombstone , ok := obj .(cgocache.DeletedFinalStateUnknown )
160
177
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 ),
163
181
"object" , obj )
164
182
return
165
183
}
@@ -175,8 +193,11 @@ func (e *EventHandler[object, request]) OnDelete(obj interface{}) {
175
193
if o , ok := obj .(object ); ok {
176
194
d .Object = o
177
195
} 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 )
180
201
return
181
202
}
182
203
0 commit comments