Skip to content

Commit

Permalink
verify pod on type assertion (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanMelnyk113 authored Sep 8, 2023
1 parent 0ed27ed commit 49b4895
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions kube/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,26 @@ type PodByIPCache struct {
}

func (p *PodByIPCache) Get(ip string) (*corev1.Pod, error) {
pods, err := p.informer.GetIndexer().ByIndex(podIPIdx, ip)
items, err := p.informer.GetIndexer().ByIndex(podIPIdx, ip)
if err != nil {
return nil, err
}
if len(pods) == 0 {
if len(items) == 0 {
return nil, ErrNotFound
}
pods := lo.FilterMap(items, func(item interface{}, i int) (*corev1.Pod, bool) {
pod, ok := item.(*corev1.Pod)
if !ok {
return nil, false
}
return pod, true
})
sort.SliceStable(pods, func(i, j int) bool {
return pods[i].(*corev1.Pod).CreationTimestamp.Before(&pods[j].(*corev1.Pod).CreationTimestamp)
return pods[i].CreationTimestamp.Before(&pods[j].CreationTimestamp)
})
for i := range pods {
if pod := pods[i]; pod != nil {
return pod.(*corev1.Pod), nil
return pod, nil
}
}
return nil, ErrNotFound
Expand Down

0 comments on commit 49b4895

Please sign in to comment.