From 1a549ec047c63ac5bc1d343358c902fbc84de4dd Mon Sep 17 00:00:00 2001 From: Roman Melnyk Date: Wed, 16 Aug 2023 14:37:47 +0300 Subject: [PATCH] Move ip2domain resolving to exporter (#50) --- cmd/collector/main.go | 4 +- cmd/exporter/main.go | 2 +- collector/collector.go | 25 ++-- collector/collector_test.go | 31 +++-- dns/noop.go | 9 +- dns/resolutions.go | 78 ++++++------ dns/resolutions_test.go | 28 ++--- exporter/exporter.go | 9 +- exporter/exporter_test.go | 53 ++++++-- exporter/ip2dns.go | 42 +++++++ exporter/ip2dns_test.go | 52 ++++++++ pb/metrics.pb.go | 235 ++++++++++++++++++++++-------------- pb/metrics.proto | 10 +- 13 files changed, 387 insertions(+), 191 deletions(-) create mode 100644 exporter/ip2dns.go create mode 100644 exporter/ip2dns_test.go diff --git a/cmd/collector/main.go b/cmd/collector/main.go index f63494c..7fa61ac 100644 --- a/cmd/collector/main.go +++ b/cmd/collector/main.go @@ -107,12 +107,12 @@ func run(log logrus.FieldLogger) error { return err } - var ip2dns dns.LookuperStarter = &dns.Noop{} + var ip2dns dns.DNSCollector = &dns.Noop{} if *ebpfDNSTracerEnabled && ebpf.IsKernelBTFAvailable() { tracer := ebpf.NewTracer(log, ebpf.Config{ QueueSize: *ebpfDNSTracerQueueSize, }) - ip2dns = &dns.IP2DNS{Tracer: tracer} + ip2dns = dns.NewIP2DNS(tracer, log) } cfg := collector.Config{ diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index 6a103c6..3385b5a 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -139,7 +139,7 @@ func run(log logrus.FieldLogger) error { return errors.New("not sinks configured") } - ex := exporter.New(log, cfg, kw, clientset, sinksList) + ex := exporter.New(ctx, log, cfg, kw, clientset, sinksList) go func() { if err := ex.Start(ctx); err != nil && !errors.Is(err, context.Canceled) { log.Errorf("exporter start: %v", err) diff --git a/collector/collector.go b/collector/collector.go index 2797a47..b32d168 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -17,6 +17,7 @@ import ( corev1 "k8s.io/api/core/v1" "github.com/castai/egressd/conntrack" + "github.com/castai/egressd/dns" "github.com/castai/egressd/metrics" "github.com/castai/egressd/pb" ) @@ -52,14 +53,14 @@ type rawNetworkMetric struct { lifetime time.Time } -type ipLookup interface{ Lookup(netaddr.IP) string } +type dnsRecorder interface{ Records() []*pb.IP2Domain } func New( cfg Config, log logrus.FieldLogger, podsWatcher podsWatcher, conntracker conntrack.Client, - ip2dns ipLookup, + ip2dns dnsRecorder, currentTimeGetter func() time.Time, ) *Collector { excludeNsMap := map[string]struct{}{} @@ -95,7 +96,7 @@ type Collector struct { log logrus.FieldLogger podsWatcher podsWatcher conntracker conntrack.Client - ip2dns ipLookup + ip2dns dnsRecorder entriesCache map[uint64]*conntrack.Entry podMetrics map[uint64]*rawNetworkMetric excludeNsMap map[string]struct{} @@ -131,7 +132,7 @@ func (c *Collector) GetRawNetworkMetricsHandler(w http.ResponseWriter, req *http items = append(items, m.RawNetworkMetric) } - batch := &pb.RawNetworkMetricBatch{Items: items} + batch := &pb.RawNetworkMetricBatch{Items: items, Ip2Domain: c.ip2dns.Records()} batchBytes, err := proto.Marshal(batch) if err != nil { c.log.Errorf("marshal batch: %v", err) @@ -190,12 +191,7 @@ func (c *Collector) collect() error { c.entriesCache[connKey] = conn groupKey := entryGroupKey(conn) - srcName := c.ip2dns.Lookup(conn.Src.IP()) - dstName := c.ip2dns.Lookup(conn.Dst.IP()) if pm, found := c.podMetrics[groupKey]; found { - pm.SrcDnsName = srcName - pm.DstDnsName = dstName - pm.TxBytes += int64(txBytes) pm.TxPackets += int64(txPackets) pm.RxBytes += int64(rxBytes) @@ -206,10 +202,8 @@ func (c *Collector) collect() error { } else { c.podMetrics[groupKey] = &rawNetworkMetric{ RawNetworkMetric: &pb.RawNetworkMetric{ - SrcDnsName: srcName, - DstDnsName: dstName, - SrcIp: toIPint32(conn.Src.IP()), - DstIp: toIPint32(conn.Dst.IP()), + SrcIp: dns.ToIPint32(conn.Src.IP()), + DstIp: dns.ToIPint32(conn.Dst.IP()), TxBytes: int64(conn.TxBytes), TxPackets: int64(conn.TxPackets), RxBytes: int64(conn.RxBytes), @@ -317,8 +311,3 @@ func conntrackEntryKey(conn *conntrack.Entry) uint64 { conntrackEntryHash.Reset() return res } - -func toIPint32(ip netaddr.IP) int32 { - b := ip.As4() - return int32(binary.BigEndian.Uint32([]byte{b[0], b[1], b[2], b[3]})) -} diff --git a/collector/collector_test.go b/collector/collector_test.go index 2334ade..22ad8ad 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -17,6 +17,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/castai/egressd/conntrack" + "github.com/castai/egressd/dns" "github.com/castai/egressd/pb" ) @@ -358,7 +359,7 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { }, } - newCollector := func(connTracker conntrack.Client, ip2dns ipLookup) *Collector { + newCollector := func(connTracker conntrack.Client, ip2dns dnsRecorder) *Collector { return New(Config{ ReadInterval: time.Millisecond, CleanupInterval: 3 * time.Millisecond, @@ -389,7 +390,7 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { connTracker := &mockConntrack{entries: initialEntries} dstIp := initialEntries[0].Dst.IP() - ip2dns := mockIP2DNS{dstIp: "first-destination.example.com"} + ip2dns := mockIP2DNS{dns.ToIPint32(dstIp): "first-destination.example.com"} coll := newCollector(connTracker, ip2dns) coll.cfg.SendTrafficDelta = false @@ -400,12 +401,10 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { key1 := entryGroupKey(&initialEntries[0]) r.EqualValues(20, coll.podMetrics[key1].TxBytes) r.EqualValues(3, coll.podMetrics[key1].TxPackets) - r.Equal("first-destination.example.com", coll.podMetrics[key1].DstDnsName) key2 := entryGroupKey(&initialEntries[1]) r.EqualValues(10, coll.podMetrics[key2].RxBytes) r.EqualValues(2, coll.podMetrics[key2].RxPackets) - r.Equal("", coll.podMetrics[key2].DstDnsName) initialEntries[0].TxBytes += 10 initialEntries[0].TxPackets += 2 @@ -429,6 +428,10 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { r.NoError(err) r.Len(batch.Items, 2) + r.Len(batch.Ip2Domain, 1) + r.Equal("first-destination.example.com", batch.Ip2Domain[0].Domain) + r.Equal(dns.ToIPint32(dstIp), batch.Ip2Domain[0].Ip) + // Check values are the same as on the last collecting action r.EqualValues(30, batch.Items[0].TxBytes) r.EqualValues(5, batch.Items[0].TxPackets) @@ -471,7 +474,7 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { connTracker := &mockConntrack{entries: initialEntries} dstIp := initialEntries[0].Dst.IP() - ip2dns := mockIP2DNS{dstIp: "first-destination.example.com"} + ip2dns := mockIP2DNS{dns.ToIPint32(dstIp): "first-destination.example.com"} coll := newCollector(connTracker, ip2dns) coll.cfg.SendTrafficDelta = true @@ -482,12 +485,10 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { key1 := entryGroupKey(&initialEntries[0]) r.EqualValues(20, coll.podMetrics[key1].TxBytes) r.EqualValues(3, coll.podMetrics[key1].TxPackets) - r.Equal("first-destination.example.com", coll.podMetrics[key1].DstDnsName) key2 := entryGroupKey(&initialEntries[1]) r.EqualValues(10, coll.podMetrics[key2].RxBytes) r.EqualValues(2, coll.podMetrics[key2].RxPackets) - r.Equal("", coll.podMetrics[key2].DstDnsName) initialEntries[0].TxBytes += 10 initialEntries[0].TxPackets += 2 @@ -511,6 +512,10 @@ func TestCollector__GetRawNetworkMetricsHandler(t *testing.T) { r.NoError(err) r.Len(batch.Items, 2) + r.Len(batch.Ip2Domain, 1) + r.Equal("first-destination.example.com", batch.Ip2Domain[0].Domain) + r.Equal(dns.ToIPint32(dstIp), batch.Ip2Domain[0].Ip) + // Check values are the same as on the last collecting action r.EqualValues(30, batch.Items[0].TxBytes) r.EqualValues(5, batch.Items[0].TxPackets) @@ -636,10 +641,14 @@ func (m *mockKubeWatcher) Get(nodeName string) ([]*corev1.Pod, error) { return res, nil } -type mockIP2DNS map[netaddr.IP]string +type mockIP2DNS map[int32]string -var _ ipLookup = (mockIP2DNS)(nil) +var _ dnsRecorder = (mockIP2DNS)(nil) -func (m mockIP2DNS) Lookup(ip netaddr.IP) string { - return m[ip] +func (m mockIP2DNS) Records() []*pb.IP2Domain { + items := make([]*pb.IP2Domain, 0, len(m)) + for ip, domain := range m { + items = append(items, &pb.IP2Domain{Ip: ip, Domain: domain}) + } + return items } diff --git a/dns/noop.go b/dns/noop.go index adba833..985319c 100644 --- a/dns/noop.go +++ b/dns/noop.go @@ -3,17 +3,18 @@ package dns import ( "context" - "inet.af/netaddr" + "github.com/castai/egressd/pb" ) type Noop struct{} -var _ LookuperStarter = (*Noop)(nil) +var _ DNSCollector = (*Noop)(nil) func (t *Noop) Start(ctx context.Context) error { <-ctx.Done() return nil } -func (t *Noop) Lookup(ip netaddr.IP) string { - return "" + +func (d *Noop) Records() []*pb.IP2Domain { + return nil } diff --git a/dns/resolutions.go b/dns/resolutions.go index 3a9bfdb..16360c2 100644 --- a/dns/resolutions.go +++ b/dns/resolutions.go @@ -2,41 +2,50 @@ package dns import ( "context" + "encoding/binary" "fmt" - "sync" "time" cache "github.com/Code-Hex/go-generics-cache" "github.com/google/gopacket/layers" + "github.com/sirupsen/logrus" "inet.af/netaddr" "github.com/castai/egressd/ebpf" + "github.com/castai/egressd/pb" ) -type LookuperStarter interface { +type DNSCollector interface { Start(ctx context.Context) error - Lookup(ip netaddr.IP) string + Records() []*pb.IP2Domain } -var _ LookuperStarter = (*IP2DNS)(nil) +var _ DNSCollector = (*IP2DNS)(nil) type tracer interface { Run(ctx context.Context) error Events() <-chan ebpf.DNSEvent } -var defaultDNSTTL = 5 * time.Minute +var defaultDNSTTL = 2 * time.Minute type IP2DNS struct { Tracer tracer - ipToName *cache.Cache[string, string] + log logrus.FieldLogger + ipToName *cache.Cache[int32, string] cnameToName *cache.Cache[string, string] - mu sync.RWMutex +} + +func NewIP2DNS(tracer tracer, log logrus.FieldLogger) *IP2DNS { + return &IP2DNS{ + Tracer: tracer, + log: log, + } } func (d *IP2DNS) Start(ctx context.Context) error { - d.ipToName = cache.NewContext[string, string](ctx) + d.ipToName = cache.NewContext[int32, string](ctx) d.cnameToName = cache.NewContext[string, string](ctx) ctx, cancel := context.WithCancel(ctx) @@ -61,37 +70,36 @@ func (d *IP2DNS) Start(ctx context.Context) error { if !ok { return nil } - func() { - d.mu.Lock() - defer d.mu.Unlock() - for _, answer := range ev.Answers { - name := string(answer.Name) - ttl := time.Duration(answer.TTL) * time.Second - if ttl == 0 { - ttl = defaultDNSTTL - } - switch answer.Type { //nolint:exhaustive - case layers.DNSTypeA: - if cname, found := d.cnameToName.Get(name); found { - name = cname - } - ip := answer.IP.To4() - d.ipToName.Set(ip.String(), name, cache.WithExpiration(ttl)) - case layers.DNSTypeCNAME: - cname := string(answer.CNAME) - d.cnameToName.Set(cname, name, cache.WithExpiration(ttl)) - default: - continue + for _, answer := range ev.Answers { + name := string(answer.Name) + switch answer.Type { //nolint:exhaustive + case layers.DNSTypeA: + if cname, found := d.cnameToName.Get(name); found { + name = cname } + ip, _ := netaddr.FromStdIP(answer.IP) + d.ipToName.Set(ToIPint32(ip), name, cache.WithExpiration(defaultDNSTTL)) + case layers.DNSTypeCNAME: + cname := string(answer.CNAME) + d.cnameToName.Set(cname, name, cache.WithExpiration(defaultDNSTTL)) + default: + continue } - }() + } } } } -func (d *IP2DNS) Lookup(ip netaddr.IP) string { - d.mu.RLock() - defer d.mu.RUnlock() - value, _ := d.ipToName.Get(ip.String()) - return value +func (d *IP2DNS) Records() []*pb.IP2Domain { + items := make([]*pb.IP2Domain, 0, len(d.ipToName.Keys())) + for _, ip := range d.ipToName.Keys() { + domain, _ := d.ipToName.Get(ip) + items = append(items, &pb.IP2Domain{Ip: ip, Domain: domain}) + } + return items +} + +func ToIPint32(ip netaddr.IP) int32 { + b := ip.As4() + return int32(binary.BigEndian.Uint32([]byte{b[0], b[1], b[2], b[3]})) } diff --git a/dns/resolutions_test.go b/dns/resolutions_test.go index 37d16a3..bd30f22 100644 --- a/dns/resolutions_test.go +++ b/dns/resolutions_test.go @@ -10,9 +10,10 @@ import ( "inet.af/netaddr" "github.com/castai/egressd/ebpf" + "github.com/castai/egressd/pb" ) -func TestIP2DNS_Lookup(t *testing.T) { +func TestIP2DNS_Records(t *testing.T) { type fields struct { Tracer tracer } @@ -20,7 +21,7 @@ func TestIP2DNS_Lookup(t *testing.T) { tests := []struct { name string fields fields - want map[string]string + want []*pb.IP2Domain }{ { name: "resolves simple A record", @@ -46,11 +47,10 @@ func TestIP2DNS_Lookup(t *testing.T) { }, }, })}, - want: map[string]string{ - "1.2.3.4": "example.com", - "1.2.3.5": "hi.example.com", - "1.2.3.6": "ehlo.example.com", - "4.3.2.1": "", + want: []*pb.IP2Domain{ + {Ip: ToIPint32(netaddr.IPv4(1,2,3,4)), Domain: "example.com"}, + {Ip: ToIPint32(netaddr.IPv4(1,2,3,5)), Domain: "hi.example.com"}, + {Ip: ToIPint32(netaddr.IPv4(1,2,3,6)), Domain: "ehlo.example.com"}, }, }, @@ -73,9 +73,8 @@ func TestIP2DNS_Lookup(t *testing.T) { }, }, })}, - want: map[string]string{ - "1.2.3.4": "example.com", - "4.3.2.1": "", + want: []*pb.IP2Domain{ + {Ip: ToIPint32(netaddr.IPv4(1,2,3,4)), Domain: "example.com"}, }, }, } @@ -87,12 +86,9 @@ func TestIP2DNS_Lookup(t *testing.T) { ip2dns := &IP2DNS{ Tracer: tt.fields.Tracer, } - _ = ip2dns.Start(ctx) - for arg, want := range tt.want { - ip := netaddr.MustParseIP(arg) - got := ip2dns.Lookup(ip) - require.Equal(t, want, got, "Lookup(%s)", ip) - } + err := ip2dns.Start(ctx) + require.NoError(t, err) + require.Equal(t, tt.want, ip2dns.Records()) }) } } diff --git a/exporter/exporter.go b/exporter/exporter.go index bbdb5c3..7f8be16 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -19,6 +19,7 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/kubernetes" + "github.com/castai/egressd/dns" "github.com/castai/egressd/exporter/config" "github.com/castai/egressd/exporter/sinks" "github.com/castai/egressd/kube" @@ -31,6 +32,7 @@ const ( ) func New( + ctx context.Context, log logrus.FieldLogger, cfg config.Config, kubeWatcher kubeWatcher, @@ -44,6 +46,7 @@ func New( kubeClient: kubeClient, httpClient: newHTTPClient(), sinks: sinks, + dnsStorage: newDnsStorage(ctx, log), } } @@ -54,6 +57,7 @@ type Exporter struct { kubeClient kubernetes.Interface httpClient *http.Client sinks []sinks.Sink + dnsStorage *dnsStorage } func (e *Exporter) Start(ctx context.Context) error { @@ -147,6 +151,7 @@ func (e *Exporter) export(ctx context.Context) error { // Aggregate raw metrics into pod metrics. var podsMetrics []*pb.PodNetworkMetric for batch := range pulledBatch { + e.dnsStorage.Fill(batch.Ip2Domain) for _, rawMetrics := range batch.Items { podMetrics, err := e.buildPodNetworkMetric(rawMetrics) if err != nil { @@ -190,12 +195,10 @@ func (e *Exporter) buildPodNetworkMetric(conn *pb.RawNetworkMetric) (*pb.PodNetw dstIP := ipFromInt32(conn.DstIp) metric := pb.PodNetworkMetric{ SrcIp: srcIP.String(), - SrcDnsName: conn.SrcDnsName, SrcPod: pod.Name, SrcNamespace: pod.Namespace, SrcNode: pod.Spec.NodeName, DstIp: dstIP.String(), - DstDnsName: conn.DstDnsName, TxBytes: conn.TxBytes, TxPackets: conn.TxPackets, RxBytes: conn.RxBytes, @@ -243,6 +246,8 @@ func (e *Exporter) buildPodNetworkMetric(conn *pb.RawNetworkMetric) (*pb.PodNetw metric.DstZone = getNodeZone(dstNode) } } + } else { + metric.DstDnsName = e.dnsStorage.Lookup(dns.ToIPint32(dstIP)) } return &metric, nil } diff --git a/exporter/exporter_test.go b/exporter/exporter_test.go index f3b088f..d644a16 100644 --- a/exporter/exporter_test.go +++ b/exporter/exporter_test.go @@ -2,6 +2,7 @@ package exporter import ( "context" + "encoding/binary" "errors" "net" "net/http" @@ -14,10 +15,12 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" + "inet.af/netaddr" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" + "github.com/castai/egressd/dns" "github.com/castai/egressd/exporter/config" "github.com/castai/egressd/exporter/sinks" "github.com/castai/egressd/kube" @@ -29,6 +32,9 @@ func TestExporter(t *testing.T) { ctx := context.Background() log := logrus.New() log.SetLevel(logrus.DebugLevel) + pod1Ip := netaddr.IPv4(10,14,7,12) + pod2Ip := netaddr.IPv4(10,14,7,5) + publicIp := netaddr.IPv4(192,0,0,1) kubeWatcher := &mockKubeWatcher{ pods: []*corev1.Pod{ @@ -41,7 +47,7 @@ func TestExporter(t *testing.T) { NodeName: "n1", }, Status: corev1.PodStatus{ - PodIP: "10.14.7.12", + PodIP: pod1Ip.String(), }, }, { @@ -53,7 +59,7 @@ func TestExporter(t *testing.T) { NodeName: "n1", }, Status: corev1.PodStatus{ - PodIP: "10.14.7.5", + PodIP: pod2Ip.String(), }, }, }, @@ -74,14 +80,26 @@ func TestExporter(t *testing.T) { batch := &pb.RawNetworkMetricBatch{ Items: []*pb.RawNetworkMetric{ { - SrcIp: 168691468, - DstIp: 168691461, + SrcIp: toIPint32(pod1Ip), + DstIp: toIPint32(pod2Ip), TxBytes: 35, TxPackets: 3, RxBytes: 30, RxPackets: 1, Proto: 6, }, + { + SrcIp: toIPint32(pod1Ip), + DstIp: toIPint32(publicIp), + TxBytes: 3, + TxPackets: 1, + RxBytes: 5, + RxPackets: 2, + Proto: 6, + }, + }, + Ip2Domain: []*pb.IP2Domain{ + {Ip: dns.ToIPint32(publicIp), Domain: "example.com"}, }, } batchBytes, err := proto.Marshal(batch) @@ -134,7 +152,7 @@ func TestExporter(t *testing.T) { }, }, } - ex := New(log, cfg, kubeWatcher, kubeClient, []sinks.Sink{sink}) + ex := New(ctx, log, cfg, kubeWatcher, kubeClient, []sinks.Sink{sink}) ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -153,8 +171,7 @@ func TestExporter(t *testing.T) { t.Fatal("timeout waiting for sink push") } - r.Len(pushedBatch.Items, 1) - item := pushedBatch.Items[0] + r.Len(pushedBatch.Items, 2) r.Equal(&pb.PodNetworkMetric{ SrcIp: "10.14.7.12", SrcPod: "p1", @@ -166,12 +183,27 @@ func TestExporter(t *testing.T) { DstNamespace: "team2", DstNode: "n1", DstZone: "us-east-1a", + DstDnsName: "", TxBytes: 35, TxPackets: 3, RxBytes: 30, RxPackets: 1, Proto: 6, - }, item) + }, pushedBatch.Items[0]) + r.Equal(&pb.PodNetworkMetric{ + SrcIp: pod1Ip.String(), + SrcPod: "p1", + SrcNamespace: "team1", + SrcNode: "n1", + SrcZone: "us-east-1a", + DstIp: publicIp.String(), + DstDnsName: "example.com", + TxBytes: 3, + TxPackets: 1, + RxBytes: 5, + RxPackets: 2, + Proto: 6, + }, pushedBatch.Items[1]) } type mockKubeWatcher struct { @@ -226,3 +258,8 @@ func (m *mockSink) Push(ctx context.Context, batch *pb.PodNetworkMetricBatch) er m.batch <- batch return nil } + +func toIPint32(ip netaddr.IP) int32 { + b := ip.As4() + return int32(binary.BigEndian.Uint32([]byte{b[0], b[1], b[2], b[3]})) +} diff --git a/exporter/ip2dns.go b/exporter/ip2dns.go new file mode 100644 index 0000000..8e40bca --- /dev/null +++ b/exporter/ip2dns.go @@ -0,0 +1,42 @@ +package exporter + +import ( + "context" + "time" + + cache "github.com/Code-Hex/go-generics-cache" + "github.com/sirupsen/logrus" + + "github.com/castai/egressd/pb" +) + +var defaultDNSTTL = 1 * time.Hour + +type dnsStorage struct { + log logrus.FieldLogger + ipToName *cache.Cache[int32, string] +} + +func newDnsStorage(ctx context.Context, log logrus.FieldLogger) *dnsStorage { + return &dnsStorage{ + log: log, + ipToName: cache.NewContext[int32, string](ctx), + } +} + +func (d *dnsStorage) Fill(items []*pb.IP2Domain) { + for i := range items { + d.ipToName.Set(items[i].Ip, items[i].Domain, cache.WithExpiration(defaultDNSTTL)) + } +} + +func (d *dnsStorage) Lookup(ip int32) string { + if ip == 0 { + return "" + } + value, ok := d.ipToName.Get(ip) + if !ok { + d.log.Debugf("domain not found for IP %q", ipFromInt32(ip)) + } + return value +} diff --git a/exporter/ip2dns_test.go b/exporter/ip2dns_test.go new file mode 100644 index 0000000..5bf8337 --- /dev/null +++ b/exporter/ip2dns_test.go @@ -0,0 +1,52 @@ +package exporter + +import ( + "context" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" + "inet.af/netaddr" + + "github.com/castai/egressd/dns" + "github.com/castai/egressd/pb" +) + +func TestIP2DNS_Lookup(t *testing.T) { + log := logrus.New() + + fields := []*pb.IP2Domain{ + {Ip: dns.ToIPint32(netaddr.IPv4(1, 2, 3, 4)), Domain: "example.com"}, + {Ip: dns.ToIPint32(netaddr.IPv4(1, 2, 3, 5)), Domain: "hi.example.com"}, + {Ip: dns.ToIPint32(netaddr.IPv4(1, 2, 3, 6)), Domain: "ehlo.example.com"}, + } + tests := []struct { + name string + wantIp string + wantDomain string + }{ + { + name: "lookup existing IP", + wantIp: "1.2.3.4", + wantDomain: "example.com", + }, + { + name: "lookup not existing IP", + wantIp: "4.3.2.1", + wantDomain: "", + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + dnsStorage := newDnsStorage(ctx, log) + dnsStorage.Fill(fields) + + ip := netaddr.MustParseIP(tt.wantIp) + got := dnsStorage.Lookup(dns.ToIPint32(ip)) + require.Equal(t, tt.wantDomain, got, "Lookup(%s)", ip) + }) + } +} diff --git a/pb/metrics.pb.go b/pb/metrics.pb.go index e28ddf4..a85e454 100644 --- a/pb/metrics.pb.go +++ b/pb/metrics.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.30.0 -// protoc v4.23.3 +// protoc-gen-go v1.28.1 +// protoc v4.23.4 // source: pb/metrics.proto package pb @@ -25,15 +25,13 @@ type RawNetworkMetric struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - SrcIp int32 `protobuf:"varint,1,opt,name=src_ip,json=srcIp,proto3" json:"src_ip,omitempty"` - DstIp int32 `protobuf:"varint,2,opt,name=dst_ip,json=dstIp,proto3" json:"dst_ip,omitempty"` - TxBytes int64 `protobuf:"varint,3,opt,name=tx_bytes,json=txBytes,proto3" json:"tx_bytes,omitempty"` - TxPackets int64 `protobuf:"varint,4,opt,name=tx_packets,json=txPackets,proto3" json:"tx_packets,omitempty"` - RxBytes int64 `protobuf:"varint,5,opt,name=rx_bytes,json=rxBytes,proto3" json:"rx_bytes,omitempty"` - RxPackets int64 `protobuf:"varint,6,opt,name=rx_packets,json=rxPackets,proto3" json:"rx_packets,omitempty"` - Proto int32 `protobuf:"varint,7,opt,name=proto,proto3" json:"proto,omitempty"` - SrcDnsName string `protobuf:"bytes,8,opt,name=src_dns_name,json=srcDnsName,proto3" json:"src_dns_name,omitempty"` - DstDnsName string `protobuf:"bytes,9,opt,name=dst_dns_name,json=dstDnsName,proto3" json:"dst_dns_name,omitempty"` + SrcIp int32 `protobuf:"varint,1,opt,name=src_ip,json=srcIp,proto3" json:"src_ip,omitempty"` + DstIp int32 `protobuf:"varint,2,opt,name=dst_ip,json=dstIp,proto3" json:"dst_ip,omitempty"` + TxBytes int64 `protobuf:"varint,3,opt,name=tx_bytes,json=txBytes,proto3" json:"tx_bytes,omitempty"` + TxPackets int64 `protobuf:"varint,4,opt,name=tx_packets,json=txPackets,proto3" json:"tx_packets,omitempty"` + RxBytes int64 `protobuf:"varint,5,opt,name=rx_bytes,json=rxBytes,proto3" json:"rx_bytes,omitempty"` + RxPackets int64 `protobuf:"varint,6,opt,name=rx_packets,json=rxPackets,proto3" json:"rx_packets,omitempty"` + Proto int32 `protobuf:"varint,7,opt,name=proto,proto3" json:"proto,omitempty"` } func (x *RawNetworkMetric) Reset() { @@ -117,26 +115,13 @@ func (x *RawNetworkMetric) GetProto() int32 { return 0 } -func (x *RawNetworkMetric) GetSrcDnsName() string { - if x != nil { - return x.SrcDnsName - } - return "" -} - -func (x *RawNetworkMetric) GetDstDnsName() string { - if x != nil { - return x.DstDnsName - } - return "" -} - type RawNetworkMetricBatch struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Items []*RawNetworkMetric `protobuf:"bytes,1,rep,name=items,proto3" json:"items,omitempty"` + Items []*RawNetworkMetric `protobuf:"bytes,1,rep,name=items,proto3" json:"items,omitempty"` + Ip2Domain []*IP2Domain `protobuf:"bytes,2,rep,name=ip2domain,proto3" json:"ip2domain,omitempty"` } func (x *RawNetworkMetricBatch) Reset() { @@ -178,6 +163,13 @@ func (x *RawNetworkMetricBatch) GetItems() []*RawNetworkMetric { return nil } +func (x *RawNetworkMetricBatch) GetIp2Domain() []*IP2Domain { + if x != nil { + return x.Ip2Domain + } + return nil +} + type PodNetworkMetric struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -198,7 +190,6 @@ type PodNetworkMetric struct { RxBytes int64 `protobuf:"varint,13,opt,name=rx_bytes,json=rxBytes,proto3" json:"rx_bytes,omitempty"` RxPackets int64 `protobuf:"varint,14,opt,name=rx_packets,json=rxPackets,proto3" json:"rx_packets,omitempty"` Proto int32 `protobuf:"varint,15,opt,name=proto,proto3" json:"proto,omitempty"` - SrcDnsName string `protobuf:"bytes,16,opt,name=src_dns_name,json=srcDnsName,proto3" json:"src_dns_name,omitempty"` DstDnsName string `protobuf:"bytes,17,opt,name=dst_dns_name,json=dstDnsName,proto3" json:"dst_dns_name,omitempty"` } @@ -339,13 +330,6 @@ func (x *PodNetworkMetric) GetProto() int32 { return 0 } -func (x *PodNetworkMetric) GetSrcDnsName() string { - if x != nil { - return x.SrcDnsName - } - return "" -} - func (x *PodNetworkMetric) GetDstDnsName() string { if x != nil { return x.DstDnsName @@ -400,11 +384,66 @@ func (x *PodNetworkMetricBatch) GetItems() []*PodNetworkMetric { return nil } +type IP2Domain struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ip int32 `protobuf:"varint,1,opt,name=ip,proto3" json:"ip,omitempty"` + Domain string `protobuf:"bytes,2,opt,name=domain,proto3" json:"domain,omitempty"` +} + +func (x *IP2Domain) Reset() { + *x = IP2Domain{} + if protoimpl.UnsafeEnabled { + mi := &file_pb_metrics_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IP2Domain) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IP2Domain) ProtoMessage() {} + +func (x *IP2Domain) ProtoReflect() protoreflect.Message { + mi := &file_pb_metrics_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IP2Domain.ProtoReflect.Descriptor instead. +func (*IP2Domain) Descriptor() ([]byte, []int) { + return file_pb_metrics_proto_rawDescGZIP(), []int{4} +} + +func (x *IP2Domain) GetIp() int32 { + if x != nil { + return x.Ip + } + return 0 +} + +func (x *IP2Domain) GetDomain() string { + if x != nil { + return x.Domain + } + return "" +} + var File_pb_metrics_proto protoreflect.FileDescriptor var file_pb_metrics_proto_rawDesc = []byte{ 0x0a, 0x10, 0x70, 0x62, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x22, 0x8e, 0x02, 0x0a, 0x10, 0x52, 0x61, 0x77, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, + 0x74, 0x6f, 0x22, 0xca, 0x01, 0x0a, 0x10, 0x52, 0x61, 0x77, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x72, 0x63, 0x5f, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x72, 0x63, 0x49, 0x70, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x73, 0x74, 0x5f, 0x69, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, @@ -416,54 +455,54 @@ var file_pb_metrics_proto_rawDesc = []byte{ 0x03, 0x52, 0x07, 0x72, 0x78, 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x72, 0x78, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x72, 0x78, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x18, 0x07, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, - 0x20, 0x0a, 0x0c, 0x73, 0x72, 0x63, 0x5f, 0x64, 0x6e, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, - 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x72, 0x63, 0x44, 0x6e, 0x73, 0x4e, 0x61, 0x6d, - 0x65, 0x12, 0x20, 0x0a, 0x0c, 0x64, 0x73, 0x74, 0x5f, 0x64, 0x6e, 0x73, 0x5f, 0x6e, 0x61, 0x6d, - 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x64, 0x73, 0x74, 0x44, 0x6e, 0x73, 0x4e, - 0x61, 0x6d, 0x65, 0x22, 0x40, 0x0a, 0x15, 0x52, 0x61, 0x77, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, - 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x42, 0x61, 0x74, 0x63, 0x68, 0x12, 0x27, 0x0a, 0x05, - 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x52, 0x61, - 0x77, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52, 0x05, - 0x69, 0x74, 0x65, 0x6d, 0x73, 0x22, 0xf6, 0x03, 0x0a, 0x10, 0x50, 0x6f, 0x64, 0x4e, 0x65, 0x74, - 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x12, 0x15, 0x0a, 0x06, 0x73, 0x72, - 0x63, 0x5f, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x72, 0x63, 0x49, - 0x70, 0x12, 0x17, 0x0a, 0x07, 0x73, 0x72, 0x63, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x06, 0x73, 0x72, 0x63, 0x50, 0x6f, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x73, 0x72, - 0x63, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x0c, 0x73, 0x72, 0x63, 0x4e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, - 0x19, 0x0a, 0x08, 0x73, 0x72, 0x63, 0x5f, 0x6e, 0x6f, 0x64, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x07, 0x73, 0x72, 0x63, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x72, - 0x63, 0x5f, 0x7a, 0x6f, 0x6e, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x72, - 0x63, 0x5a, 0x6f, 0x6e, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x64, 0x73, 0x74, 0x5f, 0x69, 0x70, 0x18, - 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x64, 0x73, 0x74, 0x49, 0x70, 0x12, 0x17, 0x0a, 0x07, - 0x64, 0x73, 0x74, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, - 0x73, 0x74, 0x50, 0x6f, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x64, 0x73, 0x74, 0x5f, 0x6e, 0x61, 0x6d, - 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x64, 0x73, - 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x64, 0x73, - 0x74, 0x5f, 0x6e, 0x6f, 0x64, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x64, 0x73, - 0x74, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x64, 0x73, 0x74, 0x5f, 0x7a, 0x6f, 0x6e, - 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x64, 0x73, 0x74, 0x5a, 0x6f, 0x6e, 0x65, - 0x12, 0x19, 0x0a, 0x08, 0x74, 0x78, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x0b, 0x20, 0x01, - 0x28, 0x03, 0x52, 0x07, 0x74, 0x78, 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x74, - 0x78, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x03, 0x52, - 0x09, 0x74, 0x78, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x12, 0x19, 0x0a, 0x08, 0x72, 0x78, - 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x72, 0x78, - 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x72, 0x78, 0x5f, 0x70, 0x61, 0x63, 0x6b, - 0x65, 0x74, 0x73, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x72, 0x78, 0x50, 0x61, 0x63, - 0x6b, 0x65, 0x74, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x18, 0x0f, 0x20, - 0x01, 0x28, 0x05, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x20, 0x0a, 0x0c, 0x73, 0x72, - 0x63, 0x5f, 0x64, 0x6e, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x10, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x0a, 0x73, 0x72, 0x63, 0x44, 0x6e, 0x73, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0c, - 0x64, 0x73, 0x74, 0x5f, 0x64, 0x6e, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x11, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x0a, 0x64, 0x73, 0x74, 0x44, 0x6e, 0x73, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x40, - 0x0a, 0x15, 0x50, 0x6f, 0x64, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, - 0x69, 0x63, 0x42, 0x61, 0x74, 0x63, 0x68, 0x12, 0x27, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, - 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x50, 0x6f, 0x64, 0x4e, 0x65, 0x74, 0x77, - 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, - 0x42, 0x1e, 0x5a, 0x1c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, - 0x61, 0x73, 0x74, 0x61, 0x69, 0x2f, 0x65, 0x67, 0x72, 0x65, 0x73, 0x73, 0x64, 0x2f, 0x70, 0x62, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x74, 0x6f, 0x18, 0x07, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, + 0x6a, 0x0a, 0x15, 0x52, 0x61, 0x77, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, + 0x72, 0x69, 0x63, 0x42, 0x61, 0x74, 0x63, 0x68, 0x12, 0x27, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, + 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x52, 0x61, 0x77, 0x4e, 0x65, 0x74, + 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x52, 0x05, 0x69, 0x74, 0x65, 0x6d, + 0x73, 0x12, 0x28, 0x0a, 0x09, 0x69, 0x70, 0x32, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x02, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x49, 0x50, 0x32, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, + 0x52, 0x09, 0x69, 0x70, 0x32, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0xda, 0x03, 0x0a, 0x10, + 0x50, 0x6f, 0x64, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, + 0x12, 0x15, 0x0a, 0x06, 0x73, 0x72, 0x63, 0x5f, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x05, 0x73, 0x72, 0x63, 0x49, 0x70, 0x12, 0x17, 0x0a, 0x07, 0x73, 0x72, 0x63, 0x5f, 0x70, + 0x6f, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x72, 0x63, 0x50, 0x6f, 0x64, + 0x12, 0x23, 0x0a, 0x0d, 0x73, 0x72, 0x63, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, + 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, 0x72, 0x63, 0x4e, 0x61, 0x6d, 0x65, + 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x72, 0x63, 0x5f, 0x6e, 0x6f, 0x64, + 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x72, 0x63, 0x4e, 0x6f, 0x64, 0x65, + 0x12, 0x19, 0x0a, 0x08, 0x73, 0x72, 0x63, 0x5f, 0x7a, 0x6f, 0x6e, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x07, 0x73, 0x72, 0x63, 0x5a, 0x6f, 0x6e, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x64, + 0x73, 0x74, 0x5f, 0x69, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x64, 0x73, 0x74, + 0x49, 0x70, 0x12, 0x17, 0x0a, 0x07, 0x64, 0x73, 0x74, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x07, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x73, 0x74, 0x50, 0x6f, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x64, + 0x73, 0x74, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x08, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0c, 0x64, 0x73, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, + 0x12, 0x19, 0x0a, 0x08, 0x64, 0x73, 0x74, 0x5f, 0x6e, 0x6f, 0x64, 0x65, 0x18, 0x09, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x07, 0x64, 0x73, 0x74, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x64, + 0x73, 0x74, 0x5f, 0x7a, 0x6f, 0x6e, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x64, + 0x73, 0x74, 0x5a, 0x6f, 0x6e, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x74, 0x78, 0x5f, 0x62, 0x79, 0x74, + 0x65, 0x73, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x74, 0x78, 0x42, 0x79, 0x74, 0x65, + 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x74, 0x78, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x18, + 0x0c, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x78, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, + 0x12, 0x19, 0x0a, 0x08, 0x72, 0x78, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x0d, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x07, 0x72, 0x78, 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x72, + 0x78, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x09, 0x72, 0x78, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x20, 0x0a, 0x0c, 0x64, 0x73, 0x74, 0x5f, 0x64, 0x6e, 0x73, 0x5f, 0x6e, 0x61, 0x6d, 0x65, + 0x18, 0x11, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x64, 0x73, 0x74, 0x44, 0x6e, 0x73, 0x4e, 0x61, + 0x6d, 0x65, 0x4a, 0x04, 0x08, 0x10, 0x10, 0x11, 0x22, 0x40, 0x0a, 0x15, 0x50, 0x6f, 0x64, 0x4e, + 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x42, 0x61, 0x74, 0x63, + 0x68, 0x12, 0x27, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x11, 0x2e, 0x50, 0x6f, 0x64, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x4d, 0x65, 0x74, + 0x72, 0x69, 0x63, 0x52, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x22, 0x33, 0x0a, 0x09, 0x49, 0x50, + 0x32, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x02, 0x69, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, + 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x42, + 0x1e, 0x5a, 0x1c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x61, + 0x73, 0x74, 0x61, 0x69, 0x2f, 0x65, 0x67, 0x72, 0x65, 0x73, 0x73, 0x64, 0x2f, 0x70, 0x62, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -478,21 +517,23 @@ func file_pb_metrics_proto_rawDescGZIP() []byte { return file_pb_metrics_proto_rawDescData } -var file_pb_metrics_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_pb_metrics_proto_msgTypes = make([]protoimpl.MessageInfo, 5) var file_pb_metrics_proto_goTypes = []interface{}{ (*RawNetworkMetric)(nil), // 0: RawNetworkMetric (*RawNetworkMetricBatch)(nil), // 1: RawNetworkMetricBatch (*PodNetworkMetric)(nil), // 2: PodNetworkMetric (*PodNetworkMetricBatch)(nil), // 3: PodNetworkMetricBatch + (*IP2Domain)(nil), // 4: IP2Domain } var file_pb_metrics_proto_depIdxs = []int32{ 0, // 0: RawNetworkMetricBatch.items:type_name -> RawNetworkMetric - 2, // 1: PodNetworkMetricBatch.items:type_name -> PodNetworkMetric - 2, // [2:2] is the sub-list for method output_type - 2, // [2:2] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 4, // 1: RawNetworkMetricBatch.ip2domain:type_name -> IP2Domain + 2, // 2: PodNetworkMetricBatch.items:type_name -> PodNetworkMetric + 3, // [3:3] is the sub-list for method output_type + 3, // [3:3] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_pb_metrics_proto_init() } @@ -549,6 +590,18 @@ func file_pb_metrics_proto_init() { return nil } } + file_pb_metrics_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IP2Domain); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -556,7 +609,7 @@ func file_pb_metrics_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_pb_metrics_proto_rawDesc, NumEnums: 0, - NumMessages: 4, + NumMessages: 5, NumExtensions: 0, NumServices: 0, }, diff --git a/pb/metrics.proto b/pb/metrics.proto index 16997cb..155ea76 100644 --- a/pb/metrics.proto +++ b/pb/metrics.proto @@ -10,12 +10,11 @@ message RawNetworkMetric { int64 rx_bytes = 5; int64 rx_packets = 6; int32 proto = 7; - string src_dns_name = 8; - string dst_dns_name = 9; } message RawNetworkMetricBatch { repeated RawNetworkMetric items = 1; + repeated IP2Domain ip2domain = 2; } message PodNetworkMetric { @@ -34,10 +33,15 @@ message PodNetworkMetric { int64 rx_bytes = 13; int64 rx_packets = 14; int32 proto = 15; - string src_dns_name = 16; + reserved 16; // src_dns_name string dst_dns_name = 17; } message PodNetworkMetricBatch { repeated PodNetworkMetric items = 1; } + +message IP2Domain { + int32 ip = 1; + string domain = 2; +}