2017-02-11 03:15:53 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
|
|
|
|
2016-09-22 01:39:25 +03:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-05-17 03:02:09 +03:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2016-09-22 01:39:25 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-08-19 00:50:49 +03:00
|
|
|
"strings"
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2016-10-07 00:40:29 +03:00
|
|
|
"github.com/Azure/azure-container-networking/log"
|
2018-08-19 00:50:49 +03:00
|
|
|
"github.com/Azure/azure-container-networking/netlink"
|
2016-09-22 01:39:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-15 21:03:56 +03:00
|
|
|
// Common prefix for all types of host network interface names.
|
|
|
|
commonInterfacePrefix = "az"
|
|
|
|
|
2016-09-22 01:39:25 +03:00
|
|
|
// Prefix for host virtual network interface names.
|
2018-05-17 03:02:09 +03:00
|
|
|
hostVEthInterfacePrefix = commonInterfacePrefix + "v"
|
2016-09-22 01:39:25 +03:00
|
|
|
)
|
|
|
|
|
2018-05-17 03:02:09 +03:00
|
|
|
func generateVethName(key string) string {
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write([]byte(key))
|
|
|
|
return hex.EncodeToString(h.Sum(nil))[:11]
|
|
|
|
}
|
|
|
|
|
2021-06-12 00:01:42 +03:00
|
|
|
func ConstructEndpointID(containerID string, _ string, ifName string) (string, string) {
|
2018-07-25 03:46:46 +03:00
|
|
|
if len(containerID) > 8 {
|
|
|
|
containerID = containerID[:8]
|
|
|
|
} else {
|
|
|
|
log.Printf("Container ID is not greater than 8 ID: %v", containerID)
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
infraEpName := containerID + "-" + ifName
|
|
|
|
|
|
|
|
return infraEpName, ""
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:15:53 +03:00
|
|
|
// newEndpointImpl creates a new endpoint in the network.
|
2021-09-14 20:56:32 +03:00
|
|
|
func (nw *network) newEndpointImpl(_ apipaClient, epInfo *EndpointInfo) (*endpoint, error) {
|
2016-09-22 01:39:25 +03:00
|
|
|
var containerIf *net.Interface
|
2016-11-23 02:28:57 +03:00
|
|
|
var ns *Namespace
|
2016-09-22 01:39:25 +03:00
|
|
|
var ep *endpoint
|
|
|
|
var err error
|
2018-05-17 03:02:09 +03:00
|
|
|
var hostIfName string
|
|
|
|
var contIfName string
|
2019-07-17 03:09:34 +03:00
|
|
|
var localIP string
|
2018-07-06 21:45:47 +03:00
|
|
|
var epClient EndpointClient
|
|
|
|
var vlanid int = 0
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2017-12-28 22:47:46 +03:00
|
|
|
if nw.Endpoints[epInfo.Id] != nil {
|
2018-05-17 03:02:09 +03:00
|
|
|
log.Printf("[net] Endpoint alreday exists.")
|
2017-12-28 22:47:46 +03:00
|
|
|
err = errEndpointExists
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
if epInfo.Data != nil {
|
|
|
|
if _, ok := epInfo.Data[VlanIDKey]; ok {
|
|
|
|
vlanid = epInfo.Data[VlanIDKey].(int)
|
|
|
|
}
|
2019-07-17 03:09:34 +03:00
|
|
|
|
|
|
|
if _, ok := epInfo.Data[LocalIPKey]; ok {
|
|
|
|
localIP = epInfo.Data[LocalIPKey].(string)
|
|
|
|
}
|
2018-07-06 21:45:47 +03:00
|
|
|
}
|
|
|
|
|
2018-05-17 03:02:09 +03:00
|
|
|
if _, ok := epInfo.Data[OptVethName]; ok {
|
|
|
|
key := epInfo.Data[OptVethName].(string)
|
2019-01-10 05:29:22 +03:00
|
|
|
log.Printf("Generate veth name based on the key provided %v", key)
|
2018-05-17 03:02:09 +03:00
|
|
|
vethname := generateVethName(key)
|
|
|
|
hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, vethname)
|
|
|
|
contIfName = fmt.Sprintf("%s%s2", hostVEthInterfacePrefix, vethname)
|
|
|
|
} else {
|
|
|
|
// Create a veth pair.
|
|
|
|
log.Printf("Generate veth name based on endpoint id")
|
|
|
|
hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, epInfo.Id[:7])
|
|
|
|
contIfName = fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, epInfo.Id[:7])
|
|
|
|
}
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
if vlanid != 0 {
|
2019-01-05 03:19:36 +03:00
|
|
|
log.Printf("OVS client")
|
2019-07-17 03:09:34 +03:00
|
|
|
if _, ok := epInfo.Data[SnatBridgeIPKey]; ok {
|
|
|
|
nw.SnatBridgeIP = epInfo.Data[SnatBridgeIPKey].(string)
|
|
|
|
}
|
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
epClient = NewOVSEndpointClient(
|
2019-07-17 03:09:34 +03:00
|
|
|
nw,
|
2018-07-06 21:45:47 +03:00
|
|
|
epInfo,
|
|
|
|
hostIfName,
|
|
|
|
contIfName,
|
2019-07-17 03:09:34 +03:00
|
|
|
vlanid,
|
|
|
|
localIP)
|
2019-01-05 03:19:36 +03:00
|
|
|
} else if nw.Mode != opModeTransparent {
|
|
|
|
log.Printf("Bridge client")
|
2018-07-06 21:45:47 +03:00
|
|
|
epClient = NewLinuxBridgeEndpointClient(nw.extIf, hostIfName, contIfName, nw.Mode)
|
2019-01-05 03:19:36 +03:00
|
|
|
} else {
|
|
|
|
log.Printf("Transparent client")
|
|
|
|
epClient = NewTransparentEndpointClient(nw.extIf, hostIfName, contIfName, nw.Mode)
|
2016-09-22 01:39:25 +03:00
|
|
|
}
|
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
// Cleanup on failure.
|
2017-08-17 00:13:46 +03:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2018-07-06 21:45:47 +03:00
|
|
|
log.Printf("CNI error. Delete Endpoint %v and rules that are created.", contIfName)
|
|
|
|
endpt := &endpoint{
|
2019-07-17 03:09:34 +03:00
|
|
|
Id: epInfo.Id,
|
|
|
|
IfName: contIfName,
|
|
|
|
HostIfName: hostIfName,
|
|
|
|
LocalIP: localIP,
|
|
|
|
IPAddresses: epInfo.IPAddresses,
|
|
|
|
Gateways: []net.IP{nw.extIf.IPv4Gateway},
|
|
|
|
DNS: epInfo.DNS,
|
|
|
|
VlanID: vlanid,
|
|
|
|
EnableSnatOnHost: epInfo.EnableSnatOnHost,
|
|
|
|
EnableMultitenancy: epInfo.EnableMultiTenancy,
|
|
|
|
AllowInboundFromHostToNC: epInfo.AllowInboundFromHostToNC,
|
|
|
|
AllowInboundFromNCToHost: epInfo.AllowInboundFromNCToHost,
|
2018-07-06 21:45:47 +03:00
|
|
|
}
|
2017-08-17 00:13:46 +03:00
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
if containerIf != nil {
|
|
|
|
endpt.MacAddress = containerIf.HardwareAddr
|
|
|
|
epClient.DeleteEndpointRules(endpt)
|
|
|
|
}
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
epClient.DeleteEndpoints(endpt)
|
|
|
|
}
|
|
|
|
}()
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
if err = epClient.AddEndpoints(epInfo); err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
return nil, err
|
2016-09-22 01:39:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
containerIf, err = net.InterfaceByName(contIfName)
|
|
|
|
if err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
return nil, err
|
2016-09-22 01:39:25 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 06:29:37 +03:00
|
|
|
// Setup rules for IP addresses on the container interface.
|
2018-07-06 21:45:47 +03:00
|
|
|
if err = epClient.AddEndpointRules(epInfo); err != nil {
|
|
|
|
return nil, err
|
2016-09-22 01:39:25 +03:00
|
|
|
}
|
|
|
|
|
2016-11-23 02:28:57 +03:00
|
|
|
// If a network namespace for the container interface is specified...
|
|
|
|
if epInfo.NetNsPath != "" {
|
|
|
|
// Open the network namespace.
|
|
|
|
log.Printf("[net] Opening netns %v.", epInfo.NetNsPath)
|
|
|
|
ns, err = OpenNamespace(epInfo.NetNsPath)
|
|
|
|
if err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
return nil, err
|
2016-11-23 02:28:57 +03:00
|
|
|
}
|
|
|
|
defer ns.Close()
|
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
if err := epClient.MoveEndpointsToContainerNS(epInfo, ns.GetFd()); err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
return nil, err
|
2016-11-23 02:28:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enter the container network namespace.
|
|
|
|
log.Printf("[net] Entering netns %v.", epInfo.NetNsPath)
|
2018-07-06 21:45:47 +03:00
|
|
|
if err = ns.Enter(); err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
return nil, err
|
2016-11-23 02:28:57 +03:00
|
|
|
}
|
2017-08-17 00:13:46 +03:00
|
|
|
|
|
|
|
// Return to host network namespace.
|
|
|
|
defer func() {
|
|
|
|
log.Printf("[net] Exiting netns %v.", epInfo.NetNsPath)
|
2018-07-06 21:45:47 +03:00
|
|
|
if err := ns.Exit(); err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
log.Printf("[net] Failed to exit netns, err:%v.", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-11-23 02:28:57 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:31:48 +03:00
|
|
|
// If a name for the container interface is specified...
|
|
|
|
if epInfo.IfName != "" {
|
2018-07-06 21:45:47 +03:00
|
|
|
if err = epClient.SetupContainerInterfaces(epInfo); err != nil {
|
2017-08-17 00:13:46 +03:00
|
|
|
return nil, err
|
2016-11-22 23:31:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
if err = epClient.ConfigureContainerInterfacesAndRoutes(epInfo); err != nil {
|
|
|
|
return nil, err
|
2016-11-23 02:28:57 +03:00
|
|
|
}
|
|
|
|
|
2016-09-22 01:39:25 +03:00
|
|
|
// Create the endpoint object.
|
|
|
|
ep = &endpoint{
|
2019-07-17 03:09:34 +03:00
|
|
|
Id: epInfo.Id,
|
|
|
|
IfName: contIfName, // container veth pair name. In cnm, we won't rename this and docker expects veth name.
|
|
|
|
HostIfName: hostIfName,
|
|
|
|
MacAddress: containerIf.HardwareAddr,
|
|
|
|
InfraVnetIP: epInfo.InfraVnetIP,
|
|
|
|
LocalIP: localIP,
|
|
|
|
IPAddresses: epInfo.IPAddresses,
|
|
|
|
Gateways: []net.IP{nw.extIf.IPv4Gateway},
|
|
|
|
DNS: epInfo.DNS,
|
|
|
|
VlanID: vlanid,
|
|
|
|
EnableSnatOnHost: epInfo.EnableSnatOnHost,
|
|
|
|
EnableInfraVnet: epInfo.EnableInfraVnet,
|
|
|
|
EnableMultitenancy: epInfo.EnableMultiTenancy,
|
|
|
|
AllowInboundFromHostToNC: epInfo.AllowInboundFromHostToNC,
|
|
|
|
AllowInboundFromNCToHost: epInfo.AllowInboundFromNCToHost,
|
|
|
|
NetworkNameSpace: epInfo.NetNsPath,
|
|
|
|
ContainerID: epInfo.ContainerID,
|
|
|
|
PODName: epInfo.PODName,
|
|
|
|
PODNameSpace: epInfo.PODNameSpace,
|
2018-06-02 03:48:19 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:30:59 +03:00
|
|
|
ep.Routes = append(ep.Routes, epInfo.Routes...)
|
2016-09-22 01:39:25 +03:00
|
|
|
return ep, nil
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:15:53 +03:00
|
|
|
// deleteEndpointImpl deletes an existing endpoint from the network.
|
2021-09-14 20:56:32 +03:00
|
|
|
func (nw *network) deleteEndpointImpl(_ apipaClient, ep *endpoint) error {
|
2018-07-06 21:45:47 +03:00
|
|
|
var epClient EndpointClient
|
|
|
|
|
2016-12-08 02:56:19 +03:00
|
|
|
// Delete the veth pair by deleting one of the peer interfaces.
|
|
|
|
// Deleting the host interface is more convenient since it does not require
|
|
|
|
// entering the container netns and hence works both for CNI and CNM.
|
2018-07-06 21:45:47 +03:00
|
|
|
if ep.VlanID != 0 {
|
|
|
|
epInfo := ep.getInfo()
|
2019-07-17 03:09:34 +03:00
|
|
|
epClient = NewOVSEndpointClient(nw, epInfo, ep.HostIfName, "", ep.VlanID, ep.LocalIP)
|
2019-01-05 03:19:36 +03:00
|
|
|
} else if nw.Mode != opModeTransparent {
|
2018-07-06 21:45:47 +03:00
|
|
|
epClient = NewLinuxBridgeEndpointClient(nw.extIf, ep.HostIfName, "", nw.Mode)
|
2019-01-05 03:19:36 +03:00
|
|
|
} else {
|
|
|
|
epClient = NewTransparentEndpointClient(nw.extIf, ep.HostIfName, "", nw.Mode)
|
2016-12-08 02:56:19 +03:00
|
|
|
}
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2018-07-06 21:45:47 +03:00
|
|
|
epClient.DeleteEndpointRules(ep)
|
|
|
|
epClient.DeleteEndpoints(ep)
|
2016-09-22 01:39:25 +03:00
|
|
|
|
2017-08-03 06:29:37 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 23:36:57 +03:00
|
|
|
// getInfoImpl returns information about the endpoint.
|
|
|
|
func (ep *endpoint) getInfoImpl(epInfo *EndpointInfo) {
|
|
|
|
}
|
2018-08-19 00:50:49 +03:00
|
|
|
|
|
|
|
func addRoutes(interfaceName string, routes []RouteInfo) error {
|
|
|
|
ifIndex := 0
|
|
|
|
interfaceIf, _ := net.InterfaceByName(interfaceName)
|
|
|
|
|
|
|
|
for _, route := range routes {
|
2018-10-03 08:07:09 +03:00
|
|
|
log.Printf("[net] Adding IP route %+v to link %v.", route, interfaceName)
|
2018-08-19 00:50:49 +03:00
|
|
|
|
|
|
|
if route.DevName != "" {
|
|
|
|
devIf, _ := net.InterfaceByName(route.DevName)
|
|
|
|
ifIndex = devIf.Index
|
|
|
|
} else {
|
|
|
|
ifIndex = interfaceIf.Index
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:30:48 +03:00
|
|
|
family := netlink.GetIpAddressFamily(route.Gw)
|
|
|
|
if route.Gw == nil {
|
|
|
|
family = netlink.GetIpAddressFamily(route.Dst.IP)
|
|
|
|
}
|
|
|
|
|
2018-08-19 00:50:49 +03:00
|
|
|
nlRoute := &netlink.Route{
|
2020-04-16 08:30:48 +03:00
|
|
|
Family: family,
|
2018-08-19 00:50:49 +03:00
|
|
|
Dst: &route.Dst,
|
|
|
|
Gw: route.Gw,
|
|
|
|
LinkIndex: ifIndex,
|
2020-04-16 08:30:48 +03:00
|
|
|
Priority: route.Priority,
|
2021-01-19 21:48:11 +03:00
|
|
|
Protocol: route.Protocol,
|
|
|
|
Scope: route.Scope,
|
2018-08-19 00:50:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := netlink.AddIpRoute(nlRoute); err != nil {
|
|
|
|
if !strings.Contains(strings.ToLower(err.Error()), "file exists") {
|
|
|
|
return err
|
|
|
|
} else {
|
2019-01-05 03:19:36 +03:00
|
|
|
log.Printf("[net] route already exists")
|
2018-08-19 00:50:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRoutes(interfaceName string, routes []RouteInfo) error {
|
|
|
|
ifIndex := 0
|
|
|
|
interfaceIf, _ := net.InterfaceByName(interfaceName)
|
|
|
|
|
|
|
|
for _, route := range routes {
|
2019-01-05 03:19:36 +03:00
|
|
|
log.Printf("[net] Deleting IP route %+v from link %v.", route, interfaceName)
|
2018-08-19 00:50:49 +03:00
|
|
|
|
|
|
|
if route.DevName != "" {
|
|
|
|
devIf, _ := net.InterfaceByName(route.DevName)
|
2019-01-10 05:29:22 +03:00
|
|
|
if devIf == nil {
|
|
|
|
log.Printf("[net] Not deleting route. Interface %v doesn't exist", interfaceName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-19 00:50:49 +03:00
|
|
|
ifIndex = devIf.Index
|
|
|
|
} else {
|
2019-01-10 05:29:22 +03:00
|
|
|
if interfaceIf == nil {
|
|
|
|
log.Printf("[net] Not deleting route. Interface %v doesn't exist", interfaceName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-19 00:50:49 +03:00
|
|
|
ifIndex = interfaceIf.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
nlRoute := &netlink.Route{
|
|
|
|
Family: netlink.GetIpAddressFamily(route.Gw),
|
|
|
|
Dst: &route.Dst,
|
|
|
|
Gw: route.Gw,
|
|
|
|
LinkIndex: ifIndex,
|
2021-01-19 21:48:11 +03:00
|
|
|
Protocol: route.Protocol,
|
|
|
|
Scope: route.Scope,
|
2018-08-19 00:50:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := netlink.DeleteIpRoute(nlRoute); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-29 21:10:27 +03:00
|
|
|
|
|
|
|
// updateEndpointImpl updates an existing endpoint in the network.
|
|
|
|
func (nw *network) updateEndpointImpl(existingEpInfo *EndpointInfo, targetEpInfo *EndpointInfo) (*endpoint, error) {
|
|
|
|
var ns *Namespace
|
|
|
|
var ep *endpoint
|
|
|
|
var err error
|
|
|
|
|
|
|
|
existingEpFromRepository := nw.Endpoints[existingEpInfo.Id]
|
|
|
|
log.Printf("[updateEndpointImpl] Going to retrieve endpoint with Id %+v to update.", existingEpInfo.Id)
|
|
|
|
if existingEpFromRepository == nil {
|
|
|
|
log.Printf("[updateEndpointImpl] Endpoint cannot be updated as it does not exist.")
|
|
|
|
err = errEndpointNotFound
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
netns := existingEpFromRepository.NetworkNameSpace
|
|
|
|
// Network namespace for the container interface has to be specified
|
|
|
|
if netns != "" {
|
|
|
|
// Open the network namespace.
|
|
|
|
log.Printf("[updateEndpointImpl] Opening netns %v.", netns)
|
|
|
|
ns, err = OpenNamespace(netns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer ns.Close()
|
|
|
|
|
|
|
|
// Enter the container network namespace.
|
|
|
|
log.Printf("[updateEndpointImpl] Entering netns %v.", netns)
|
|
|
|
if err = ns.Enter(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return to host network namespace.
|
|
|
|
defer func() {
|
|
|
|
log.Printf("[updateEndpointImpl] Exiting netns %v.", netns)
|
|
|
|
if err := ns.Exit(); err != nil {
|
|
|
|
log.Printf("[updateEndpointImpl] Failed to exit netns, err:%v.", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
log.Printf("[updateEndpointImpl] Endpoint cannot be updated as the network namespace does not exist: Epid: %v", existingEpInfo.Id)
|
|
|
|
err = errNamespaceNotFound
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[updateEndpointImpl] Going to update routes in netns %v.", netns)
|
|
|
|
if err = updateRoutes(existingEpInfo, targetEpInfo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the endpoint object.
|
|
|
|
ep = &endpoint{
|
|
|
|
Id: existingEpInfo.Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update existing endpoint state with the new routes to persist
|
2021-07-08 21:30:59 +03:00
|
|
|
ep.Routes = append(ep.Routes, targetEpInfo.Routes...)
|
2018-10-29 21:10:27 +03:00
|
|
|
|
|
|
|
return ep, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateRoutes(existingEp *EndpointInfo, targetEp *EndpointInfo) error {
|
|
|
|
log.Printf("Updating routes for the endpoint %+v.", existingEp)
|
|
|
|
log.Printf("Target endpoint is %+v", targetEp)
|
|
|
|
|
|
|
|
existingRoutes := make(map[string]RouteInfo)
|
|
|
|
targetRoutes := make(map[string]RouteInfo)
|
|
|
|
var tobeDeletedRoutes []RouteInfo
|
|
|
|
var tobeAddedRoutes []RouteInfo
|
|
|
|
|
|
|
|
// we should not remove default route from container if it exists
|
|
|
|
// we do not support enable/disable snat for now
|
|
|
|
defaultDst := net.ParseIP("0.0.0.0")
|
|
|
|
|
|
|
|
log.Printf("Going to collect routes and skip default and infravnet routes if applicable.")
|
|
|
|
log.Printf("Key for default route: %+v", defaultDst.String())
|
|
|
|
|
|
|
|
infraVnetKey := ""
|
|
|
|
if targetEp.EnableInfraVnet {
|
|
|
|
infraVnetSubnet := targetEp.InfraVnetAddressSpace
|
|
|
|
if infraVnetSubnet != "" {
|
|
|
|
infraVnetKey = strings.Split(infraVnetSubnet, "/")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Key for route to infra vnet: %+v", infraVnetKey)
|
|
|
|
for _, route := range existingEp.Routes {
|
|
|
|
destination := route.Dst.IP.String()
|
|
|
|
log.Printf("Checking destination as %+v to skip or not", destination)
|
|
|
|
isDefaultRoute := destination == defaultDst.String()
|
|
|
|
isInfraVnetRoute := targetEp.EnableInfraVnet && (destination == infraVnetKey)
|
|
|
|
if !isDefaultRoute && !isInfraVnetRoute {
|
|
|
|
existingRoutes[route.Dst.String()] = route
|
|
|
|
log.Printf("%+v was skipped", destination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range targetEp.Routes {
|
|
|
|
targetRoutes[route.Dst.String()] = route
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, existingRoute := range existingRoutes {
|
|
|
|
dst := existingRoute.Dst.String()
|
|
|
|
if _, ok := targetRoutes[dst]; !ok {
|
|
|
|
tobeDeletedRoutes = append(tobeDeletedRoutes, existingRoute)
|
|
|
|
log.Printf("Adding following route to the tobeDeleted list: %+v", existingRoute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, targetRoute := range targetRoutes {
|
|
|
|
dst := targetRoute.Dst.String()
|
|
|
|
if _, ok := existingRoutes[dst]; !ok {
|
|
|
|
tobeAddedRoutes = append(tobeAddedRoutes, targetRoute)
|
|
|
|
log.Printf("Adding following route to the tobeAdded list: %+v", targetRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err := deleteRoutes(existingEp.IfName, tobeDeletedRoutes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = addRoutes(existingEp.IfName, tobeAddedRoutes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Successfully updated routes for the endpoint %+v using target: %+v", existingEp, targetEp)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-05 03:19:36 +03:00
|
|
|
|
|
|
|
func getDefaultGateway(routes []RouteInfo) net.IP {
|
|
|
|
_, defDstIP, _ := net.ParseCIDR("0.0.0.0/0")
|
|
|
|
for _, route := range routes {
|
|
|
|
if route.Dst.String() == defDstIP.String() {
|
|
|
|
return route.Gw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|