azure-container-networking/network/endpoint_linux.go

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

// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package network
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"net"
"strings"
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/Azure/azure-container-networking/cns"
"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/ovsctl"
"github.com/Azure/azure-container-networking/platform"
"go.uber.org/zap"
)
const (
// Common prefix for all types of host network interface names.
commonInterfacePrefix = "az"
// Prefix for host virtual network interface names.
hostVEthInterfacePrefix = commonInterfacePrefix + "v"
)
cni/network unit test coverage (#1020) * adding uts * 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> * Remove dead codes from telemetry package (#1004) * Netlink package interfacing and adding a fake (#996) * 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> * test: add tests for CNI Azure invoker (#1010) * include add tests * test delete * gci * chore: Refactor UTs in telemetry packages (#1011) * Refactor UTs to cleanup UTs and increase UT coverages * User assert for consistency * Applied comments and resolve lint error * Delete unnecessary license header * Add UT coverage ovs_network_client (#1008) * Added ovsctl mock * Changed iptables and ovsctl to use interface instead of concrete classes * Added tests for ovs_networkclient_linux.go * Fix linter issues Co-authored-by: Shriroop <shrjo@microsoft.com> * unitest for add, delete, get added test for handling second add call in windows added linux and windows specific tests added multitenancy, baremetal tests fixed linter errors * fix linter issue * fix nns test added comment linter fixes and dependency injection from top * adding back removed file fixed merge issues * linter fixes Co-authored-by: Evan Baker <rbtr@users.noreply.github.com> Co-authored-by: JungukCho <jungukcho@microsoft.com> Co-authored-by: Vamsi Kalapala <vakr@microsoft.com> Co-authored-by: Mathew Merrick <matmerr@users.noreply.github.com> Co-authored-by: Shriroop Joshi <shriroopjoshi@users.noreply.github.com> Co-authored-by: Shriroop <shrjo@microsoft.com>
2021-09-21 02:58:18 +03:00
type AzureHNSEndpointClient interface{}
func generateVethName(key string) string {
h := sha1.New()
h.Write([]byte(key))
return hex.EncodeToString(h.Sum(nil))[:11]
}
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 {
logger.Info("Container ID is not greater than 8 ID", zap.String("containerID", containerID))
2018-07-25 03:46:46 +03:00
return "", ""
}
infraEpName := containerID + "-" + ifName
return infraEpName, ""
}
// newEndpointImpl creates a new endpoint in the network.
func (nw *network) newEndpointImpl(
_ apipaClient,
nl netlink.NetlinkInterface,
plc platform.ExecClient,
netioCli netio.NetIOInterface,
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
testEpClient EndpointClient,
nsc NamespaceClientInterface,
iptc ipTablesClient,
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
epInfo []*EndpointInfo,
) (*endpoint, 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
var (
err error
hostIfName string
contIfName string
localIP string
vlanid = 0
defaultEpInfo = epInfo[0]
containerIf *net.Interface
)
if nw.Endpoints[defaultEpInfo.Id] != nil {
logger.Info("[net] Endpoint already exists.")
err = errEndpointExists
return nil, 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 defaultEpInfo.Data != nil {
if _, ok := defaultEpInfo.Data[VlanIDKey]; ok {
vlanid = defaultEpInfo.Data[VlanIDKey].(int)
}
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 _, ok := defaultEpInfo.Data[LocalIPKey]; ok {
localIP = defaultEpInfo.Data[LocalIPKey].(string)
}
}
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 _, ok := defaultEpInfo.Data[OptVethName]; ok {
key := defaultEpInfo.Data[OptVethName].(string)
logger.Info("Generate veth name based on the key provided", zap.String("key", key))
vethname := generateVethName(key)
hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, vethname)
contIfName = fmt.Sprintf("%s%s2", hostVEthInterfacePrefix, vethname)
} else {
// Create a veth pair.
logger.Info("Generate veth name based on endpoint id")
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
hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, defaultEpInfo.Id[:7])
contIfName = fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, defaultEpInfo.Id[:7])
}
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
ep := &endpoint{
Id: defaultEpInfo.Id,
IfName: contIfName, // container veth pair name. In cnm, we won't rename this and docker expects veth name.
HostIfName: hostIfName,
InfraVnetIP: defaultEpInfo.InfraVnetIP,
LocalIP: localIP,
IPAddresses: defaultEpInfo.IPAddresses,
DNS: defaultEpInfo.DNS,
VlanID: vlanid,
EnableSnatOnHost: defaultEpInfo.EnableSnatOnHost,
EnableInfraVnet: defaultEpInfo.EnableInfraVnet,
EnableMultitenancy: defaultEpInfo.EnableMultiTenancy,
AllowInboundFromHostToNC: defaultEpInfo.AllowInboundFromHostToNC,
AllowInboundFromNCToHost: defaultEpInfo.AllowInboundFromNCToHost,
NetworkNameSpace: defaultEpInfo.NetNsPath,
ContainerID: defaultEpInfo.ContainerID,
PODName: defaultEpInfo.PODName,
PODNameSpace: defaultEpInfo.PODNameSpace,
Routes: defaultEpInfo.Routes,
SecondaryInterfaces: make(map[string]*InterfaceInfo),
}
if nw.extIf != nil {
ep.Gateways = []net.IP{nw.extIf.IPv4Gateway}
}
for _, epInfo := range epInfo {
// testEpClient is non-nil only when the endpoint is created for the unit test
// resetting epClient to testEpClient in loop to use the test endpoint client if specified
epClient := testEpClient
if epClient == nil {
//nolint:gocritic
if vlanid != 0 {
if nw.Mode == opModeTransparentVlan {
logger.Info("Transparent vlan client")
if _, ok := epInfo.Data[SnatBridgeIPKey]; ok {
nw.SnatBridgeIP = epInfo.Data[SnatBridgeIPKey].(string)
}
epClient = NewTransparentVlanEndpointClient(nw, epInfo, hostIfName, contIfName, vlanid, localIP, nl, plc, nsc, iptc)
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
} else {
logger.Info("OVS client")
if _, ok := epInfo.Data[SnatBridgeIPKey]; ok {
nw.SnatBridgeIP = epInfo.Data[SnatBridgeIPKey].(string)
}
epClient = NewOVSEndpointClient(
nw,
epInfo,
hostIfName,
contIfName,
vlanid,
localIP,
nl,
ovsctl.NewOvsctl(),
plc,
iptc)
}
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
} else if nw.Mode != opModeTransparent {
logger.Info("Bridge client")
epClient = NewLinuxBridgeEndpointClient(nw.extIf, hostIfName, contIfName, nw.Mode, nl, plc)
} else if epInfo.NICType == cns.DelegatedVMNIC {
logger.Info("Secondary client")
epClient = NewSecondaryEndpointClient(nl, netioCli, plc, nsc, ep)
} else {
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
logger.Info("Transparent client")
epClient = NewTransparentEndpointClient(nw.extIf, hostIfName, contIfName, nw.Mode, nl, netioCli, plc)
feat: Add native linux endpoint client to prep removing OVS (#1471) * Native Endpoint Client Add Endpoints * AddEndpointRules, ConfigureContainerInterfacesAndRoutes * Changed interface names, log statements nw.extIf.Name > eth0 (eth0) eth0.vlanid > eth0.X (eth0.1) %s%s hostIfName > vnet (A1veth0) %s%s-2 contIfName > container (B1veth0) * Renaming, using lib to set ns * Namespace "path" is /var/run/netns/<NS> * Loopback set up, Remove auto kernel subnet route * Cannot set link to up if it's in another NS * Multiple containers on same VNET NS * Delete Endpoint routes on Delete * Minimizing netns usage * Moving NS Exec Code * Further minimized netns.Set usage * Moved helper methods down, drafted tests * Removed DevName from Route Info, more tests * Test existing vnet ns, delete endpoint * NetNS interface for testing * Separated tests by namespace * Endpoints delete if they cannot be moved into NS * Namespace netns tests * Added Native Client to deleteEndpointImpl * Deletion of Endpoints Impl and Tests * Cleaned code (Tests ok) * Moved mock/netns to package (Tests ok) * Fixing Netns (wip) Moved netnsinterface to consumer package (network). Removed "Netns" from "NewNetns" and "NewMockNetns" as it is unambiguous. Changed uintptr to int and casted the int to uintptr when needed later. * Using errors.Wrap for error context (wip) * Removed sentence case (wip) * Removing variable predeclaration * Removed NewNativeEndpointClient Directly instantiating struct because nothing special happens in NewNativeEndpointClient * Removed generics from ExecuteInNS * Removed uintptr from mocknetns, tests compile Forgot to remove uintptr from mocknetns * Fix tests, lint * Fixes from linter Works on VMSS * Replacing references to ethX with vlan veth * Removed unnecessary log * Removed unnecessary mac, fix tests * Mockns method name enum * Unable to use GetNetworkInterfaceByName due to NS If I use GetNetworkInterface, I need to be in the vnet NS, but that means I will need to call ExecuteInNS, which causes tests to fail. * Fixes from linter * Assume if NS exists, vlan veth exists Tests ok * Fixes for Linter * Fix delete tests * Fix delete tests bug * Go mod tidy for linting Hopefully this fixes the windows lint error * No lint on vishvananda netns Maybe this will fix the windows linter? * Build linux only for netns package Maybe this fixes the linter error? * Remove nolint to see if linter fails * Moved netns interface to caller, generalized tests Tests ok, Native ok * Typos * Reordered if statement, unwrapped arp Tests ok, ping ok, wget ok * Renamed veth, fixed logs * Made deleteEndpoints logic clearer, renamed error * Renamed eth0 to primaryHostIfName, vlanEth to vlanIf
2022-08-03 00:54:10 +03:00
}
}
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
//nolint:gocritic
defer func(client EndpointClient, contIfName string) {
// Cleanup on failure.
if err != nil {
logger.Error("CNI error. Delete Endpoint and rules that are created", zap.Error(err), zap.String("contIfName", contIfName))
if containerIf != nil {
client.DeleteEndpointRules(ep)
}
// set deleteHostVeth to true to cleanup host veth interface if created
//nolint:errcheck // ignore error
client.DeleteEndpoints(ep)
}
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
}(epClient, contIfName)
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
// wrapping endpoint client commands in anonymous func so that namespace can be exit and closed before the next loop
//nolint:wrapcheck // ignore wrap check
err = func() error {
if epErr := epClient.AddEndpoints(epInfo); epErr != nil {
return epErr
}
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.NICType == cns.InfraNIC {
var epErr error
containerIf, epErr = netioCli.GetNetworkInterfaceByName(contIfName)
if epErr != nil {
return epErr
}
ep.MacAddress = containerIf.HardwareAddr
}
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
// Setup rules for IP addresses on the container interface.
if epErr := epClient.AddEndpointRules(epInfo); epErr != nil {
return epErr
}
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 a network namespace for the container interface is specified...
if epInfo.NetNsPath != "" {
// Open the network namespace.
logger.Info("Opening netns", zap.Any("NetNsPath", epInfo.NetNsPath))
ns, epErr := nsc.OpenNamespace(epInfo.NetNsPath)
if epErr != nil {
return epErr
}
defer ns.Close()
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 epErr := epClient.MoveEndpointsToContainerNS(epInfo, ns.GetFd()); epErr != nil {
return epErr
}
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
// Enter the container network namespace.
logger.Info("Entering netns", zap.Any("NetNsPath", epInfo.NetNsPath))
if epErr := ns.Enter(); epErr != nil {
return epErr
}
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 to host network namespace.
defer func() {
logger.Info("Exiting netns", zap.Any("NetNsPath", epInfo.NetNsPath))
if epErr := ns.Exit(); epErr != nil {
logger.Error("Failed to exit netns with", zap.Error(epErr))
}
}()
}
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.IPV6Mode != "" {
// Enable ipv6 setting in container
logger.Info("Enable ipv6 setting in container.")
nuc := networkutils.NewNetworkUtils(nl, plc)
if epErr := nuc.UpdateIPV6Setting(0); epErr != nil {
return fmt.Errorf("Enable ipv6 in container failed:%w", epErr)
}
}
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 a name for the container interface is specified...
if epInfo.IfName != "" {
if epErr := epClient.SetupContainerInterfaces(epInfo); epErr != nil {
return epErr
}
}
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 epClient.ConfigureContainerInterfacesAndRoutes(epInfo)
}()
if err != nil {
return nil, err
}
}
return ep, nil
}
// deleteEndpointImpl deletes an existing endpoint from the network.
func (nw *network) deleteEndpointImpl(nl netlink.NetlinkInterface, plc platform.ExecClient, epClient EndpointClient, nioc netio.NetIOInterface, nsc NamespaceClientInterface,
iptc ipTablesClient, ep *endpoint,
) error {
// 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.
feat: Add SNAT bridge to Native, decouple SNAT bridge (#1506) * Native Endpoint Client Add Endpoints * AddEndpointRules, ConfigureContainerInterfacesAndRoutes * Changed interface names, log statements nw.extIf.Name > eth0 (eth0) eth0.vlanid > eth0.X (eth0.1) %s%s hostIfName > vnet (A1veth0) %s%s-2 contIfName > container (B1veth0) * Renaming, using lib to set ns * Namespace "path" is /var/run/netns/<NS> * Loopback set up, Remove auto kernel subnet route * Cannot set link to up if it's in another NS * Multiple containers on same VNET NS * Delete Endpoint routes on Delete * Minimizing netns usage * Moving NS Exec Code * Further minimized netns.Set usage * Moved helper methods down, drafted tests * Removed DevName from Route Info, more tests * Test existing vnet ns, delete endpoint * NetNS interface for testing * Separated tests by namespace * Endpoints delete if they cannot be moved into NS * Namespace netns tests * Added Native Client to deleteEndpointImpl * Deletion of Endpoints Impl and Tests * Cleaned code (Tests ok) * Moved mock/netns to package (Tests ok) * Fixing Netns (wip) Moved netnsinterface to consumer package (network). Removed "Netns" from "NewNetns" and "NewMockNetns" as it is unambiguous. Changed uintptr to int and casted the int to uintptr when needed later. * Using errors.Wrap for error context (wip) * Removed sentence case (wip) * Removing variable predeclaration * Removed NewNativeEndpointClient Directly instantiating struct because nothing special happens in NewNativeEndpointClient * Removed generics from ExecuteInNS * Removed uintptr from mocknetns, tests compile Forgot to remove uintptr from mocknetns * Fix tests, lint * Fixes from linter Works on VMSS * Replacing references to ethX with vlan veth * Removed unnecessary log * Removed unnecessary mac, fix tests * Mockns method name enum * Unable to use GetNetworkInterfaceByName due to NS If I use GetNetworkInterface, I need to be in the vnet NS, but that means I will need to call ExecuteInNS, which causes tests to fail. * Fixes from linter * Assume if NS exists, vlan veth exists Tests ok * Fixes for Linter * Snat refactor * Fix delete tests * Fix delete tests bug * More snat refactor * Breaking, prepping for Native Snat Delete native endpoint snat route linux to remove errors and in theory, ovs should work fine again. * Go mod tidy for linting Hopefully this fixes the windows lint error * Add fields to native endpoint client for snat * Using New() func to create Native Client Creation of the native endpoint client is too complicated to directly instantiate. * Snat defaults * Insert SNAT entry points * Native Snat error handling * Breaking, decouple ovsctl from snat Proposed Solution implementation Moved ovsctlClient.AddPortOnOVSBridge to ovs_endpoint_snatroute_linux.go. Removed ovsctlclient from NewSnatClient. Removed ovsctlClient from testing file. * Delete unecessary ovssnat files * No lint on vishvananda netns Maybe this will fix the windows linter? * Build linux only for netns package Maybe this fixes the linter error? * Remove nolint to see if linter fails * Breaking, removed bridgeName bridgeName refers to the OVS Switch I believe * If native uses snat bridge, should also get IP * Breaking, Decouple or Wrap snat route * Check to see if snat triggered * Snat behaviors specific to ovs/native * Pass the pointer Add/Delete ok * Renaming to make consts public * Breaking, moving ovs specific parts of snat to ovs * Remove enable infra vnet (Tests ok) Tested: Allow Host to NC only Allow NC to Host only Allow both Wget Ping between containers Warning: Enable snat is still hard coded to true!!! * Move add port to after exists() check * Moved netns interface to caller, generalized tests Tests ok, Native ok * Typos * Reordered if statement, unwrapped arp Tests ok, ping ok, wget ok * Linted, wrapping errors * Go fumpt entire network package * Code markers removed, clean (Tests ok) OVS & Native: - Ping between two containers same VM, no packets on bridge - Ping between two containers diff VM, no packets on bridge - Ping other container not in vnet, no packets on bridge - Ping snat to container, packets on bridge - Ping container to snat, packets on bridge - Tcpdump confirmed on azSnatBr - Deletion of containers deletes appropriate interfaces * Renamed veth, fixed logs * Made deleteEndpoints logic clearer, renamed error * Renamed eth0 to primaryHostIfName, vlanEth to vlanIf * Deleted debug log * Corrected merge (hardware addr) (Tests ok) * Renamed vlan veth to hostExtIf_vlanID, Disabled RA eth0.2 makes disable RA look for a folder eth0 and then another sub folder "2". ("eth0/2") However, it should look for a folder named "eth0.2" literally. To solve this, we change the naming scheme to use an underscore instead. (Tests ok) * Renamed Native to TransparentVlan Confirmed basic functionality on VM with correct mode * Make file updated * Create azure-windows-multitenancy-transparent-vlan.conflist * Unified snat err format * Rename to transparent-vlan * Route table support added to local netlink * Moved SNAT to end of function * Defer deleting vlan interface on failure
2022-08-10 23:50:26 +03:00
// epClient is nil only for unit test.
if epClient == nil {
//nolint:gocritic
if ep.VlanID != 0 {
epInfo := ep.getInfo()
if nw.Mode == opModeTransparentVlan {
logger.Info("Transparent vlan client")
epClient = NewTransparentVlanEndpointClient(nw, epInfo, ep.HostIfName, "", ep.VlanID, ep.LocalIP, nl, plc, nsc, iptc)
} else {
epClient = NewOVSEndpointClient(nw, epInfo, ep.HostIfName, "", ep.VlanID, ep.LocalIP, nl, ovsctl.NewOvsctl(), plc, iptc)
}
} else if nw.Mode != opModeTransparent {
epClient = NewLinuxBridgeEndpointClient(nw.extIf, ep.HostIfName, "", nw.Mode, nl, plc)
feat: Add native linux endpoint client to prep removing OVS (#1471) * Native Endpoint Client Add Endpoints * AddEndpointRules, ConfigureContainerInterfacesAndRoutes * Changed interface names, log statements nw.extIf.Name > eth0 (eth0) eth0.vlanid > eth0.X (eth0.1) %s%s hostIfName > vnet (A1veth0) %s%s-2 contIfName > container (B1veth0) * Renaming, using lib to set ns * Namespace "path" is /var/run/netns/<NS> * Loopback set up, Remove auto kernel subnet route * Cannot set link to up if it's in another NS * Multiple containers on same VNET NS * Delete Endpoint routes on Delete * Minimizing netns usage * Moving NS Exec Code * Further minimized netns.Set usage * Moved helper methods down, drafted tests * Removed DevName from Route Info, more tests * Test existing vnet ns, delete endpoint * NetNS interface for testing * Separated tests by namespace * Endpoints delete if they cannot be moved into NS * Namespace netns tests * Added Native Client to deleteEndpointImpl * Deletion of Endpoints Impl and Tests * Cleaned code (Tests ok) * Moved mock/netns to package (Tests ok) * Fixing Netns (wip) Moved netnsinterface to consumer package (network). Removed "Netns" from "NewNetns" and "NewMockNetns" as it is unambiguous. Changed uintptr to int and casted the int to uintptr when needed later. * Using errors.Wrap for error context (wip) * Removed sentence case (wip) * Removing variable predeclaration * Removed NewNativeEndpointClient Directly instantiating struct because nothing special happens in NewNativeEndpointClient * Removed generics from ExecuteInNS * Removed uintptr from mocknetns, tests compile Forgot to remove uintptr from mocknetns * Fix tests, lint * Fixes from linter Works on VMSS * Replacing references to ethX with vlan veth * Removed unnecessary log * Removed unnecessary mac, fix tests * Mockns method name enum * Unable to use GetNetworkInterfaceByName due to NS If I use GetNetworkInterface, I need to be in the vnet NS, but that means I will need to call ExecuteInNS, which causes tests to fail. * Fixes from linter * Assume if NS exists, vlan veth exists Tests ok * Fixes for Linter * Fix delete tests * Fix delete tests bug * Go mod tidy for linting Hopefully this fixes the windows lint error * No lint on vishvananda netns Maybe this will fix the windows linter? * Build linux only for netns package Maybe this fixes the linter error? * Remove nolint to see if linter fails * Moved netns interface to caller, generalized tests Tests ok, Native ok * Typos * Reordered if statement, unwrapped arp Tests ok, ping ok, wget ok * Renamed veth, fixed logs * Made deleteEndpoints logic clearer, renamed error * Renamed eth0 to primaryHostIfName, vlanEth to vlanIf
2022-08-03 00:54:10 +03:00
} else {
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 len(ep.SecondaryInterfaces) > 0 {
epClient = NewSecondaryEndpointClient(nl, nioc, plc, nsc, ep)
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
epClient.DeleteEndpointRules(ep)
//nolint:errcheck // ignore error
epClient.DeleteEndpoints(ep)
}
epClient = NewTransparentEndpointClient(nw.extIf, ep.HostIfName, "", nw.Mode, nl, nioc, plc)
feat: Add native linux endpoint client to prep removing OVS (#1471) * Native Endpoint Client Add Endpoints * AddEndpointRules, ConfigureContainerInterfacesAndRoutes * Changed interface names, log statements nw.extIf.Name > eth0 (eth0) eth0.vlanid > eth0.X (eth0.1) %s%s hostIfName > vnet (A1veth0) %s%s-2 contIfName > container (B1veth0) * Renaming, using lib to set ns * Namespace "path" is /var/run/netns/<NS> * Loopback set up, Remove auto kernel subnet route * Cannot set link to up if it's in another NS * Multiple containers on same VNET NS * Delete Endpoint routes on Delete * Minimizing netns usage * Moving NS Exec Code * Further minimized netns.Set usage * Moved helper methods down, drafted tests * Removed DevName from Route Info, more tests * Test existing vnet ns, delete endpoint * NetNS interface for testing * Separated tests by namespace * Endpoints delete if they cannot be moved into NS * Namespace netns tests * Added Native Client to deleteEndpointImpl * Deletion of Endpoints Impl and Tests * Cleaned code (Tests ok) * Moved mock/netns to package (Tests ok) * Fixing Netns (wip) Moved netnsinterface to consumer package (network). Removed "Netns" from "NewNetns" and "NewMockNetns" as it is unambiguous. Changed uintptr to int and casted the int to uintptr when needed later. * Using errors.Wrap for error context (wip) * Removed sentence case (wip) * Removing variable predeclaration * Removed NewNativeEndpointClient Directly instantiating struct because nothing special happens in NewNativeEndpointClient * Removed generics from ExecuteInNS * Removed uintptr from mocknetns, tests compile Forgot to remove uintptr from mocknetns * Fix tests, lint * Fixes from linter Works on VMSS * Replacing references to ethX with vlan veth * Removed unnecessary log * Removed unnecessary mac, fix tests * Mockns method name enum * Unable to use GetNetworkInterfaceByName due to NS If I use GetNetworkInterface, I need to be in the vnet NS, but that means I will need to call ExecuteInNS, which causes tests to fail. * Fixes from linter * Assume if NS exists, vlan veth exists Tests ok * Fixes for Linter * Fix delete tests * Fix delete tests bug * Go mod tidy for linting Hopefully this fixes the windows lint error * No lint on vishvananda netns Maybe this will fix the windows linter? * Build linux only for netns package Maybe this fixes the linter error? * Remove nolint to see if linter fails * Moved netns interface to caller, generalized tests Tests ok, Native ok * Typos * Reordered if statement, unwrapped arp Tests ok, ping ok, wget ok * Renamed veth, fixed logs * Made deleteEndpoints logic clearer, renamed error * Renamed eth0 to primaryHostIfName, vlanEth to vlanIf
2022-08-03 00:54:10 +03:00
}
}
epClient.DeleteEndpointRules(ep)
// deleteHostVeth set to false not to delete veth as CRI will remove network namespace and
// veth will get removed as part of that.
//nolint:errcheck // ignore error
epClient.DeleteEndpoints(ep)
return nil
}
// getInfoImpl returns information about the endpoint.
func (ep *endpoint) getInfoImpl(epInfo *EndpointInfo) {
}
func addRoutes(nl netlink.NetlinkInterface, netioshim netio.NetIOInterface, interfaceName string, routes []RouteInfo) error {
ifIndex := 0
for _, route := range routes {
if route.DevName != "" {
devIf, _ := netioshim.GetNetworkInterfaceByName(route.DevName)
ifIndex = devIf.Index
} else {
interfaceIf, err := netioshim.GetNetworkInterfaceByName(interfaceName)
if err != nil {
logger.Error("Interface not found with", zap.Error(err))
return fmt.Errorf("addRoutes failed: %w", err)
}
ifIndex = interfaceIf.Index
}
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
family := netlink.GetIPAddressFamily(route.Gw)
if route.Gw == nil {
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
family = netlink.GetIPAddressFamily(route.Dst.IP)
}
nlRoute := &netlink.Route{
Family: family,
Dst: &route.Dst,
Gw: route.Gw,
LinkIndex: ifIndex,
Priority: route.Priority,
Protocol: route.Protocol,
Scope: route.Scope,
feat: Add SNAT bridge to Native, decouple SNAT bridge (#1506) * Native Endpoint Client Add Endpoints * AddEndpointRules, ConfigureContainerInterfacesAndRoutes * Changed interface names, log statements nw.extIf.Name > eth0 (eth0) eth0.vlanid > eth0.X (eth0.1) %s%s hostIfName > vnet (A1veth0) %s%s-2 contIfName > container (B1veth0) * Renaming, using lib to set ns * Namespace "path" is /var/run/netns/<NS> * Loopback set up, Remove auto kernel subnet route * Cannot set link to up if it's in another NS * Multiple containers on same VNET NS * Delete Endpoint routes on Delete * Minimizing netns usage * Moving NS Exec Code * Further minimized netns.Set usage * Moved helper methods down, drafted tests * Removed DevName from Route Info, more tests * Test existing vnet ns, delete endpoint * NetNS interface for testing * Separated tests by namespace * Endpoints delete if they cannot be moved into NS * Namespace netns tests * Added Native Client to deleteEndpointImpl * Deletion of Endpoints Impl and Tests * Cleaned code (Tests ok) * Moved mock/netns to package (Tests ok) * Fixing Netns (wip) Moved netnsinterface to consumer package (network). Removed "Netns" from "NewNetns" and "NewMockNetns" as it is unambiguous. Changed uintptr to int and casted the int to uintptr when needed later. * Using errors.Wrap for error context (wip) * Removed sentence case (wip) * Removing variable predeclaration * Removed NewNativeEndpointClient Directly instantiating struct because nothing special happens in NewNativeEndpointClient * Removed generics from ExecuteInNS * Removed uintptr from mocknetns, tests compile Forgot to remove uintptr from mocknetns * Fix tests, lint * Fixes from linter Works on VMSS * Replacing references to ethX with vlan veth * Removed unnecessary log * Removed unnecessary mac, fix tests * Mockns method name enum * Unable to use GetNetworkInterfaceByName due to NS If I use GetNetworkInterface, I need to be in the vnet NS, but that means I will need to call ExecuteInNS, which causes tests to fail. * Fixes from linter * Assume if NS exists, vlan veth exists Tests ok * Fixes for Linter * Snat refactor * Fix delete tests * Fix delete tests bug * More snat refactor * Breaking, prepping for Native Snat Delete native endpoint snat route linux to remove errors and in theory, ovs should work fine again. * Go mod tidy for linting Hopefully this fixes the windows lint error * Add fields to native endpoint client for snat * Using New() func to create Native Client Creation of the native endpoint client is too complicated to directly instantiate. * Snat defaults * Insert SNAT entry points * Native Snat error handling * Breaking, decouple ovsctl from snat Proposed Solution implementation Moved ovsctlClient.AddPortOnOVSBridge to ovs_endpoint_snatroute_linux.go. Removed ovsctlclient from NewSnatClient. Removed ovsctlClient from testing file. * Delete unecessary ovssnat files * No lint on vishvananda netns Maybe this will fix the windows linter? * Build linux only for netns package Maybe this fixes the linter error? * Remove nolint to see if linter fails * Breaking, removed bridgeName bridgeName refers to the OVS Switch I believe * If native uses snat bridge, should also get IP * Breaking, Decouple or Wrap snat route * Check to see if snat triggered * Snat behaviors specific to ovs/native * Pass the pointer Add/Delete ok * Renaming to make consts public * Breaking, moving ovs specific parts of snat to ovs * Remove enable infra vnet (Tests ok) Tested: Allow Host to NC only Allow NC to Host only Allow both Wget Ping between containers Warning: Enable snat is still hard coded to true!!! * Move add port to after exists() check * Moved netns interface to caller, generalized tests Tests ok, Native ok * Typos * Reordered if statement, unwrapped arp Tests ok, ping ok, wget ok * Linted, wrapping errors * Go fumpt entire network package * Code markers removed, clean (Tests ok) OVS & Native: - Ping between two containers same VM, no packets on bridge - Ping between two containers diff VM, no packets on bridge - Ping other container not in vnet, no packets on bridge - Ping snat to container, packets on bridge - Ping container to snat, packets on bridge - Tcpdump confirmed on azSnatBr - Deletion of containers deletes appropriate interfaces * Renamed veth, fixed logs * Made deleteEndpoints logic clearer, renamed error * Renamed eth0 to primaryHostIfName, vlanEth to vlanIf * Deleted debug log * Corrected merge (hardware addr) (Tests ok) * Renamed vlan veth to hostExtIf_vlanID, Disabled RA eth0.2 makes disable RA look for a folder eth0 and then another sub folder "2". ("eth0/2") However, it should look for a folder named "eth0.2" literally. To solve this, we change the naming scheme to use an underscore instead. (Tests ok) * Renamed Native to TransparentVlan Confirmed basic functionality on VM with correct mode * Make file updated * Create azure-windows-multitenancy-transparent-vlan.conflist * Unified snat err format * Rename to transparent-vlan * Route table support added to local netlink * Moved SNAT to end of function * Defer deleting vlan interface on failure
2022-08-10 23:50:26 +03:00
Table: route.Table,
}
logger.Info("Adding IP route to link", zap.Any("route", route), zap.String("interfaceName", interfaceName))
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 := nl.AddIPRoute(nlRoute); err != nil {
if !strings.Contains(strings.ToLower(err.Error()), "file exists") {
return err
} else {
logger.Info("route already exists")
}
}
}
return nil
}
func deleteRoutes(nl netlink.NetlinkInterface, netioshim netio.NetIOInterface, interfaceName string, routes []RouteInfo) error {
ifIndex := 0
for _, route := range routes {
if route.DevName != "" {
devIf, _ := netioshim.GetNetworkInterfaceByName(route.DevName)
if devIf == nil {
logger.Info("Not deleting route. Interface doesn't exist", zap.String("interfaceName", interfaceName))
continue
}
ifIndex = devIf.Index
} else if interfaceName != "" {
interfaceIf, _ := netioshim.GetNetworkInterfaceByName(interfaceName)
if interfaceIf == nil {
logger.Info("Not deleting route. Interface doesn't exist", zap.String("interfaceName", interfaceName))
continue
}
ifIndex = interfaceIf.Index
}
family := netlink.GetIPAddressFamily(route.Gw)
if route.Gw == nil {
family = netlink.GetIPAddressFamily(route.Dst.IP)
}
nlRoute := &netlink.Route{
Family: family,
Dst: &route.Dst,
LinkIndex: ifIndex,
Gw: route.Gw,
Protocol: route.Protocol,
Scope: route.Scope,
}
logger.Info("Deleting IP route from link", zap.Any("route", route), zap.String("interfaceName", interfaceName))
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 := nl.DeleteIPRoute(nlRoute); err != nil {
return err
}
}
return nil
}
// updateEndpointImpl updates an existing endpoint in the network.
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 (nm *networkManager) updateEndpointImpl(nw *network, existingEpInfo *EndpointInfo, targetEpInfo *EndpointInfo) (*endpoint, error) {
var ep *endpoint
existingEpFromRepository := nw.Endpoints[existingEpInfo.Id]
logger.Info("[updateEndpointImpl] Going to retrieve endpoint with Id to update", zap.String("id", existingEpInfo.Id))
if existingEpFromRepository == nil {
logger.Info("[updateEndpointImpl] Endpoint cannot be updated as it does not exist")
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 nil, errEndpointNotFound
}
netns := existingEpFromRepository.NetworkNameSpace
// Network namespace for the container interface has to be specified
if netns != "" {
// Open the network namespace.
logger.Info("[updateEndpointImpl] Opening netns", zap.Any("netns", netns))
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
ns, err := nm.nsClient.OpenNamespace(netns)
if err != nil {
return nil, err
}
defer ns.Close()
// Enter the container network namespace.
logger.Info("[updateEndpointImpl] Entering netns", zap.Any("netns", netns))
if err = ns.Enter(); err != nil {
return nil, err
}
// Return to host network namespace.
defer func() {
logger.Info("[updateEndpointImpl] Exiting netns", zap.Any("netns", netns))
if err := ns.Exit(); err != nil {
logger.Error("[updateEndpointImpl] Failed to exit netns with", zap.Error(err))
}
}()
} else {
logger.Info("[updateEndpointImpl] Endpoint cannot be updated as the network namespace does not exist: Epid", zap.String("id", existingEpInfo.Id),
zap.String("component", "updateEndpointImpl"))
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 nil, errNamespaceNotFound
}
logger.Info("[updateEndpointImpl] Going to update routes in netns", zap.Any("netns", netns))
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 err := nm.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
ep.Routes = append(ep.Routes, targetEpInfo.Routes...)
return ep, nil
}
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 (nm *networkManager) updateRoutes(existingEp *EndpointInfo, targetEp *EndpointInfo) error {
logger.Info("Updating routes for the endpoint", zap.Any("existingEp", existingEp))
logger.Info("Target endpoint is", zap.Any("targetEp", 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")
logger.Info("Going to collect routes and skip default and infravnet routes if applicable.")
logger.Info("Key for default route", zap.String("route", defaultDst.String()))
infraVnetKey := ""
if targetEp.EnableInfraVnet {
infraVnetSubnet := targetEp.InfraVnetAddressSpace
if infraVnetSubnet != "" {
infraVnetKey = strings.Split(infraVnetSubnet, "/")[0]
}
}
logger.Info("Key for route to infra vnet", zap.String("infraVnetKey", infraVnetKey))
for _, route := range existingEp.Routes {
destination := route.Dst.IP.String()
logger.Info("Checking destination as to skip or not", zap.String("destination", destination))
isDefaultRoute := destination == defaultDst.String()
isInfraVnetRoute := targetEp.EnableInfraVnet && (destination == infraVnetKey)
if !isDefaultRoute && !isInfraVnetRoute {
existingRoutes[route.Dst.String()] = route
logger.Info("was skipped", zap.String("destination", 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)
logger.Info("Adding following route to the tobeDeleted list", zap.Any("existingRoute", existingRoute))
}
}
for _, targetRoute := range targetRoutes {
dst := targetRoute.Dst.String()
if _, ok := existingRoutes[dst]; !ok {
tobeAddedRoutes = append(tobeAddedRoutes, targetRoute)
logger.Info("Adding following route to the tobeAdded list", zap.Any("targetRoute", targetRoute))
}
}
err := deleteRoutes(nm.netlink, &netio.NetIO{}, existingEp.IfName, tobeDeletedRoutes)
if err != nil {
return err
}
err = addRoutes(nm.netlink, &netio.NetIO{}, existingEp.IfName, tobeAddedRoutes)
if err != nil {
return err
}
logger.Info("Successfully updated routes for the endpoint using target", zap.Any("existingEp", existingEp), zap.Any("targetEp", targetEp))
return nil
}
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
}