From 174309acc86e708193fd479cba2a11efe5f81de5 Mon Sep 17 00:00:00 2001 From: Amit Schendel Date: Thu, 7 Nov 2024 10:02:18 +0000 Subject: [PATCH 1/4] Adding cache for dns Signed-off-by: Amit Schendel --- main.go | 2 +- pkg/dnsmanager/dns_manager.go | 123 ++++++++++++++++++++++++++--- pkg/dnsmanager/dns_manager_test.go | 75 ++++++++++++++---- 3 files changed, 173 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 34e0318b..6deafa64 100644 --- a/main.go +++ b/main.go @@ -198,7 +198,7 @@ func main() { var dnsManagerClient dnsmanager.DNSManagerClient var dnsResolver dnsmanager.DNSResolver if cfg.EnableNetworkTracing || cfg.EnableRuntimeDetection { - dnsManager := dnsmanager.CreateDNSManager() + dnsManager := dnsmanager.CreateDNSManager(ctx) dnsManagerClient = dnsManager // NOTE: dnsResolver is set for threat detection. dnsResolver = dnsManager diff --git a/pkg/dnsmanager/dns_manager.go b/pkg/dnsmanager/dns_manager.go index 3a7d2f49..a8ef3e8a 100644 --- a/pkg/dnsmanager/dns_manager.go +++ b/pkg/dnsmanager/dns_manager.go @@ -1,37 +1,140 @@ package dnsmanager import ( + "context" "net" + "sync" + "time" "github.com/goradd/maps" tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" ) -// DNSManager is used to manage DNS events and save IP resolutions. It exposes an API to resolve IP address to domain name. +// DNSManager is used to manage DNS events and save IP resolutions. type DNSManager struct { - addressToDomainMap maps.SafeMap[string, string] // this map is used to resolve IP address to domain name + addressToDomainMap maps.SafeMap[string, string] + lookupCache *sync.Map // Cache for DNS lookups + failureCache *sync.Map // Cache for failed lookups to prevent repeated attempts + cleanupTicker *time.Ticker // Ticker for periodic cache cleanup + cancel context.CancelFunc // Cancel function for cleanup goroutine } +type cacheEntry struct { + addresses []string + timestamp time.Time +} + +const ( + defaultPositiveTTL = 1 * time.Minute // Default TTL for successful lookups + defaultNegativeTTL = 5 * time.Second // Default TTL for failed lookups + cleanupInterval = 5 * time.Minute // How often to run cache cleanup +) + var _ DNSManagerClient = (*DNSManager)(nil) var _ DNSResolver = (*DNSManager)(nil) -func CreateDNSManager() *DNSManager { - return &DNSManager{} +func CreateDNSManager(ctx context.Context) *DNSManager { + ctx, cancel := context.WithCancel(ctx) + dm := &DNSManager{ + lookupCache: &sync.Map{}, + failureCache: &sync.Map{}, + cleanupTicker: time.NewTicker(cleanupInterval), + cancel: cancel, + } + + // Start the cleanup goroutine + go dm.cacheCleaner(ctx) + + return dm } func (dm *DNSManager) ReportDNSEvent(dnsEvent tracerdnstype.Event) { - + // If we have addresses in the event, use them directly if len(dnsEvent.Addresses) > 0 { for _, address := range dnsEvent.Addresses { dm.addressToDomainMap.Set(address, dnsEvent.DNSName) } - } else { - addresses, err := net.LookupIP(dnsEvent.DNSName) - if err != nil { + + // Update the cache with these known good addresses + dm.lookupCache.Store(dnsEvent.DNSName, cacheEntry{ + addresses: dnsEvent.Addresses, + timestamp: time.Now(), + }) + return + } + + // Check if we've recently failed to look up this domain + if failedTime, failed := dm.failureCache.Load(dnsEvent.DNSName); failed { + if time.Since(failedTime.(time.Time)) < defaultNegativeTTL { return } - for _, address := range addresses { - dm.addressToDomainMap.Set(address.String(), dnsEvent.DNSName) + // Failed entry has expired, remove it + dm.failureCache.Delete(dnsEvent.DNSName) + } + + // Check if we have a cached result + if cached, ok := dm.lookupCache.Load(dnsEvent.DNSName); ok { + entry := cached.(cacheEntry) + if time.Since(entry.timestamp) < defaultPositiveTTL { + // Use cached addresses + for _, addr := range entry.addresses { + dm.addressToDomainMap.Set(addr, dnsEvent.DNSName) + } + return + } + } + + // Only perform lookup if we don't have cached results + addresses, err := net.LookupIP(dnsEvent.DNSName) + if err != nil { + // Cache the failure + dm.failureCache.Store(dnsEvent.DNSName, time.Now()) + return + } + + // Convert addresses to strings and store them + addrStrings := make([]string, 0, len(addresses)) + for _, addr := range addresses { + addrStr := addr.String() + addrStrings = append(addrStrings, addrStr) + dm.addressToDomainMap.Set(addrStr, dnsEvent.DNSName) + } + + // Cache the successful lookup + dm.lookupCache.Store(dnsEvent.DNSName, cacheEntry{ + addresses: addrStrings, + timestamp: time.Now(), + }) +} + +// cacheCleaner runs periodically to clean up expired entries from both caches +func (dm *DNSManager) cacheCleaner(ctx context.Context) { + for { + select { + case <-dm.cleanupTicker.C: + now := time.Now() + + // Clean up positive cache + dm.lookupCache.Range(func(key, value interface{}) bool { + entry := value.(cacheEntry) + if now.Sub(entry.timestamp) > defaultPositiveTTL { + dm.lookupCache.Delete(key) + } + return true + }) + + // Clean up negative cache + dm.failureCache.Range(func(key, value interface{}) bool { + failedTime := value.(time.Time) + if now.Sub(failedTime) > defaultNegativeTTL { + dm.failureCache.Delete(key) + } + return true + }) + + case <-ctx.Done(): + dm.cleanupTicker.Stop() + return } } } diff --git a/pkg/dnsmanager/dns_manager_test.go b/pkg/dnsmanager/dns_manager_test.go index bc8edebc..30a9a218 100644 --- a/pkg/dnsmanager/dns_manager_test.go +++ b/pkg/dnsmanager/dns_manager_test.go @@ -2,8 +2,11 @@ package dnsmanager import ( "net" + "sync" "testing" + "time" + "github.com/goradd/maps" tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" ) @@ -13,6 +16,7 @@ func TestResolveIPAddress(t *testing.T) { dnsEvent tracerdnstype.Event ipAddr string want string + wantOk bool }{ { name: "ip found", @@ -24,7 +28,8 @@ func TestResolveIPAddress(t *testing.T) { "67.225.146.248", }, }, - want: "test.com", + want: "test.com", + wantOk: true, }, { name: "ip not found", @@ -36,57 +41,95 @@ func TestResolveIPAddress(t *testing.T) { "54.23.332.4", }, }, - want: "", + want: "", + wantOk: false, }, { name: "no address", ipAddr: "67.225.146.248", dnsEvent: tracerdnstype.Event{ DNSName: "test.com", - NumAnswers: 0, // will not resolve + NumAnswers: 0, }, - want: "", + want: "", + wantOk: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - dm := &DNSManager{} + // Create a properly initialized DNSManager + dm := &DNSManager{ + addressToDomainMap: maps.SafeMap[string, string]{}, + lookupCache: &sync.Map{}, + failureCache: &sync.Map{}, + cleanupTicker: time.NewTicker(cleanupInterval), + } + dm.ReportDNSEvent(tt.dnsEvent) - got, _ := dm.ResolveIPAddress(tt.ipAddr) - if got != tt.want { - t.Errorf("ResolveIPAddress() got = %v, want %v", got, tt.want) + got, ok := dm.ResolveIPAddress(tt.ipAddr) + if got != tt.want || ok != tt.wantOk { + t.Errorf("ResolveIPAddress() got = %v, ok = %v, want = %v, wantOk = %v", got, ok, tt.want, tt.wantOk) } + + // Cleanup + dm.cleanupTicker.Stop() }) } } func TestResolveIPAddressFallback(t *testing.T) { + // Skip the test if running in CI or without network access + if testing.Short() { + t.Skip("Skipping test that requires network access") + } + tests := []struct { name string dnsEvent tracerdnstype.Event want string + wantOk bool }{ - { name: "dns resolution fallback", dnsEvent: tracerdnstype.Event{ - DNSName: "test.com", + DNSName: "example.com", // Using example.com as it's guaranteed to exist NumAnswers: 1, }, - want: "test.com", + want: "example.com", + wantOk: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - addresses, _ := net.LookupIP(tt.dnsEvent.DNSName) - dm := &DNSManager{} + // Create a properly initialized DNSManager + dm := &DNSManager{ + addressToDomainMap: maps.SafeMap[string, string]{}, + lookupCache: &sync.Map{}, + failureCache: &sync.Map{}, + cleanupTicker: time.NewTicker(cleanupInterval), + } + + // Perform the actual DNS lookup + addresses, err := net.LookupIP(tt.dnsEvent.DNSName) + if err != nil { + t.Skipf("DNS lookup failed: %v", err) + return + } + if len(addresses) == 0 { + t.Skip("No addresses returned from DNS lookup") + return + } + dm.ReportDNSEvent(tt.dnsEvent) - got, _ := dm.ResolveIPAddress(addresses[0].String()) - if got != tt.want { - t.Errorf("ResolveIPAddress() got = %v, want %v", got, tt.want) + got, ok := dm.ResolveIPAddress(addresses[0].String()) + if got != tt.want || ok != tt.wantOk { + t.Errorf("ResolveIPAddress() got = %v, ok = %v, want = %v, wantOk = %v", got, ok, tt.want, tt.wantOk) } + + // Cleanup + dm.cleanupTicker.Stop() }) } } From 6d71962ea017b4b90fed61dcb6913c4b55c67673 Mon Sep 17 00:00:00 2001 From: Amit Schendel Date: Thu, 7 Nov 2024 11:28:19 +0000 Subject: [PATCH 2/4] Changing cache to use istio cache Signed-off-by: Amit Schendel --- main.go | 2 +- pkg/dnsmanager/dns_manager.go | 90 +++++++----------------------- pkg/dnsmanager/dns_manager_test.go | 78 ++++++++++++++++++-------- 3 files changed, 75 insertions(+), 95 deletions(-) diff --git a/main.go b/main.go index 6deafa64..34e0318b 100644 --- a/main.go +++ b/main.go @@ -198,7 +198,7 @@ func main() { var dnsManagerClient dnsmanager.DNSManagerClient var dnsResolver dnsmanager.DNSResolver if cfg.EnableNetworkTracing || cfg.EnableRuntimeDetection { - dnsManager := dnsmanager.CreateDNSManager(ctx) + dnsManager := dnsmanager.CreateDNSManager() dnsManagerClient = dnsManager // NOTE: dnsResolver is set for threat detection. dnsResolver = dnsManager diff --git a/pkg/dnsmanager/dns_manager.go b/pkg/dnsmanager/dns_manager.go index a8ef3e8a..9234137c 100644 --- a/pkg/dnsmanager/dns_manager.go +++ b/pkg/dnsmanager/dns_manager.go @@ -1,51 +1,39 @@ package dnsmanager import ( - "context" "net" - "sync" "time" "github.com/goradd/maps" tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" + "istio.io/pkg/cache" ) // DNSManager is used to manage DNS events and save IP resolutions. type DNSManager struct { addressToDomainMap maps.SafeMap[string, string] - lookupCache *sync.Map // Cache for DNS lookups - failureCache *sync.Map // Cache for failed lookups to prevent repeated attempts - cleanupTicker *time.Ticker // Ticker for periodic cache cleanup - cancel context.CancelFunc // Cancel function for cleanup goroutine + lookupCache cache.ExpiringCache // Cache for DNS lookups + failureCache cache.ExpiringCache // Cache for failed lookups } type cacheEntry struct { addresses []string - timestamp time.Time } const ( defaultPositiveTTL = 1 * time.Minute // Default TTL for successful lookups defaultNegativeTTL = 5 * time.Second // Default TTL for failed lookups - cleanupInterval = 5 * time.Minute // How often to run cache cleanup ) var _ DNSManagerClient = (*DNSManager)(nil) var _ DNSResolver = (*DNSManager)(nil) -func CreateDNSManager(ctx context.Context) *DNSManager { - ctx, cancel := context.WithCancel(ctx) - dm := &DNSManager{ - lookupCache: &sync.Map{}, - failureCache: &sync.Map{}, - cleanupTicker: time.NewTicker(cleanupInterval), - cancel: cancel, +func CreateDNSManager() *DNSManager { + return &DNSManager{ + // Create TTL caches with their respective expiration times + lookupCache: cache.NewTTL(defaultPositiveTTL, defaultPositiveTTL), + failureCache: cache.NewTTL(defaultNegativeTTL, defaultNegativeTTL), } - - // Start the cleanup goroutine - go dm.cacheCleaner(ctx) - - return dm } func (dm *DNSManager) ReportDNSEvent(dnsEvent tracerdnstype.Event) { @@ -56,39 +44,32 @@ func (dm *DNSManager) ReportDNSEvent(dnsEvent tracerdnstype.Event) { } // Update the cache with these known good addresses - dm.lookupCache.Store(dnsEvent.DNSName, cacheEntry{ + dm.lookupCache.Set(dnsEvent.DNSName, cacheEntry{ addresses: dnsEvent.Addresses, - timestamp: time.Now(), }) return } // Check if we've recently failed to look up this domain - if failedTime, failed := dm.failureCache.Load(dnsEvent.DNSName); failed { - if time.Since(failedTime.(time.Time)) < defaultNegativeTTL { - return - } - // Failed entry has expired, remove it - dm.failureCache.Delete(dnsEvent.DNSName) + if _, found := dm.failureCache.Get(dnsEvent.DNSName); found { + return } // Check if we have a cached result - if cached, ok := dm.lookupCache.Load(dnsEvent.DNSName); ok { + if cached, found := dm.lookupCache.Get(dnsEvent.DNSName); found { entry := cached.(cacheEntry) - if time.Since(entry.timestamp) < defaultPositiveTTL { - // Use cached addresses - for _, addr := range entry.addresses { - dm.addressToDomainMap.Set(addr, dnsEvent.DNSName) - } - return + // Use cached addresses + for _, addr := range entry.addresses { + dm.addressToDomainMap.Set(addr, dnsEvent.DNSName) } + return } // Only perform lookup if we don't have cached results addresses, err := net.LookupIP(dnsEvent.DNSName) if err != nil { - // Cache the failure - dm.failureCache.Store(dnsEvent.DNSName, time.Now()) + // Cache the failure - we just need to store something, using empty struct + dm.failureCache.Set(dnsEvent.DNSName, struct{}{}) return } @@ -101,44 +82,11 @@ func (dm *DNSManager) ReportDNSEvent(dnsEvent tracerdnstype.Event) { } // Cache the successful lookup - dm.lookupCache.Store(dnsEvent.DNSName, cacheEntry{ + dm.lookupCache.Set(dnsEvent.DNSName, cacheEntry{ addresses: addrStrings, - timestamp: time.Now(), }) } -// cacheCleaner runs periodically to clean up expired entries from both caches -func (dm *DNSManager) cacheCleaner(ctx context.Context) { - for { - select { - case <-dm.cleanupTicker.C: - now := time.Now() - - // Clean up positive cache - dm.lookupCache.Range(func(key, value interface{}) bool { - entry := value.(cacheEntry) - if now.Sub(entry.timestamp) > defaultPositiveTTL { - dm.lookupCache.Delete(key) - } - return true - }) - - // Clean up negative cache - dm.failureCache.Range(func(key, value interface{}) bool { - failedTime := value.(time.Time) - if now.Sub(failedTime) > defaultNegativeTTL { - dm.failureCache.Delete(key) - } - return true - }) - - case <-ctx.Done(): - dm.cleanupTicker.Stop() - return - } - } -} - func (dm *DNSManager) ResolveIPAddress(ipAddr string) (string, bool) { domain := dm.addressToDomainMap.Get(ipAddr) return domain, domain != "" diff --git a/pkg/dnsmanager/dns_manager_test.go b/pkg/dnsmanager/dns_manager_test.go index 30a9a218..aabcb01c 100644 --- a/pkg/dnsmanager/dns_manager_test.go +++ b/pkg/dnsmanager/dns_manager_test.go @@ -2,11 +2,8 @@ package dnsmanager import ( "net" - "sync" "testing" - "time" - "github.com/goradd/maps" tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" ) @@ -58,22 +55,13 @@ func TestResolveIPAddress(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Create a properly initialized DNSManager - dm := &DNSManager{ - addressToDomainMap: maps.SafeMap[string, string]{}, - lookupCache: &sync.Map{}, - failureCache: &sync.Map{}, - cleanupTicker: time.NewTicker(cleanupInterval), - } + dm := CreateDNSManager() dm.ReportDNSEvent(tt.dnsEvent) got, ok := dm.ResolveIPAddress(tt.ipAddr) if got != tt.want || ok != tt.wantOk { t.Errorf("ResolveIPAddress() got = %v, ok = %v, want = %v, wantOk = %v", got, ok, tt.want, tt.wantOk) } - - // Cleanup - dm.cleanupTicker.Stop() }) } } @@ -103,13 +91,7 @@ func TestResolveIPAddressFallback(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Create a properly initialized DNSManager - dm := &DNSManager{ - addressToDomainMap: maps.SafeMap[string, string]{}, - lookupCache: &sync.Map{}, - failureCache: &sync.Map{}, - cleanupTicker: time.NewTicker(cleanupInterval), - } + dm := CreateDNSManager() // Perform the actual DNS lookup addresses, err := net.LookupIP(tt.dnsEvent.DNSName) @@ -127,9 +109,59 @@ func TestResolveIPAddressFallback(t *testing.T) { if got != tt.want || ok != tt.wantOk { t.Errorf("ResolveIPAddress() got = %v, ok = %v, want = %v, wantOk = %v", got, ok, tt.want, tt.wantOk) } - - // Cleanup - dm.cleanupTicker.Stop() }) } } + +func TestCacheFallbackBehavior(t *testing.T) { + dm := CreateDNSManager() + + // Test successful DNS lookup caching + event := tracerdnstype.Event{ + DNSName: "test.com", + Addresses: []string{ + "1.2.3.4", + }, + } + dm.ReportDNSEvent(event) + + // Check if the lookup is cached + cached, found := dm.lookupCache.Get(event.DNSName) + if !found { + t.Error("Expected DNS lookup to be cached") + } + + entry, ok := cached.(cacheEntry) + if !ok { + t.Error("Cached entry is not of type cacheEntry") + } + if len(entry.addresses) != 1 || entry.addresses[0] != "1.2.3.4" { + t.Error("Cached addresses do not match expected values") + } + + // Test failed lookup caching + failEvent := tracerdnstype.Event{ + DNSName: "nonexistent.local", + } + dm.ReportDNSEvent(failEvent) + + // Check if the failure is cached + _, found = dm.failureCache.Get(failEvent.DNSName) + if !found { + t.Error("Expected failed DNS lookup to be cached") + } + + // Test cache hit behavior + hitCount := 0 + for i := 0; i < 5; i++ { + if cached, found := dm.lookupCache.Get(event.DNSName); found { + entry := cached.(cacheEntry) + if len(entry.addresses) > 0 { + hitCount++ + } + } + } + if hitCount != 5 { + t.Errorf("Expected 5 cache hits, got %d", hitCount) + } +} From 23da027089a6af48f3be4e01a8dc564d08cea493 Mon Sep 17 00:00:00 2001 From: Amit Schendel Date: Thu, 7 Nov 2024 11:45:54 +0000 Subject: [PATCH 3/4] go mod tidy + test for concurent threads Signed-off-by: Amit Schendel --- go.mod | 2 +- pkg/dnsmanager/dns_manager_test.go | 67 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 08ab17c0..4688889d 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( go.opentelemetry.io/otel v1.30.0 go.opentelemetry.io/otel/trace v1.30.0 go.uber.org/multierr v1.11.0 + golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa golang.org/x/net v0.29.0 golang.org/x/sys v0.25.0 gonum.org/v1/plot v0.14.0 @@ -227,7 +228,6 @@ require ( go.uber.org/zap v1.27.0 // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect golang.org/x/crypto v0.27.0 // indirect - golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect golang.org/x/image v0.18.0 // indirect golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.8.0 // indirect diff --git a/pkg/dnsmanager/dns_manager_test.go b/pkg/dnsmanager/dns_manager_test.go index aabcb01c..b9a0702b 100644 --- a/pkg/dnsmanager/dns_manager_test.go +++ b/pkg/dnsmanager/dns_manager_test.go @@ -2,9 +2,11 @@ package dnsmanager import ( "net" + "sync" "testing" tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" + "golang.org/x/exp/rand" ) func TestResolveIPAddress(t *testing.T) { @@ -165,3 +167,68 @@ func TestCacheFallbackBehavior(t *testing.T) { t.Errorf("Expected 5 cache hits, got %d", hitCount) } } + +func TestConcurrentAccess(t *testing.T) { + dm := CreateDNSManager() + const numGoroutines = 100 + const numOperations = 1000 + + // Create a wait group to synchronize goroutines + var wg sync.WaitGroup + wg.Add(numGoroutines) + + // Create some test data + testEvents := []tracerdnstype.Event{ + { + DNSName: "test1.com", + Addresses: []string{"1.1.1.1", "2.2.2.2"}, + }, + { + DNSName: "test2.com", + Addresses: []string{"3.3.3.3", "4.4.4.4"}, + }, + { + DNSName: "test3.com", + Addresses: []string{"5.5.5.5", "6.6.6.6"}, + }, + } + + // Launch multiple goroutines to concurrently access the cache + for i := 0; i < numGoroutines; i++ { + go func() { + defer wg.Done() + + for j := 0; j < numOperations; j++ { + // Randomly choose between writing and reading + if rand.Float32() < 0.5 { + // Write operation + event := testEvents[rand.Intn(len(testEvents))] + dm.ReportDNSEvent(event) + } else { + // Read operation + if cached, found := dm.lookupCache.Get("test1.com"); found { + entry := cached.(cacheEntry) + // Verify the slice hasn't been modified + if len(entry.addresses) != 2 { + t.Errorf("Unexpected number of addresses: %d", len(entry.addresses)) + } + } + } + } + }() + } + + // Wait for all goroutines to complete + wg.Wait() + + // Verify final state + for _, event := range testEvents { + if cached, found := dm.lookupCache.Get(event.DNSName); found { + entry := cached.(cacheEntry) + if len(entry.addresses) != len(event.Addresses) { + t.Errorf("Cache entry for %s has wrong number of addresses: got %d, want %d", + event.DNSName, len(entry.addresses), len(event.Addresses)) + } + } + } +} From f15c5e9e423064092771c7d2e6d7f55f30a315d0 Mon Sep 17 00:00:00 2001 From: Amit Schendel Date: Thu, 7 Nov 2024 12:07:44 +0000 Subject: [PATCH 4/4] Improving test to use math/rand/v2 Signed-off-by: Amit Schendel --- go.mod | 2 +- pkg/dnsmanager/dns_manager_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 4688889d..08ab17c0 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,6 @@ require ( go.opentelemetry.io/otel v1.30.0 go.opentelemetry.io/otel/trace v1.30.0 go.uber.org/multierr v1.11.0 - golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa golang.org/x/net v0.29.0 golang.org/x/sys v0.25.0 gonum.org/v1/plot v0.14.0 @@ -228,6 +227,7 @@ require ( go.uber.org/zap v1.27.0 // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect golang.org/x/crypto v0.27.0 // indirect + golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect golang.org/x/image v0.18.0 // indirect golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.8.0 // indirect diff --git a/pkg/dnsmanager/dns_manager_test.go b/pkg/dnsmanager/dns_manager_test.go index b9a0702b..8fd6e173 100644 --- a/pkg/dnsmanager/dns_manager_test.go +++ b/pkg/dnsmanager/dns_manager_test.go @@ -5,8 +5,9 @@ import ( "sync" "testing" + "math/rand/v2" + tracerdnstype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types" - "golang.org/x/exp/rand" ) func TestResolveIPAddress(t *testing.T) { @@ -202,7 +203,7 @@ func TestConcurrentAccess(t *testing.T) { // Randomly choose between writing and reading if rand.Float32() < 0.5 { // Write operation - event := testEvents[rand.Intn(len(testEvents))] + event := testEvents[rand.IntN(len(testEvents))] dm.ReportDNSEvent(event) } else { // Read operation