Skip to content

Commit

Permalink
Allow the network mapper to resolve DNS on its own if no DNS traffic …
Browse files Browse the repository at this point in the history
…has been seen and an unresolved address appears
  • Loading branch information
orishoshan committed Sep 18, 2024
1 parent 6c1314d commit eec41ad
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/mapper/pkg/dnscache/dns_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func NewDNSCache() *DNSCache {
}
}

func (d *DNSCache) AddOrUpdateDNSData(dnsName string, ip string, ttlSeconds int) {
ttl := time.Duration(ttlSeconds) * time.Second
func (d *DNSCache) AddOrUpdateDNSData(dnsName string, ip string, ttl time.Duration) {
d.cache.Set(dnsName, ip, ttl)
d.ipToNameCache.Set(ip, dnsName, ttl)
}
Expand Down
10 changes: 5 additions & 5 deletions src/mapper/pkg/dnscache/dns_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ func (s *DNSCacheTestSuite) TearDownTest() {

func (s *DNSCacheTestSuite) TestDNSCache() {
cache := NewDNSCache()
cache.AddOrUpdateDNSData("good-news.com", IP1, 60)
cache.AddOrUpdateDNSData("good-news.com", IP1, 60*time.Second)
ip, found := cache.GetResolvedIP("good-news.com")
s.Require().True(found)
s.Require().Equal(IP1, ip)

cache.AddOrUpdateDNSData("good-news.com", IP2, 60)
cache.AddOrUpdateDNSData("good-news.com", IP2, 60*time.Second)
ip, found = cache.GetResolvedIP("good-news.com")
s.Require().True(found)
s.Require().Equal(IP2, ip)

_, found = cache.GetResolvedIP("bad-news.de")
s.Require().False(found)

cache.AddOrUpdateDNSData("bad-news.de", IP1, 60)
cache.AddOrUpdateDNSData("bad-news.de", IP1, 60*time.Second)
ip, found = cache.GetResolvedIP("bad-news.de")
s.Require().True(found)
s.Require().Equal(IP1, ip)
Expand All @@ -50,7 +50,7 @@ func (s *DNSCacheTestSuite) TestCapacityConfig() {
names := make([]string, 0)
for i := 0; i < capacityLimit+1; i++ {
dnsName := fmt.Sprintf("dns-%d.com", i)
cache.AddOrUpdateDNSData(dnsName, IP1, 60)
cache.AddOrUpdateDNSData(dnsName, IP1, 60*time.Second)
names = append(names, dnsName)
}

Expand All @@ -67,7 +67,7 @@ func (s *DNSCacheTestSuite) TestCapacityConfig() {
func (s *DNSCacheTestSuite) TestTTL() {
cache := NewDNSCache()

cache.AddOrUpdateDNSData("my-future-blog.de", IP1, 1)
cache.AddOrUpdateDNSData("my-future-blog.de", IP1, 1*time.Second)
ip, found := cache.GetResolvedIP("my-future-blog.de")
s.Require().True(found)
s.Require().Equal(IP1, ip)
Expand Down
38 changes: 28 additions & 10 deletions src/mapper/pkg/dnsintentspublisher/dns_intents_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
otterizev2alpha1 "github.com/otterize/intents-operator/src/operator/api/v2alpha1"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/otterize/network-mapper/src/mapper/pkg/config"
"github.com/otterize/network-mapper/src/mapper/pkg/dnscache"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"net"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"
Expand All @@ -19,17 +21,13 @@ const (
hasAnyDnsIntentsIndexValue = "true"
)

type DnsCache interface {
GetResolvedIP(dnsName string) (string, bool)
}

type Publisher struct {
client client.Client
dnsCache DnsCache
dnsCache *dnscache.DNSCache
updateInterval time.Duration
}

func NewPublisher(k8sClient client.Client, dnsCache DnsCache) *Publisher {
func NewPublisher(k8sClient client.Client, dnsCache *dnscache.DNSCache) *Publisher {
return &Publisher{
client: k8sClient,
dnsCache: dnsCache,
Expand Down Expand Up @@ -153,14 +151,34 @@ func (p *Publisher) compareIntentsAndStatus(clientIntents otterizev2alpha1.Clien
}

func (p *Publisher) appendResolvedIps(dnsName string, resolvedIPsMap map[string][]string) bool {
resolvedIP, ok := p.dnsCache.GetResolvedIP(dnsName)
if !ok {
return false
}
resolvedIP, ipResolved := p.dnsCache.GetResolvedIP(dnsName)

ips, ok := resolvedIPsMap[dnsName]
if !ok {
ips = make([]string, 0)
if !ipResolved {
// Try to resolve it ourselves
ctxTimeout, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
logrus.WithField("dnsName", dnsName).Warn("DNS cache miss, resolving it ourselves")
ipaddrs, err := net.DefaultResolver.LookupIPAddr(ctxTimeout, dnsName)
if err != nil {
logrus.WithError(err).WithField("dnsName", dnsName).Error("Failed to resolve DNS")
return false
}

for _, ip := range ipaddrs {
ips = append(ips, ip.String())
p.dnsCache.AddOrUpdateDNSData(dnsName, ip.String(), 60)
}
resolvedIPsMap[dnsName] = ips
return true
}
}

// This happens when we've resolved the IP ourselves in a prior run, and still have no new passive resolution this time.
if !ipResolved {
return false
}

if slices.Contains(ips, resolvedIP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ func (s *PublisherTestSuite) TestUpdate() {
s.Require().NoError(err)
}

func ttlForTest() int {
return int(time.Hour.Seconds())
func ttlForTest() time.Duration {
return time.Hour
}

func TestPublisherTestSuite(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions src/mapper/pkg/resolvers/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (r *Resolver) Register(e *echo.Echo) {

func (r *Resolver) RunForever(ctx context.Context) error {
errgrp, errGrpCtx := errgroup.WithContext(ctx)
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
return runHandleLoop(errGrpCtx, r.dnsCaptureResults, r.handleReportCaptureResults)
})
//errgrp.Go(func() error {
// defer errorreporter.AutoNotify()
// return runHandleLoop(errGrpCtx, r.dnsCaptureResults, r.handleReportCaptureResults)
//})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
return runHandleLoop(errGrpCtx, r.tcpCaptureResults, r.handleReportTCPCaptureResults)
Expand Down
6 changes: 5 additions & 1 deletion src/mapper/pkg/resolvers/schema.helpers.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ func (r *Resolver) handleDNSCaptureResultsAsExternalTraffic(_ context.Context, d
if dest.DestinationIP != nil {
ip = *dest.DestinationIP
intent.IPs = map[externaltrafficholder.IP]struct{}{externaltrafficholder.IP(*dest.DestinationIP): {}}
r.dnsCache.AddOrUpdateDNSData(dest.Destination, ip, int(lo.FromPtr(dest.TTL)))
ttl := 60 * time.Second
if dest.TTL != nil {
ttl = time.Duration(*dest.TTL) * time.Second
}
r.dnsCache.AddOrUpdateDNSData(dest.Destination, ip, ttl)
}
logrus.Debugf("Saw external traffic, from '%s.%s' to '%s' (IP '%s')", srcSvcIdentity.Name, srcSvcIdentity.Namespace, dest.Destination, ip)

Expand Down

0 comments on commit eec41ad

Please sign in to comment.