From c934c8fb1192b13496f817f908cfef9a88a46b84 Mon Sep 17 00:00:00 2001 From: Evgeny Slutsky Date: Wed, 4 Dec 2024 16:04:36 +0100 Subject: [PATCH] OCPBUGS-38468: use default route mtu Signed-off-by: Evgeny Slutsky --- pkg/config/ovn/ovn.go | 7 +++--- pkg/util/net.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/pkg/config/ovn/ovn.go b/pkg/config/ovn/ovn.go index 8adf9f38afb..1373d89782e 100644 --- a/pkg/config/ovn/ovn.go +++ b/pkg/config/ovn/ovn.go @@ -8,6 +8,7 @@ import ( "path/filepath" "regexp" + "github.com/openshift/microshift/pkg/util" "k8s.io/klog/v2" "sigs.k8s.io/yaml" ) @@ -70,10 +71,10 @@ func (o *OVNKubernetesConfig) validateConfig() error { return nil } -// getClusterMTU retrieves MTU from ovn-kubernetes gateway interface "br-ex", -// and falls back to use 1500 when "br-ex" mtu is unable to get or less than 0. +// getClusterMTU retrieves MTU from the default route network interface, +// and falls back to use 1500 when unable to get the mtu or less than 0. func (o *OVNKubernetesConfig) getClusterMTU(multinode bool) { - link, err := net.InterfaceByName(OVNGatewayInterface) + link, err := util.FindDefaultRouteInterface() if err == nil && link.MTU > 0 { o.MTU = link.MTU } else { diff --git a/pkg/util/net.go b/pkg/util/net.go index fcdc4918955..c759bf86bca 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -266,3 +266,57 @@ func GetHostIPv6(ipHint string) (string, error) { return "", fmt.Errorf("unable to find host IPv6 address") } +func FindDefaultRouteInterface() (*tcpnet.Interface, error) { + nodeIP, err := GetHostIP("") + if err != nil { + return nil, fmt.Errorf("failed to get host IP: %v", err) + } + + ip := tcpnet.ParseIP(nodeIP) + if ip == nil { + return nil, fmt.Errorf("invalid IP address: %s", nodeIP) + } + + ifaces, err := tcpnet.Interfaces() + + if err != nil { + return nil, err + } + + isDown := func(iface tcpnet.Interface) bool { + return iface.Flags&1 == 0 + } + + for _, iface := range ifaces { + if isDown(iface) { + continue + } + found, err := ipAddrExistsAtInterface(ip, iface) + if err != nil { + return nil, err + } + if !found { + continue + } + + return &iface, nil + } + return nil, fmt.Errorf("no usable interface found") +} + +func ipAddrExistsAtInterface(ipAddr tcpnet.IP, iface tcpnet.Interface) (bool, error) { + addrs, err := iface.Addrs() + + if err != nil { + return false, err + } + + for _, a := range addrs { + if ipnet, ok := a.(*tcpnet.IPNet); ok { + if ipnet.IP.Equal(ipAddr) { + return true, nil + } + } + } + return false, nil +}