Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/watcher/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (watcher *FakeK8sWatcher) FindMirrorPod(_ string) (*corev1.Pod, error) {
}

func (watcher *FakeK8sWatcher) FindPod(podID string) (*corev1.Pod, error) {
ret, ok := findPod(watcher.pods)
ret, ok := findPod(podID, watcher.pods)
if ok {
return ret, nil
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ func (watcher *K8sWatcher) FindPod(podID string) (*corev1.Pod, error) {
if podInformer == nil {
return nil, fmt.Errorf("pod informer not initialized")
}

// First try to find the pod by index
objs, err := podInformer.GetIndexer().ByIndex(podIdx, podID)
if err != nil {
return nil, fmt.Errorf("watcher returned: %w", err)
Expand All @@ -282,17 +284,18 @@ func (watcher *K8sWatcher) FindPod(podID string) (*corev1.Pod, error) {
return nil, fmt.Errorf("unexpected type %t", objs[0])
}

// If unsuccessful, fall back to walking the entire pod list
allPods := podInformer.GetStore().List()
if pod, ok := findPod(allPods); ok {
if pod, ok := findPod(podID, allPods); ok {
return pod, nil
}
return nil, fmt.Errorf("unable to find pod with ID %s (index pods=%d all pods=%d)", podID, len(objs), len(allPods))
}

func findPod(pods []interface{}) (*corev1.Pod, bool) {
func findPod(podID string, pods []interface{}) (*corev1.Pod, bool) {
for i := range pods {
if pod, ok := pods[i].(*corev1.Pod); ok {
if pod.UID == podIdx {
if string(pod.UID) == podID {
return pod, true
}
}
Expand Down
Loading