diff --git a/links/link_mgmt-net.go b/links/link_mgmt-net.go index 4d9d94f9f..f0b928aa4 100644 --- a/links/link_mgmt-net.go +++ b/links/link_mgmt-net.go @@ -1,11 +1,13 @@ package links import ( + "context" "fmt" "github.com/containernetworking/plugins/pkg/ns" log "github.com/sirupsen/logrus" "github.com/srl-labs/containerlab/utils" + "github.com/vishvananda/netlink" ) type LinkMgmtNetRaw struct { @@ -112,6 +114,29 @@ func (*mgmtBridgeLinkNode) GetLinkEndpointType() LinkEndpointType { return LinkEndpointTypeBridge } +func (b *mgmtBridgeLinkNode) AddLinkToContainer(ctx context.Context, link netlink.Link, f func(ns.NetNS) error) error { + // retrieve the namespace handle + ns, err := ns.GetCurrentNS() + if err != nil { + return err + } + + // get the bridge as netlink.Link + br, err := utils.LinkByNameOrAlias(b.shortname) + if err != nil { + return err + } + + // assign the bridge to the link as master + err = netlink.LinkSetMaster(link, br) + if err != nil { + return err + } + + // execute the given function + return ns.Do(f) +} + func getMgmtBrLinkNode() *mgmtBridgeLinkNode { if _mgmtBrLinkMgmtBrInstance == nil { currns, err := ns.GetCurrentNS() diff --git a/tests/01-smoke/16-mgmtnet.robot b/tests/01-smoke/16-mgmtnet.robot new file mode 100644 index 000000000..451e47c86 --- /dev/null +++ b/tests/01-smoke/16-mgmtnet.robot @@ -0,0 +1,45 @@ +*** Settings *** +Library OperatingSystem +Library String +Library Process +Resource ../common.robot + +Suite Setup Setup +Suite Teardown Run Keyword Teardown + + +*** Variables *** +${lab-name} mgmtnetif +${topo} ${CURDIR}/16-mgmtnetinterface.clab.yml +${runtime} docker + + +*** Test Cases *** +Deploy ${lab-name} lab + ${result} = Run Process + ... sudo -E ${CLAB_BIN} --runtime ${runtime} deploy -t ${topo} + ... shell=True + Log ${result.stdout} + Should Be Equal As Integers ${result.rc} 0 + +Check host side interface is attached to mgmt bridge and up + ${params} = Set Variable docker network inspect clab --format '{{ $opt := index .Options "com.docker.network.bridge.name"}}{{ $opt }}' + ${mgmtbrname} = Run Process ${params} + ... shell=True + Log ${mgmtbrname.stdout} + ${result} = Run Process + ... sudo -E ip link show dev l1eth1 + ... shell=True + Log ${result.stdout} + Should Be Equal As Integers ${result.rc} 0 + Should Contain ${result.stdout} state UP + Should Contain ${result.stdout} master ${mgmtbrname.stdout} + +*** Keywords *** +Teardown + # destroy all labs + Run sudo -E ${CLAB_BIN} --runtime ${runtime} destroy -c -a + +Setup + # skipping this test suite for podman for now + Skip If '${runtime}' == 'podman' diff --git a/tests/01-smoke/16-mgmtnetinterface.clab.yml b/tests/01-smoke/16-mgmtnetinterface.clab.yml new file mode 100644 index 000000000..b462ca691 --- /dev/null +++ b/tests/01-smoke/16-mgmtnetinterface.clab.yml @@ -0,0 +1,10 @@ +name: mgmtnetif + +topology: + nodes: + l1: + kind: linux + image: alpine:latest + cmd: sleep infinity + links: + - endpoints: [l1:eth1, mgmt-net:l1eth1] \ No newline at end of file