diff --git a/cni/network/invoker_cns.go b/cni/network/invoker_cns.go index 11d61bb148..a19aeb3631 100644 --- a/cni/network/invoker_cns.go +++ b/cni/network/invoker_cns.go @@ -27,12 +27,13 @@ type CNSIPAMInvoker struct { } type IPv4ResultInfo struct { - podIPAddress string - ncSubnetPrefix uint8 - ncPrimaryIP string - gwIPAddress string - hostSubnet string - hostPrimaryIP string + podIPAddress string + ncSubnetPrefix uint8 + ncPrimaryIP string + ncGatewayIPAddress string + hostSubnet string + hostPrimaryIP string + hostGateway string } func NewCNSInvoker(podName, namespace string) (*CNSIPAMInvoker, error) { @@ -61,17 +62,26 @@ func (invoker *CNSIPAMInvoker) Add(nwCfg *cni.NetworkConfig, subnetPrefix *net.I } resultIPv4 := IPv4ResultInfo{ - podIPAddress: response.PodIpInfo.PodIPConfig.IPAddress, - ncSubnetPrefix: response.PodIpInfo.NetworkContainerPrimaryIPConfig.IPSubnet.PrefixLength, - ncPrimaryIP: response.PodIpInfo.NetworkContainerPrimaryIPConfig.IPSubnet.IPAddress, - gwIPAddress: response.PodIpInfo.NetworkContainerPrimaryIPConfig.GatewayIPAddress, - hostSubnet: response.PodIpInfo.HostPrimaryIPInfo.Subnet, - hostPrimaryIP: response.PodIpInfo.HostPrimaryIPInfo.PrimaryIP, + podIPAddress: response.PodIpInfo.PodIPConfig.IPAddress, + ncSubnetPrefix: response.PodIpInfo.NetworkContainerPrimaryIPConfig.IPSubnet.PrefixLength, + ncPrimaryIP: response.PodIpInfo.NetworkContainerPrimaryIPConfig.IPSubnet.IPAddress, + ncGatewayIPAddress: response.PodIpInfo.NetworkContainerPrimaryIPConfig.GatewayIPAddress, + hostSubnet: response.PodIpInfo.HostPrimaryIPInfo.Subnet, + hostPrimaryIP: response.PodIpInfo.HostPrimaryIPInfo.PrimaryIP, + hostGateway: response.PodIpInfo.HostPrimaryIPInfo.Gateway, + } + + ncgw := net.ParseIP(resultIPv4.ncGatewayIPAddress) + if ncgw == nil { + return nil, nil, fmt.Errorf("Gateway address %v from response is invalid", resultIPv4.ncGatewayIPAddress) } // set the NC Primary IP in options options[network.SNATIPKey] = resultIPv4.ncPrimaryIP + // set host gateway in options + options[network.HostGWKey] = resultIPv4.hostGateway + log.Printf("Received result %+v for pod %v", resultIPv4, podInfo) result, err := getCNIIPv4Result(resultIPv4, subnetPrefix) @@ -85,7 +95,7 @@ func (invoker *CNSIPAMInvoker) Add(nwCfg *cni.NetworkConfig, subnetPrefix *net.I func getCNIIPv4Result(info IPv4ResultInfo, subnetPrefix *net.IPNet) (*cniTypesCurr.Result, error) { - gw := net.ParseIP(info.gwIPAddress) + gw := net.ParseIP(info.ncGatewayIPAddress) if gw == nil { return nil, fmt.Errorf("Gateway address %v from response is invalid", gw) } diff --git a/cns/NetworkContainerContract.go b/cns/NetworkContainerContract.go index 0f97dbb5ad..4e53408959 100644 --- a/cns/NetworkContainerContract.go +++ b/cns/NetworkContainerContract.go @@ -215,6 +215,7 @@ type PodIpInfo struct { // DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container. type HostIPInfo struct { + Gateway string PrimaryIP string Subnet string } diff --git a/cns/restserver/util.go b/cns/restserver/util.go index b493bd160a..1a67edb101 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -668,6 +668,7 @@ func (service *HTTPRestService) populateIpConfigInfoUntransacted(ipConfigStatus podIpInfo.HostPrimaryIPInfo.PrimaryIP = hostInterfaceInfo.PrimaryIP podIpInfo.HostPrimaryIPInfo.Subnet = hostInterfaceInfo.Subnet + podIpInfo.HostPrimaryIPInfo.Gateway = hostInterfaceInfo.Gateway return nil } diff --git a/network/bridge_networkclient_linux.go b/network/bridge_networkclient_linux.go index 6930aa8e84..b1b5d6e225 100644 --- a/network/bridge_networkclient_linux.go +++ b/network/bridge_networkclient_linux.go @@ -50,18 +50,32 @@ func (client *LinuxBridgeClient) CreateBridge() error { func (client *LinuxBridgeClient) AddRoutes(nwInfo *NetworkInfo, interfaceName string) error { if client.nwInfo.IPAMType == AzureCNS { - // add pod subnet to host + + // fetch the host gateway IP from options + gwIP := client.nwInfo.Options[HostGWKey] + if gwIP == nil { + return fmt.Errorf("Host gateway IP in Options not set") + } + + gatewayIP := net.ParseIP(gwIP.(string)) + if gatewayIP == nil { + return fmt.Errorf("Invalid host gateway IP: %+v", gwIP) + } + + // add host gateway as the default gateway for pod IP's devIf, _ := net.InterfaceByName(interfaceName) ifIndex := devIf.Index - family := netlink.GetIpAddressFamily(Ipv4DefaultRouteDstPrefix.IP) + family := netlink.GetIpAddressFamily(gatewayIP) nlRoute := &netlink.Route{ Family: family, Dst: &client.nwInfo.PodSubnet.Prefix, - Gw: Ipv4DefaultRouteDstPrefix.IP, + Gw: gatewayIP, LinkIndex: ifIndex, } + log.Printf("Adding Swift route %+v", nlRoute) + if err := netlink.AddIpRoute(nlRoute); err != nil { if !strings.Contains(strings.ToLower(err.Error()), "file exists") { return fmt.Errorf("Failed to add route to host interface with error: %v", err) diff --git a/network/manager.go b/network/manager.go index 2e4e4b8730..2b003e208b 100644 --- a/network/manager.go +++ b/network/manager.go @@ -21,6 +21,7 @@ const ( VlanIDKey = "VlanID" AzureCNS = "azure-cns" SNATIPKey = "NCPrimaryIPKey" + HostGWKey = "HostGatewayIP" genericData = "com.docker.network.generic" )