Skip to content

Commit

Permalink
set mtu in cni spec to auto configure MTU's of the pod's veth's and…
Browse files Browse the repository at this point in the history
… kube-bridge interfaces

Fixes #165
  • Loading branch information
murali-reddy authored and aauren committed Oct 2, 2020
1 parent d32d651 commit db1bd56
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/controllers/routing/network_routes_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package routing

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -341,6 +342,46 @@ func (nrc *NetworkRoutingController) updateCNIConfig() {
glog.Fatalf("Failed to insert `subnet`(pod CIDR) into CNI conf file: %s", err.Error())
}
}

err = nrc.autoConfigureMTU()
if err != nil {
glog.Fatalf("Failed to auto-configure MTU: %s", err.Error())
}
}

func (nrc *NetworkRoutingController) autoConfigureMTU() error {
mtu, err := getMTUFromNodeIP(nrc.nodeIP)
file, err := ioutil.ReadFile(nrc.cniConfFile)
if err != nil {
return fmt.Errorf("Failed to load CNI conf file: %s", err.Error())
}
var config interface{}
err = json.Unmarshal(file, &config)
if err != nil {
return fmt.Errorf("Failed to parse JSON from CNI conf file: %s", err.Error())
}
if strings.HasSuffix(nrc.cniConfFile, ".conflist") {
configMap := config.(map[string]interface{})
for key := range configMap {
if key != "plugins" {
continue
}
pluginConfigs := configMap["plugins"].([]interface{})
for _, pluginConfig := range pluginConfigs {
pluginConfigMap := pluginConfig.(map[string]interface{})
pluginConfigMap["mtu"] = mtu
}
}
} else {
pluginConfig := config.(map[string]interface{})
pluginConfig["mtu"] = mtu
}
configJSON, _ := json.Marshal(config)
err = ioutil.WriteFile(nrc.cniConfFile, configJSON, 0644)
if err != nil {
return fmt.Errorf("Failed to insert `mtu` into CNI conf file: %s", err.Error())
}
return nil
}

func (nrc *NetworkRoutingController) watchBgpUpdates() {
Expand Down
20 changes: 20 additions & 0 deletions pkg/controllers/routing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ func getNodeSubnet(nodeIP net.IP) (net.IPNet, string, error) {
return net.IPNet{}, "", errors.New("Failed to find interface with specified node ip")
}

func getMTUFromNodeIP(nodeIP net.IP) (int, error) {
links, err := netlink.LinkList()
if err != nil {
return 0, errors.New("Failed to get list of links")
}
for _, link := range links {
addresses, err := netlink.AddrList(link, netlink.FAMILY_ALL)
if err != nil {
return 0, errors.New("Failed to get list of addr")
}
for _, addr := range addresses {
if addr.IPNet.IP.Equal(nodeIP) {
lintMTU := link.Attrs().MTU
return lintMTU - 20, nil // -20 to accommodate IPIP header
}
}
}
return 0, errors.New("Failed to find interface with specified node ip")
}

// generateTunnelName will generate a name for a tunnel interface given a node IP
// for example, if the node IP is 10.0.0.1 the tunnel interface will be named tun-10001
// Since linux restricts interface names to 15 characters, if length of a node IP
Expand Down

0 comments on commit db1bd56

Please sign in to comment.