Skip to content

Commit 519aeca

Browse files
fix: Fail network creation if handleCommonOptions return error (#1063)
* handle errors in setting up routes and iptables for AKS-Swift * added netio interface in networkmanager
1 parent 943c1ae commit 519aeca

File tree

5 files changed

+24
-43
lines changed

5 files changed

+24
-43
lines changed

cni/network/network.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/Azure/azure-container-networking/common"
2020
"github.com/Azure/azure-container-networking/iptables"
2121
"github.com/Azure/azure-container-networking/log"
22+
"github.com/Azure/azure-container-networking/netio"
2223
"github.com/Azure/azure-container-networking/netlink"
2324
"github.com/Azure/azure-container-networking/network"
2425
"github.com/Azure/azure-container-networking/network/policy"
@@ -114,7 +115,7 @@ func NewPlugin(name string,
114115

115116
nl := netlink.NewNetlink()
116117
// Setup network manager.
117-
nm, err := network.NewNetworkManager(nl, platform.NewExecClient())
118+
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(), &netio.NetIO{})
118119
if err != nil {
119120
return nil, err
120121
}

cnm/network/network.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
cnsclient "github.com/Azure/azure-container-networking/cns/client"
1313
"github.com/Azure/azure-container-networking/common"
1414
"github.com/Azure/azure-container-networking/log"
15+
"github.com/Azure/azure-container-networking/netio"
1516
"github.com/Azure/azure-container-networking/netlink"
1617
"github.com/Azure/azure-container-networking/network"
1718
"github.com/Azure/azure-container-networking/platform"
@@ -52,7 +53,7 @@ func NewPlugin(config *common.PluginConfig) (NetPlugin, error) {
5253

5354
nl := netlink.NewNetlink()
5455
// Setup network manager.
55-
nm, err := network.NewNetworkManager(nl, platform.NewExecClient())
56+
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(), &netio.NetIO{})
5657
if err != nil {
5758
return nil, err
5859
}

cnms/service/networkmonitor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
cnms "github.com/Azure/azure-container-networking/cnms/cnmspackage"
1212
acn "github.com/Azure/azure-container-networking/common"
1313
"github.com/Azure/azure-container-networking/log"
14+
"github.com/Azure/azure-container-networking/netio"
1415
"github.com/Azure/azure-container-networking/netlink"
1516
"github.com/Azure/azure-container-networking/network"
1617
"github.com/Azure/azure-container-networking/platform"
@@ -148,7 +149,7 @@ func main() {
148149
}
149150

150151
nl := netlink.NewNetlink()
151-
nm, err := network.NewNetworkManager(nl, platform.NewExecClient())
152+
nm, err := network.NewNetworkManager(nl, platform.NewExecClient(), &netio.NetIO{})
152153
if err != nil {
153154
log.Printf("[monitor] Failed while creating network manager")
154155
return

network/manager.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
cnms "github.com/Azure/azure-container-networking/cnms/cnmspackage"
1212
"github.com/Azure/azure-container-networking/common"
1313
"github.com/Azure/azure-container-networking/log"
14+
"github.com/Azure/azure-container-networking/netio"
1415
"github.com/Azure/azure-container-networking/netlink"
1516
"github.com/Azure/azure-container-networking/platform"
1617
"github.com/Azure/azure-container-networking/store"
@@ -58,6 +59,7 @@ type networkManager struct {
5859
ExternalInterfaces map[string]*externalInterface
5960
store store.KeyValueStore
6061
netlink netlink.NetlinkInterface
62+
netio netio.NetIOInterface
6163
plClient platform.ExecClient
6264
sync.Mutex
6365
}
@@ -86,11 +88,12 @@ type NetworkManager interface {
8688
}
8789

8890
// Creates a new network manager.
89-
func NewNetworkManager(nl netlink.NetlinkInterface, plc platform.ExecClient) (NetworkManager, error) {
91+
func NewNetworkManager(nl netlink.NetlinkInterface, plc platform.ExecClient, netioCli netio.NetIOInterface) (NetworkManager, error) {
9092
nm := &networkManager{
9193
ExternalInterfaces: make(map[string]*externalInterface),
9294
netlink: nl,
9395
plClient: plc,
96+
netio: netioCli,
9497
}
9598

9699
return nm, nil

network/network_linux.go

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,29 @@ type route netlink.Route
5959
// NewNetworkImpl creates a new container network.
6060
func (nm *networkManager) newNetworkImpl(nwInfo *NetworkInfo, extIf *externalInterface) (*network, error) {
6161
// Connect the external interface.
62-
var vlanid int
62+
var (
63+
vlanid int
64+
ifName string
65+
)
6366
opt, _ := nwInfo.Options[genericData].(map[string]interface{})
6467
log.Printf("opt %+v options %+v", opt, nwInfo.Options)
6568

6669
switch nwInfo.Mode {
6770
case opModeTunnel:
68-
err := nm.handleCommonOptions(extIf.Name, nwInfo)
69-
if err != nil {
70-
log.Printf("tunnel handleCommonOptions failed with error %s", err.Error())
71-
}
7271
fallthrough
7372
case opModeBridge:
7473
log.Printf("create bridge")
74+
ifName = extIf.BridgeName
7575
if err := nm.connectExternalInterface(extIf, nwInfo); err != nil {
7676
return nil, err
7777
}
7878

7979
if opt != nil && opt[VlanIDKey] != nil {
8080
vlanid, _ = strconv.Atoi(opt[VlanIDKey].(string))
8181
}
82-
err := nm.handleCommonOptions(extIf.BridgeName, nwInfo)
83-
if err != nil {
84-
log.Printf("bridge handleCommonOptions failed with error %s", err.Error())
85-
}
8682
case opModeTransparent:
8783
log.Printf("Transparent mode")
84+
ifName = extIf.Name
8885
if nwInfo.IPV6Mode != "" {
8986
nu := networkutils.NewNetworkUtils(nm.netlink, nm.plClient)
9087
if err := nu.EnableIPV6Forwarding(); err != nil {
@@ -95,6 +92,12 @@ func (nm *networkManager) newNetworkImpl(nwInfo *NetworkInfo, extIf *externalInt
9592
return nil, errNetworkModeInvalid
9693
}
9794

95+
err := nm.handleCommonOptions(ifName, nwInfo)
96+
if err != nil {
97+
log.Printf("handleCommonOptions failed with error %s", err.Error())
98+
return nil, err
99+
}
100+
98101
// Create the network object.
99102
nw := &network{
100103
Id: nwInfo.Id,
@@ -109,10 +112,10 @@ func (nm *networkManager) newNetworkImpl(nwInfo *NetworkInfo, extIf *externalInt
109112
return nw, nil
110113
}
111114

112-
func (nm *networkManager) handleCommonOptions(ifname string, nwInfo *NetworkInfo) error {
115+
func (nm *networkManager) handleCommonOptions(ifName string, nwInfo *NetworkInfo) error {
113116
var err error
114117
if routes, exists := nwInfo.Options[RoutesKey]; exists {
115-
err = nm.addBridgeRoutes(ifname, routes.([]RouteInfo))
118+
err = addRoutes(nm.netlink, nm.netio, ifName, routes.([]RouteInfo))
116119
if err != nil {
117120
return err
118121
}
@@ -571,34 +574,6 @@ func (*networkManager) addToIptables(cmds []iptables.IPTableEntry) error {
571574
return nil
572575
}
573576

574-
func (nm *networkManager) addBridgeRoutes(bridgeName string, routes []RouteInfo) error {
575-
log.Printf("Adding routes...")
576-
for _, route := range routes {
577-
route.DevName = bridgeName
578-
devIf, _ := net.InterfaceByName(route.DevName)
579-
ifIndex := devIf.Index
580-
gwfamily := netlink.GetIPAddressFamily(route.Gw)
581-
582-
nlRoute := &netlink.Route{
583-
Family: gwfamily,
584-
Dst: &route.Dst,
585-
Gw: route.Gw,
586-
LinkIndex: ifIndex,
587-
}
588-
589-
if err := nm.netlink.AddIPRoute(nlRoute); err != nil {
590-
if !strings.Contains(strings.ToLower(err.Error()), "file exists") {
591-
return fmt.Errorf("Failed to add %+v to host interface with error: %v", nlRoute, err)
592-
}
593-
log.Printf("[cni-net] route already exists: dst %+v, gw %+v, interfaceName %v", nlRoute.Dst, nlRoute.Gw, route.DevName)
594-
}
595-
596-
log.Printf("[cni-net] Added route %+v", route)
597-
}
598-
599-
return nil
600-
}
601-
602577
// Add ipv6 nat gateway IP on bridge
603578
func (nm *networkManager) addIpv6NatGateway(nwInfo *NetworkInfo) error {
604579
log.Printf("[net] Adding ipv6 nat gateway on azure bridge")

0 commit comments

Comments
 (0)