Skip to content

Commit a610596

Browse files
committed
fact(GetMTUFromNodeIP): move up a layer of abstraction
This function is useful for more than just the NRC, move it up a layer into the global utils so it can be used from multiple controllers.
1 parent e223ea8 commit a610596

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

pkg/controllers/routing/network_routes_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (nrc *NetworkRoutingController) Run(healthChan chan<- *healthcheck.Controll
215215
}
216216

217217
if nrc.autoMTU {
218-
mtu, err := getMTUFromNodeIP(nrc.nodeIP, nrc.enableOverlays)
218+
mtu, err := utils.GetMTUFromNodeIP(nrc.nodeIP, nrc.enableOverlays)
219219
if err != nil {
220220
klog.Errorf("Failed to find MTU for node IP: %s for intelligently setting the kube-bridge MTU due to %s.", nrc.nodeIP, err.Error())
221221
}
@@ -373,7 +373,7 @@ func (nrc *NetworkRoutingController) updateCNIConfig() {
373373
}
374374

375375
func (nrc *NetworkRoutingController) autoConfigureMTU() error {
376-
mtu, err := getMTUFromNodeIP(nrc.nodeIP, nrc.enableOverlays)
376+
mtu, err := utils.GetMTUFromNodeIP(nrc.nodeIP, nrc.enableOverlays)
377377
if err != nil {
378378
return fmt.Errorf("failed to generate MTU: %s", err.Error())
379379
}

pkg/controllers/routing/utils.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,6 @@ func getNodeSubnet(nodeIP net.IP) (net.IPNet, string, error) {
109109
return net.IPNet{}, "", errors.New("failed to find interface with specified node ip")
110110
}
111111

112-
func getMTUFromNodeIP(nodeIP net.IP, overlayEnabled bool) (int, error) {
113-
links, err := netlink.LinkList()
114-
if err != nil {
115-
return 0, errors.New("failed to get list of links")
116-
}
117-
for _, link := range links {
118-
addresses, err := netlink.AddrList(link, netlink.FAMILY_ALL)
119-
if err != nil {
120-
return 0, errors.New("failed to get list of addr")
121-
}
122-
for _, addr := range addresses {
123-
if addr.IPNet.IP.Equal(nodeIP) {
124-
linkMTU := link.Attrs().MTU
125-
if overlayEnabled {
126-
return linkMTU - 20, nil // -20 to accommodate IPIP header
127-
}
128-
return linkMTU, nil
129-
}
130-
}
131-
}
132-
return 0, errors.New("failed to find interface with specified node ip")
133-
}
134-
135112
// generateTunnelName will generate a name for a tunnel interface given a node IP
136113
// for example, if the node IP is 10.0.0.1 the tunnel interface will be named tun-10001
137114
// Since linux restricts interface names to 15 characters, if length of a node IP

pkg/utils/node.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"net"
88
"os"
99

10+
"github.com/vishvananda/netlink"
11+
1012
apiv1 "k8s.io/api/core/v1"
1113
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1214
"k8s.io/client-go/kubernetes"
@@ -59,3 +61,27 @@ func GetNodeIP(node *apiv1.Node) (net.IP, error) {
5961
}
6062
return nil, errors.New("host IP unknown")
6163
}
64+
65+
// GetMTUFromNodeIP returns the MTU by detecting it from the IP on the node and figuring in tunneling configurations
66+
func GetMTUFromNodeIP(nodeIP net.IP, overlayEnabled bool) (int, error) {
67+
links, err := netlink.LinkList()
68+
if err != nil {
69+
return 0, errors.New("failed to get list of links")
70+
}
71+
for _, link := range links {
72+
addresses, err := netlink.AddrList(link, netlink.FAMILY_ALL)
73+
if err != nil {
74+
return 0, errors.New("failed to get list of addr")
75+
}
76+
for _, addr := range addresses {
77+
if addr.IPNet.IP.Equal(nodeIP) {
78+
linkMTU := link.Attrs().MTU
79+
if overlayEnabled {
80+
return linkMTU - 20, nil // -20 to accommodate IPIP header
81+
}
82+
return linkMTU, nil
83+
}
84+
}
85+
}
86+
return 0, errors.New("failed to find interface with specified node IP")
87+
}

0 commit comments

Comments
 (0)