azure-container-networking/network/transparent_endpointclient_...

330 строки
11 KiB
Go
Исходник Обычный вид История

package network
import (
"fmt"
"net"
"github.com/Azure/azure-container-networking/netio"
"github.com/Azure/azure-container-networking/netlink"
"github.com/Azure/azure-container-networking/network/networkutils"
"github.com/Azure/azure-container-networking/platform"
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
"github.com/pkg/errors"
"go.uber.org/zap"
)
const (
virtualGwIPString = "169.254.1.1/32"
defaultGwCidr = "0.0.0.0/0"
defaultGw = "0.0.0.0"
virtualv6GwString = "fe80::1234:5678:9abc/128"
defaultv6Cidr = "::/0"
ipv4Bits = 32
ipv6Bits = 128
ipv4FullMask = 32
ipv6FullMask = 128
defaultHostVethHwAddr = "aa:aa:aa:aa:aa:aa"
)
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
var errorTransparentEndpointClient = errors.New("TransparentEndpointClient Error")
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
func newErrorTransparentEndpointClient(err error) error {
return errors.Wrapf(err, "%s", errorTransparentEndpointClient)
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
}
type TransparentEndpointClient struct {
bridgeName string
hostPrimaryIfName string
hostVethName string
containerVethName string
hostPrimaryMac net.HardwareAddr
containerMac net.HardwareAddr
hostVethMac net.HardwareAddr
mode string
netlink netlink.NetlinkInterface
netioshim netio.NetIOInterface
plClient platform.ExecClient
netUtilsClient networkutils.NetworkUtils
}
func NewTransparentEndpointClient(
extIf *externalInterface,
hostVethName string,
containerVethName string,
mode string,
nl netlink.NetlinkInterface,
nioc netio.NetIOInterface,
plc platform.ExecClient,
) *TransparentEndpointClient {
client := &TransparentEndpointClient{
bridgeName: extIf.BridgeName,
hostPrimaryIfName: extIf.Name,
hostVethName: hostVethName,
containerVethName: containerVethName,
hostPrimaryMac: extIf.MacAddress,
mode: mode,
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
netlink: nl,
netioshim: nioc,
plClient: plc,
netUtilsClient: networkutils.NewNetworkUtils(nl, plc),
}
return client
}
func (client *TransparentEndpointClient) setArpProxy(ifName string) error {
cmd := fmt.Sprintf("echo 1 > /proc/sys/net/ipv4/conf/%v/proxy_arp", ifName)
_, err := client.plClient.ExecuteCommand(cmd)
return err
}
func (client *TransparentEndpointClient) AddEndpoints(epInfo *EndpointInfo) error {
if _, err := client.netioshim.GetNetworkInterfaceByName(client.hostVethName); err == nil {
logger.Info("Deleting old host veth", zap.String("hostVethName", client.hostVethName))
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
if err = client.netlink.DeleteLink(client.hostVethName); err != nil {
logger.Error("Failed to delete old", zap.String("hostVethName", client.hostVethName), zap.Error(err))
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
}
primaryIf, err := client.netioshim.GetNetworkInterfaceByName(client.hostPrimaryIfName)
if err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
mac, err := net.ParseMAC(defaultHostVethHwAddr)
if err != nil {
logger.Error("Failed to parse the mac addrress", zap.String("defaultHostVethHwAddr", defaultHostVethHwAddr))
}
if err = client.netUtilsClient.CreateEndpoint(client.hostVethName, client.containerVethName, mac); err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
defer func() {
if err != nil {
if delErr := client.netlink.DeleteLink(client.hostVethName); delErr != nil {
logger.Error("Deleting veth failed on addendpoint failure", zap.Error(delErr))
}
}
}()
containerIf, err := client.netioshim.GetNetworkInterfaceByName(client.containerVethName)
if err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
client.containerMac = containerIf.HardwareAddr
hostVethIf, err := client.netioshim.GetNetworkInterfaceByName(client.hostVethName)
if err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
client.hostVethMac = hostVethIf.HardwareAddr
logger.Info("Setting mtu on veth interface", zap.Int("MTU", primaryIf.MTU), zap.String("hostVethName", client.hostVethName))
if err := client.netlink.SetLinkMTU(client.hostVethName, primaryIf.MTU); err != nil {
logger.Error("Setting mtu failed for hostveth", zap.String("hostVethName", client.hostVethName),
zap.Error(err))
}
if err := client.netlink.SetLinkMTU(client.containerVethName, primaryIf.MTU); err != nil {
logger.Error("Setting mtu failed for containerveth", zap.String("containerVethName", client.containerVethName),
zap.Error(err))
}
return nil
}
func (client *TransparentEndpointClient) AddEndpointRules(epInfo *EndpointInfo) error {
var routeInfoList []RouteInfo
// ip route add <podip> dev <hostveth>
// This route is needed for incoming packets to pod to route via hostveth
for _, ipAddr := range epInfo.IPAddresses {
var (
routeInfo RouteInfo
ipNet net.IPNet
)
if ipAddr.IP.To4() != nil {
ipNet = net.IPNet{IP: ipAddr.IP, Mask: net.CIDRMask(ipv4FullMask, ipv4Bits)}
} else {
ipNet = net.IPNet{IP: ipAddr.IP, Mask: net.CIDRMask(ipv6FullMask, ipv6Bits)}
}
logger.Info("Adding route for the", zap.String("ip", ipNet.String()))
routeInfo.Dst = ipNet
routeInfoList = append(routeInfoList, routeInfo)
}
if err := addRoutes(client.netlink, client.netioshim, client.hostVethName, routeInfoList); err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
logger.Info("calling setArpProxy for", zap.String("hostVethName", client.hostVethName))
if err := client.setArpProxy(client.hostVethName); err != nil {
logger.Error("setArpProxy failed with", zap.Error(err))
return err
}
return nil
}
func (client *TransparentEndpointClient) DeleteEndpointRules(ep *endpoint) {
// ip route del <podip> dev <hostveth>
// Deleting the route set up for routing the incoming packets to pod
for _, ipAddr := range ep.IPAddresses {
var (
routeInfo RouteInfo
ipNet net.IPNet
)
if ipAddr.IP.To4() != nil {
ipNet = net.IPNet{IP: ipAddr.IP, Mask: net.CIDRMask(ipv4FullMask, ipv4Bits)}
} else {
ipNet = net.IPNet{IP: ipAddr.IP, Mask: net.CIDRMask(ipv6FullMask, ipv6Bits)}
}
logger.Info("Deleting route for the", zap.String("ip", ipNet.String()))
routeInfo.Dst = ipNet
if err := deleteRoutes(client.netlink, client.netioshim, client.hostVethName, []RouteInfo{routeInfo}); err != nil {
logger.Error("Failed to delete route on VM for the", zap.String("ip", ipNet.String()), zap.Error(err))
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
}
}
}
func (client *TransparentEndpointClient) MoveEndpointsToContainerNS(epInfo *EndpointInfo, nsID uintptr) error {
// Move the container interface to container's network namespace.
logger.Info("Setting link netns", zap.String("containerVethName", client.containerVethName), zap.String("NetNsPath", epInfo.NetNsPath))
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
if err := client.netlink.SetLinkNetNs(client.containerVethName, nsID); err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
return nil
}
func (client *TransparentEndpointClient) SetupContainerInterfaces(epInfo *EndpointInfo) error {
if err := client.netUtilsClient.SetupContainerInterface(client.containerVethName, epInfo.IfName); err != nil {
return err
}
client.containerVethName = epInfo.IfName
return nil
}
func (client *TransparentEndpointClient) ConfigureContainerInterfacesAndRoutes(epInfo *EndpointInfo) error {
if err := client.netUtilsClient.AssignIPToInterface(client.containerVethName, epInfo.IPAddresses); err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
// ip route del 10.240.0.0/12 dev eth0 (removing kernel subnet route added by above call)
for _, ipAddr := range epInfo.IPAddresses {
_, ipnet, _ := net.ParseCIDR(ipAddr.String())
routeInfo := RouteInfo{
Dst: *ipnet,
Scope: netlink.RT_SCOPE_LINK,
Protocol: netlink.RTPROT_KERNEL,
}
if err := deleteRoutes(client.netlink, client.netioshim, client.containerVethName, []RouteInfo{routeInfo}); err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
}
// add route for virtualgwip
// ip route add 169.254.1.1/32 dev eth0
virtualGwIP, virtualGwNet, _ := net.ParseCIDR(virtualGwIPString)
routeInfo := RouteInfo{
Dst: *virtualGwNet,
Scope: netlink.RT_SCOPE_LINK,
}
if err := addRoutes(client.netlink, client.netioshim, client.containerVethName, []RouteInfo{routeInfo}); err != nil {
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
return newErrorTransparentEndpointClient(err)
}
feat: cni refactor for swift v2 (#2330) * feat: update contracts to support swift 2 * add comments * rename AddressType to NICType * update contract names and comments * address comments * feat: update invokers to support swift 2 * address comments * address comments * refactor cns invoker per comments * update invokers based on contract change * update test * update with contract changes * fix linter errs * fix naming * fix linter * fix linter * address comments * update tests * add tests * address nit comments * add comments * address comments * fix casing * address comments * feat: update invokers to support swift 2 * address comments * feat: update invokers to support swift 2 * feat: update invokers to support swift 2 * feat: update endpoint clients for swift 2 * address comments * fix lint errs * update endpoint clients based on contract changes * update tests * only skip adding default route * modify AddEndpoints per comments * address comments * update deleteendpoint * enter ns before moving interface back to vm ns * update delete endpoint test * add namespace interface for testing * fix lint * fix lint * add comment * add extra delete endpoint test * update test * feat: update invokers to support swift 2 * address comments * address comments * feat: refactor endpoint create/delete flow for swift 2 * address comments * address comments * address linter * update based on contract changes * update with contract changes * add more tests and address comments * modify AddEndpoints per comments * update test for invoker add and endpoint client add failure * address comments * fix lint * update windows tests * update refactor with namespace interface * fix lint * rebasing fixes * address comments --------- Co-authored-by: Jaeryn <tsch@microsoft.com>
2023-11-01 22:50:35 +03:00
if !epInfo.SkipDefaultRoutes {
// ip route add default via 169.254.1.1 dev eth0
_, defaultIPNet, _ := net.ParseCIDR(defaultGwCidr)
dstIP := net.IPNet{IP: net.ParseIP(defaultGw), Mask: defaultIPNet.Mask}
routeInfo = RouteInfo{
Dst: dstIP,
Gw: virtualGwIP,
}
if err := addRoutes(client.netlink, client.netioshim, client.containerVethName, []RouteInfo{routeInfo}); err != nil {
return err
}
} else if err := addRoutes(client.netlink, client.netioshim, client.containerVethName, epInfo.Routes); err != nil {
return newErrorTransparentEndpointClient(err)
}
// arp -s 169.254.1.1 e3:45:f4:ac:34:12 - add static arp entry for virtualgwip to hostveth interface mac
logger.Info("Adding static arp for IP address and MAC in Container namespace",
zap.String("address", virtualGwNet.String()), zap.Any("hostVethMac", client.hostVethMac))
linkInfo := netlink.LinkInfo{
Name: client.containerVethName,
IPAddr: virtualGwNet.IP,
MacAddress: client.hostVethMac,
}
if err := client.netlink.SetOrRemoveLinkAddress(linkInfo, netlink.ADD, netlink.NUD_PROBE); err != nil {
return fmt.Errorf("Adding arp in container failed: %w", err)
}
2023-04-30 00:50:44 +03:00
// IPv6Mode can be ipv6NAT or dual stack overlay
// set epInfo ipv6Mode to 'dualStackOverlay' to set ipv6Routes and ipv6NeighborEntries for Linux pod in dualStackOverlay ipam mode
if epInfo.IPV6Mode != "" {
if err := client.setupIPV6Routes(); err != nil {
return err
}
}
if epInfo.IPV6Mode != "" {
return client.setIPV6NeighEntry()
}
return nil
}
func (client *TransparentEndpointClient) setupIPV6Routes() error {
logger.Info("Setting up ipv6 routes in container")
// add route for virtualgwip
// ip -6 route add fe80::1234:5678:9abc/128 dev eth0
virtualGwIP, virtualGwNet, _ := net.ParseCIDR(virtualv6GwString)
gwRoute := RouteInfo{
Dst: *virtualGwNet,
Scope: netlink.RT_SCOPE_LINK,
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
}
// ip -6 route add default via fe80::1234:5678:9abc dev eth0
_, defaultIPNet, _ := net.ParseCIDR(defaultv6Cidr)
logger.Info("defaultv6ipnet", zap.Any("defaultIPNet", defaultIPNet))
defaultRoute := RouteInfo{
Dst: *defaultIPNet,
Gw: virtualGwIP,
}
return addRoutes(client.netlink, client.netioshim, client.containerVethName, []RouteInfo{gwRoute, defaultRoute})
}
func (client *TransparentEndpointClient) setIPV6NeighEntry() error {
logger.Info("Add v6 neigh entry for default gw ip")
hostGwIP, _, _ := net.ParseCIDR(virtualv6GwString)
linkInfo := netlink.LinkInfo{
Name: client.containerVethName,
IPAddr: hostGwIP,
MacAddress: client.hostVethMac,
}
if err := client.netlink.SetOrRemoveLinkAddress(linkInfo, netlink.ADD, netlink.NUD_PERMANENT); err != nil {
logger.Error("Failed setting neigh entry in container", zap.Error(err))
return fmt.Errorf("Failed setting neigh entry in container: %w", err)
}
Netlink package interfacing and adding a fake (#996) (#1025) * Initial pass at Netlink interface * changing some netlink and epc * Resolcing all dependencies on netlink package * first pass at adding a netlinkinterface * windows working now * feat: update cns client (#992) * fix debug commands Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix: update cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to debug calls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * repackage cns client Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * add ctx to all methods and preinit all route urls Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * down-scope cns client interface and move to consumer packages Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * no unkeyed struct literals Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * trace updated client method signatures out through windows paths * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * fix windows build Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * delint Signed-off-by: Evan Baker <rbtr@users.noreply.github.com> * windows working now * Some golints checks * commenting a flaky NPM UT and adding some golint checks * renaming fakenetlink to mocknetlink * removing a mock netlink usage * fixing more golints and a test fix * fixing more go lints * Adding in netlink from higher level as input * adding netlinkinterface to windows endpoint impl * removing netlink name confusion Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Evan Baker <rbtr@users.noreply.github.com>
2021-09-20 21:57:12 +03:00
return nil
}
func (client *TransparentEndpointClient) DeleteEndpoints(_ *endpoint) error {
return nil
}