Skip to content

Commit ba747f3

Browse files
Extact the pod if it's being deleted to avoid a future race condition
during processing when the event is pulled of the work queue.
1 parent 7de53eb commit ba747f3

1 file changed

Lines changed: 42 additions & 25 deletions

File tree

internal/controller/controller.go

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323

2424
// PodEvent represents a pod event to be processed.
2525
type PodEvent struct {
26-
Key string
27-
EventType string
26+
Key string
27+
EventType string
28+
DeletedPod *corev1.Pod // Only populated for delete events
2829
}
2930

3031
// Controller is the Kubernetes controller for tracking deployments.
@@ -149,14 +150,14 @@ func New(clientset kubernetes.Interface, namespace string, cfg *Config) (*Contro
149150
}
150151
},
151152
DeleteFunc: func(obj any) {
152-
_, ok := obj.(*corev1.Pod)
153+
pod, ok := obj.(*corev1.Pod)
153154
if !ok {
154155
// Handle deleted final state unknown
155156
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
156157
if !ok {
157158
return
158159
}
159-
_, ok = tombstone.Obj.(*corev1.Pod)
160+
pod, ok = tombstone.Obj.(*corev1.Pod)
160161
if !ok {
161162
return
162163
}
@@ -168,8 +169,9 @@ func New(clientset kubernetes.Interface, namespace string, cfg *Config) (*Contro
168169
// bother with handling it.
169170
if err == nil {
170171
queue.Add(PodEvent{
171-
Key: key,
172-
EventType: "DELETED",
172+
Key: key,
173+
EventType: "DELETED",
174+
DeletedPod: pod,
173175
})
174176
}
175177
},
@@ -255,26 +257,41 @@ func (c *Controller) processNextItem(ctx context.Context) bool {
255257

256258
// processEvent processes a single pod event.
257259
func (c *Controller) processEvent(ctx context.Context, event PodEvent) error {
258-
// Get the pod from the informer's cache
259-
obj, exists, err := c.podInformer.GetIndexer().GetByKey(event.Key)
260-
if err != nil {
261-
slog.Error("Failed to get pod from cache",
262-
"key", event.Key,
263-
"error", err,
264-
)
265-
return nil
266-
}
267-
if !exists {
268-
// Pod no longer exists in cache, skip processing
269-
return nil
270-
}
260+
var pod *corev1.Pod
271261

272-
pod, ok := obj.(*corev1.Pod)
273-
if !ok {
274-
slog.Error("Invalid object type in cache",
275-
"key", event.Key,
276-
)
277-
return nil
262+
if event.EventType == "DELETED" {
263+
// For delete events, use the pod captured at deletion time
264+
// since it's already been removed from the cache
265+
pod = event.DeletedPod
266+
if pod == nil {
267+
slog.Error("Delete event missing pod data",
268+
"key", event.Key,
269+
)
270+
return nil
271+
}
272+
} else {
273+
// For other events, get the pod from the informer's cache
274+
obj, exists, err := c.podInformer.GetIndexer().GetByKey(event.Key)
275+
if err != nil {
276+
slog.Error("Failed to get pod from cache",
277+
"key", event.Key,
278+
"error", err,
279+
)
280+
return nil
281+
}
282+
if !exists {
283+
// Pod no longer exists in cache, skip processing
284+
return nil
285+
}
286+
287+
var ok bool
288+
pod, ok = obj.(*corev1.Pod)
289+
if !ok {
290+
slog.Error("Invalid object type in cache",
291+
"key", event.Key,
292+
)
293+
return nil
294+
}
278295
}
279296

280297
status := deploymentrecord.StatusDeployed

0 commit comments

Comments
 (0)