@@ -3,6 +3,7 @@ package kubefinder
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "github.com/hashicorp/golang-lru/v2/expirable"
6
7
"github.com/otterize/intents-operator/src/shared/errors"
7
8
"github.com/otterize/intents-operator/src/shared/serviceidresolver"
8
9
"github.com/otterize/network-mapper/src/mapper/pkg/config"
@@ -37,22 +38,30 @@ type KubeFinder struct {
37
38
mgr manager.Manager
38
39
client client.Client
39
40
serviceIdResolver * serviceidresolver.Resolver
41
+ SeenIPsTTLCache * expirable.LRU [string , struct {}]
40
42
}
41
43
42
- var ErrNoPodFound = errors .NewSentinelError ("no pod found" )
43
- var ErrFoundMoreThanOnePod = errors .NewSentinelError ("ip belongs to more than one pod" )
44
- var ErrFoundMoreThanOneService = errors .NewSentinelError ("ip belongs to more than one service" )
45
- var ErrServiceNotFound = errors .NewSentinelError ("service not found" )
44
+ var (
45
+ ErrNoPodFound = errors .NewSentinelError ("no pod found" )
46
+ ErrFoundMoreThanOnePod = errors .NewSentinelError ("ip belongs to more than one pod" )
47
+ ErrFoundMoreThanOneService = errors .NewSentinelError ("ip belongs to more than one service" )
48
+ ErrServiceNotFound = errors .NewSentinelError ("service not found" )
49
+ )
46
50
47
51
func NewKubeFinder (ctx context.Context , mgr manager.Manager ) (* KubeFinder , error ) {
48
52
indexer := & KubeFinder {client : mgr .GetClient (), mgr : mgr , serviceIdResolver : serviceidresolver .NewResolver (mgr .GetClient ())}
53
+ indexer .initCache ()
49
54
err := indexer .initIndexes (ctx )
50
55
if err != nil {
51
56
return nil , errors .Wrap (err )
52
57
}
53
58
return indexer , nil
54
59
}
55
60
61
+ func (k * KubeFinder ) initCache () {
62
+ k .SeenIPsTTLCache = expirable .NewLRU [string , struct {}](2000 , nil , time .Minute * 10 )
63
+ }
64
+
56
65
func (k * KubeFinder ) initIndexes (ctx context.Context ) error {
57
66
err := k .mgr .GetCache ().IndexField (ctx , & corev1.Pod {}, podIPIndexField , func (object client.Object ) []string {
58
67
res := make ([]string , 0 )
@@ -80,6 +89,7 @@ func (k *KubeFinder) initIndexes(ctx context.Context) error {
80
89
}
81
90
82
91
for _ , ip := range pod .Status .PodIPs {
92
+ k .SeenIPsTTLCache .Add (ip .IP , struct {}{})
83
93
res = append (res , ip .IP )
84
94
}
85
95
return res
@@ -464,7 +474,12 @@ func (k *KubeFinder) ResolveOtterizeIdentityForService(ctx context.Context, svc
464
474
}
465
475
466
476
func (k * KubeFinder ) IsSrcIpClusterInternal (ctx context.Context , ip string ) (bool , error ) {
467
- // Known issue: this function is currently missing support for services/endpoints, node.PodCIDR, and pods that were deleted.
477
+ // Known issue: this function is currently missing support for services/endpoints, node.PodCIDR
478
+
479
+ wasPodIp := k .WasPodIP (ip )
480
+ if wasPodIp {
481
+ return true , nil
482
+ }
468
483
469
484
isNode , err := k .IsNodeIP (ctx , ip )
470
485
if err != nil {
@@ -502,6 +517,10 @@ func (k *KubeFinder) IsPodIp(ctx context.Context, ip string) (bool, error) {
502
517
return len (pods .Items ) > 0 , nil
503
518
}
504
519
520
+ func (k * KubeFinder ) WasPodIP (ip string ) bool {
521
+ return k .SeenIPsTTLCache .Contains (ip )
522
+ }
523
+
505
524
func (k * KubeFinder ) IsNodeIP (ctx context.Context , ip string ) (bool , error ) {
506
525
var nodes corev1.NodeList
507
526
err := k .client .List (ctx , & nodes , client.MatchingFields {nodeIPIndexField : ip })
0 commit comments