Skip to content

Commit e9c6ae4

Browse files
Avoid using deepcopy.
there is no need to copy the pod to the event object, the pod can be fetched from the informers cache.
1 parent abc8a01 commit e9c6ae4

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

internal/controller/controller.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
type PodEvent struct {
2424
Key string
2525
EventType string
26-
Pod *corev1.Pod
2726
}
2827

2928
// Controller is the Kubernetes controller for tracking deployments.
@@ -99,7 +98,6 @@ func New(clientset kubernetes.Interface, namespace string, cfg *Config) *Control
9998
queue.Add(PodEvent{
10099
Key: key,
101100
EventType: "CREATED",
102-
Pod: pod.DeepCopy(),
103101
})
104102
}
105103
}
@@ -137,20 +135,19 @@ func New(clientset kubernetes.Interface, namespace string, cfg *Config) *Control
137135
queue.Add(PodEvent{
138136
Key: key,
139137
EventType: "CREATED",
140-
Pod: newPod.DeepCopy(),
141138
})
142139
}
143140
}
144141
},
145142
DeleteFunc: func(obj any) {
146-
pod, ok := obj.(*corev1.Pod)
143+
_, ok := obj.(*corev1.Pod)
147144
if !ok {
148145
// Handle deleted final state unknown
149146
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
150147
if !ok {
151148
return
152149
}
153-
pod, ok = tombstone.Obj.(*corev1.Pod)
150+
_, ok = tombstone.Obj.(*corev1.Pod)
154151
if !ok {
155152
return
156153
}
@@ -164,7 +161,6 @@ func New(clientset kubernetes.Interface, namespace string, cfg *Config) *Control
164161
queue.Add(PodEvent{
165162
Key: key,
166163
EventType: "DELETED",
167-
Pod: pod.DeepCopy(),
168164
})
169165
}
170166
},
@@ -243,8 +239,25 @@ func (c *Controller) processNextItem(ctx context.Context) bool {
243239

244240
// processEvent processes a single pod event.
245241
func (c *Controller) processEvent(ctx context.Context, event PodEvent) error {
246-
pod := event.Pod
247-
if pod == nil {
242+
// Get the pod from the informer's cache
243+
obj, exists, err := c.podInformer.GetIndexer().GetByKey(event.Key)
244+
if err != nil {
245+
slog.Error("Failed to get pod from cache",
246+
"key", event.Key,
247+
"error", err,
248+
)
249+
return nil
250+
}
251+
if !exists {
252+
// Pod no longer exists in cache, skip processing
253+
return nil
254+
}
255+
256+
pod, ok := obj.(*corev1.Pod)
257+
if !ok {
258+
slog.Error("Invalid object type in cache",
259+
"key", event.Key,
260+
)
248261
return nil
249262
}
250263

@@ -333,6 +346,8 @@ func getARDeploymentName(p *corev1.Pod, c corev1.Container, tmpl string) string
333346
}
334347

335348
// getContainerDigest extracts the image digest from the container status.
349+
// The spec only contains the desired state, so any resoled digests must
350+
// be pulled from the status field.
336351
func getContainerDigest(pod *corev1.Pod, containerName string) string {
337352
// Check regular container statuses
338353
for _, status := range pod.Status.ContainerStatuses {

0 commit comments

Comments
 (0)