find pod networks
@@ -2,10 +2,11 @@ package discovery
import (
"github.com/grandcat/zeroconf"
+ "github.com/netgroup-polito/dronev2/internal/discovery/clients"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"math/rand"
"net"
"os"
- "strings"
"time"
)
@@ -44,17 +45,20 @@ func GetInterfaces() []net.Interface {
if err != nil {
return nil
}
+ podNets, err := getPodNets()
+ if err != nil {
+ return nil
+ }
for _, ifi := range ifaces {
addrs, err := ifi.Addrs()
if err != nil {
continue
}
- // TODO: find smarter way
// select interfaces with IP addresses not in pod local network
sel := false
for _, addr := range addrs {
- if !strings.Contains(addr.String(), "10.") {
ip := getIP(addr)
+ if !isPod(podNets, ip) {
if ip != nil && ip.To4() != nil {
sel = true
}
@@ -73,3 +77,33 @@ func GetInterfaces() []net.Interface {
}
return interfaces
}
+
+func getPodNets() ([]*net.IPNet, error) {
+ client, err := clients.NewK8sClient()
+ if err != nil {
+ return nil, err
+ }
+ nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
+ if err != nil {
+ return nil, err
+ }
+ var res []*net.IPNet
+ for _, n := range nodes.Items {
+ _, ipnet, err := net.ParseCIDR(n.Spec.PodCIDR)
+ if err != nil {
+ Log.Error(err, err.Error())
+ continue
+ }
+ res = append(res, ipnet)
+ }
+ return res, nil
+}
+
+func isPod(podNets []*net.IPNet, ip net.IP) bool {
+ for _, ipnet := range podNets {
+ if ipnet.Contains(ip) {
+ return true
+ }
+ }
+ return false
+}
