From 69b6bceec76caea07f95ba5cc372205c16e0e11b Mon Sep 17 00:00:00 2001 From: Kuralamudhan Ramakrishnan Date: Sun, 9 Aug 2020 21:38:57 -0700 Subject: adding gwipaddress features in network annotation - check defautgateway is true only for one network - first network with defautgateway is only configured, rest is ignored - add node network and ovn4nfv0-* route info for eth0 interface Signed-off-by: Kuralamudhan Ramakrishnan Change-Id: I078dee22627b2fa92e34b6cc4fa2ec7c8cd58220 --- cmd/ovn4nfvk8s-cni/app/helper_linux.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'cmd') diff --git a/cmd/ovn4nfvk8s-cni/app/helper_linux.go b/cmd/ovn4nfvk8s-cni/app/helper_linux.go index cfcd4e9..21db529 100644 --- a/cmd/ovn4nfvk8s-cni/app/helper_linux.go +++ b/cmd/ovn4nfvk8s-cni/app/helper_linux.go @@ -8,12 +8,14 @@ import ( "os/exec" "ovn4nfv-k8s-plugin/internal/pkg/config" "ovn4nfv-k8s-plugin/internal/pkg/network" + "ovn4nfv-k8s-plugin/internal/pkg/ovn" "strconv" "strings" "github.com/containernetworking/cni/pkg/types/current" "github.com/containernetworking/plugins/pkg/ip" "github.com/containernetworking/plugins/pkg/ns" + "github.com/prometheus/common/log" "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" ) @@ -96,9 +98,19 @@ func CreateNodeOVSInternalPort(nodeintfipaddr, nodeintfmacaddr, node string) err return nil } -func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, gatewayIP, defaultGateway string, idx, mtu int) (*current.Interface, *current.Interface, error) { +func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, gatewayIP, defaultGateway string, idx, mtu int, isDefaultGW bool) (*current.Interface, *current.Interface, error) { hostIface := ¤t.Interface{} contIface := ¤t.Interface{} + var hostNet string + + if defaultGateway == "false" && isDefaultGW == true && ifName == "eth0" { + var err error + hostNet, err = network.GetHostNetwork() + if err != nil { + log.Error(err, "Failed to get host network") + return nil, nil, fmt.Errorf("failed to get host network: %v", err) + } + } var oldHostVethName string err := netns.Do(func(hostNS ns.NetNS) error { @@ -147,6 +159,15 @@ func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, return err } } + + if defaultGateway == "false" && isDefaultGW == true && ifName == "eth0" { + stdout, stderr, err := ovn.RunIP("route", "add", hostNet, "via", gatewayIP) + if err != nil && !strings.Contains(stderr, "RTNETLINK answers: File exists") { + logrus.Errorf("Failed to ip route add stout %s, stderr %s, err %v", stdout, stderr, err) + return fmt.Errorf("Failed to ip route add stout %s, stderr %s, err %v", stdout, stderr, err) + } + } + oldHostVethName = hostVeth.Name return nil @@ -165,7 +186,7 @@ func setupInterface(netns ns.NetNS, containerID, ifName, macAddress, ipAddress, } // ConfigureInterface sets up the container interface -var ConfigureInterface = func(containerNetns, containerID, ifName, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway string, idx, mtu int) ([]*current.Interface, error) { +var ConfigureInterface = func(containerNetns, containerID, ifName, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway string, idx, mtu int, isDefaultGW bool) ([]*current.Interface, error) { netns, err := ns.GetNS(containerNetns) if err != nil { return nil, fmt.Errorf("failed to open netns %q: %v", containerNetns, err) @@ -178,9 +199,8 @@ var ConfigureInterface = func(containerNetns, containerID, ifName, namespace, po } else { ifaceID = fmt.Sprintf("%s_%s", namespace, podName) interfaceName = ifName - defaultGateway = "true" } - hostIface, contIface, err := setupInterface(netns, containerID, interfaceName, macAddress, ipAddress, gatewayIP, defaultGateway, idx, mtu) + hostIface, contIface, err := setupInterface(netns, containerID, interfaceName, macAddress, ipAddress, gatewayIP, defaultGateway, idx, mtu, isDefaultGW) if err != nil { return nil, err } -- cgit 1.2.3-korg