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 --- internal/pkg/cniserver/cni.go | 18 +++++++++++++++++- internal/pkg/network/iface.go | 18 ++++++++++++++++++ internal/pkg/ovn/ovn.go | 20 +++++++++++++------- 3 files changed, 48 insertions(+), 8 deletions(-) (limited to 'internal') diff --git a/internal/pkg/cniserver/cni.go b/internal/pkg/cniserver/cni.go index 95a41d5..ee848f7 100644 --- a/internal/pkg/cniserver/cni.go +++ b/internal/pkg/cniserver/cni.go @@ -107,6 +107,7 @@ func (cr *CNIServerRequest) addMultipleInterfaces(ovnAnnotation, namespace, podN var index int var result *current.Result var dstResult types.Result + var isDefaultGW bool for _, ovnNet := range ovnAnnotatedMap { ipAddress := ovnNet["ip_address"] macAddress := ovnNet["mac_address"] @@ -124,8 +125,23 @@ func (cr *CNIServerRequest) addMultipleInterfaces(ovnAnnotation, namespace, podN klog.Errorf("addMultipleInterfaces: interface can't be null") return nil } + + if interfaceName != "*" && defaultGateway == "true" && isDefaultGW == false { + isDefaultGW = true + } else if interfaceName != "*" && defaultGateway == "true" { + defaultGateway = "false" + } + + if interfaceName == "*" && isDefaultGW == true { + defaultGateway = "false" + } + + if interfaceName == "*" && isDefaultGW == false { + defaultGateway = "true" + } + klog.Infof("addMultipleInterfaces: ipAddress %v %v", ipAddress, interfaceName) - interfacesArray, err = app.ConfigureInterface(cr.Netns, cr.SandboxID, cr.IfName, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway, index, config.Default.MTU) + interfacesArray, err = app.ConfigureInterface(cr.Netns, cr.SandboxID, cr.IfName, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway, index, config.Default.MTU, isDefaultGW) if err != nil { klog.Errorf("Failed to configure interface in pod: %v", err) return nil diff --git a/internal/pkg/network/iface.go b/internal/pkg/network/iface.go index b2a57bd..d49566d 100644 --- a/internal/pkg/network/iface.go +++ b/internal/pkg/network/iface.go @@ -27,6 +27,24 @@ func GetDefaultGateway() (string, error) { return "", errors.New("Unable to find default route") } +//CheckRoute return bool isPresent +func CheckRoute(dst, gw string) (bool, error) { + var isPresent bool + routes, err := netlink.RouteList(nil, syscall.AF_INET) + if err != nil { + return isPresent, err + } + + for _, route := range routes { + if route.Dst.String() == dst && route.Gw.To4().String() == gw { + isPresent = true + } + } + + return isPresent, nil + +} + // GetDefaultGatewayInterface return default gateway interface link func GetDefaultGatewayInterface() (*net.Interface, error) { routes, err := netlink.RouteList(nil, syscall.AF_INET) diff --git a/internal/pkg/ovn/ovn.go b/internal/pkg/ovn/ovn.go index 97dc99d..776c534 100644 --- a/internal/pkg/ovn/ovn.go +++ b/internal/pkg/ovn/ovn.go @@ -63,6 +63,7 @@ type netInterface struct { DefaultGateway string IPAddress string MacAddress string + GWIPaddress string } var ovnCtl *Controller @@ -159,7 +160,7 @@ func (oc *Controller) AddLogicalPorts(pod *kapi.Pod, ovnNetObjs []map[string]int portName = fmt.Sprintf("%s_%s", pod.Namespace, pod.Name) ns.Interface = "*" } - outStr = oc.addLogicalPortWithSwitch(pod, ns.Name, ns.IPAddress, ns.MacAddress, portName) + outStr = oc.addLogicalPortWithSwitch(pod, ns.Name, ns.IPAddress, ns.MacAddress, ns.GWIPaddress, portName) if outStr == "" { return } @@ -174,7 +175,7 @@ func (oc *Controller) AddLogicalPorts(pod *kapi.Pod, ovnNetObjs []map[string]int if defaultInterface == false { // Add Default interface portName := fmt.Sprintf("%s_%s", pod.Namespace, pod.Name) - outStr = oc.addLogicalPortWithSwitch(pod, Ovn4nfvDefaultNw, "", "", portName) + outStr = oc.addLogicalPortWithSwitch(pod, Ovn4nfvDefaultNw, "", "", "", portName) if outStr == "" { return } @@ -465,7 +466,7 @@ func (oc *Controller) getNodeLogicalPortIPAddr(pod *kapi.Pod) (ipAddress string, return ipAddr, nil } -func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipAddress, macAddress, portName string) (annotation string) { +func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipAddress, macAddress, gwipAddress, portName string) (annotation string) { var out, stderr string var err error var isStaticIP bool @@ -552,10 +553,15 @@ func (oc *Controller) addLogicalPortWithSwitch(pod *kapi.Pod, logicalSwitch, ipA return } - gatewayIP, err := oc.getNodeLogicalPortIPAddr(pod) - if err != nil { - log.Error(err, "Error obtaining gateway address for switch", "logicalSwitch", logicalSwitch) - return + var gatewayIP string + if gwipAddress != "" { + gatewayIP = gwipAddress + } else { + gatewayIP, err = oc.getNodeLogicalPortIPAddr(pod) + if err != nil { + log.Error(err, "Error obtaining gateway address for switch", "logicalSwitch", logicalSwitch) + return + } } annotation = fmt.Sprintf(`{\"ip_address\":\"%s/%s\", \"mac_address\":\"%s\", \"gateway_ip\": \"%s\"}`, addresses[1], mask, addresses[0], gatewayIP) -- cgit 1.2.3-korg