Skip to content

Commit

Permalink
enable-custom-range
Browse files Browse the repository at this point in the history
  • Loading branch information
hany-mhajna-payu-gpo authored Nov 28, 2024
1 parent 9ae8d4b commit 9239811
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Config struct {
// or as the constantly growing counter value
SendTrafficDelta bool
LogEntries bool

// New field to accept custom CIDR ranges as strings
CustomPrivateCIDRs []string
}

type podsWatcher interface {
Expand Down Expand Up @@ -84,6 +87,16 @@ func New(
panic("cleanup interval not set")
}

customPrivateCIDRs := make([]netaddr.IPPrefix, 0, len(cfg.CustomPrivateCIDRs))
for _, cidr := range cfg.CustomPrivateCIDRs {
prefix, err := netaddr.ParseIPPrefix(cidr)
if err != nil {
log.Errorf("invalid CIDR: %s, error: %v", cidr, err)
continue
}
customPrivateCIDRs = append(customPrivateCIDRs, prefix)
}

return &Collector{
cfg: cfg,
log: log,
Expand All @@ -94,6 +107,7 @@ func New(
podMetrics: map[uint64]*rawNetworkMetric{},
excludeNsMap: excludeNsMap,
currentTimeGetter: currentTimeGetter,
customPrivateCIDRs: customPrivateCIDRs,
exporterClient: &http.Client{Timeout: 10 * time.Second},
}
}
Expand All @@ -109,6 +123,7 @@ type Collector struct {
excludeNsMap map[string]struct{}
currentTimeGetter func() time.Time
exporterClient *http.Client
customPrivateCIDRs []netaddr.IPPrefix
mu sync.Mutex

firstCollectDone bool
Expand Down Expand Up @@ -385,11 +400,22 @@ func conntrackEntryKey(conn *conntrack.Entry) uint64 {
return res
}

func isPrivateNetwork(ip netaddr.IP) bool {
return ip.IsPrivate() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
ip.IsLinkLocalMulticast() ||
ip.IsInterfaceLocalMulticast()
}
func (c *Collector) isPrivateNetwork(ip netaddr.IP) bool {
if ip.IsPrivate() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
ip.IsLinkLocalMulticast() ||
ip.IsInterfaceLocalMulticast() {
return true
}

// Check custom CIDR ranges
for _, cidr := range c.customPrivateCIDRs {
if cidr.Contains(ip) {
return true
}
}

return false
}

0 comments on commit 9239811

Please sign in to comment.