Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create iptables forwarding rule targeting containerlab nodes #788

Merged
merged 9 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ htmltest:
# to obtain the pushed artifact use: docker run --rm -v $(pwd):/workspace ghcr.io/deislabs/oras:v0.11.1 pull ttl.sh/<image-name>
.PHONY: ttl-push
ttl-push: build-with-podman
docker run --rm -v $$(pwd)/bin:/workspace ghcr.io/deislabs/oras:v0.11.1 push ttl.sh/clab-$$(git rev-parse --short HEAD):1d --manifest-config /dev/null:application/vnd.acme.rocket.config ./containerlab
@echo "download with: docker run --rm -v \$$(pwd):/workspace ghcr.io/deislabs/oras:v0.11.1 pull ttl.sh/clab-$$(git rev-parse --short HEAD):1d"
docker run --rm -v $$(pwd)/bin:/workspace ghcr.io/oras-project/oras:v0.12.0 push ttl.sh/clab-$$(git rev-parse --short HEAD):1d ./containerlab
@echo "download with: docker run --rm -v \$$(pwd):/workspace ghcr.io/oras-project/oras:v0.12.0 pull ttl.sh/clab-$$(git rev-parse --short HEAD):1d"
1 change: 1 addition & 0 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
NodeGroupLabel = "clab-node-group"
NodeLabDirLabel = "clab-node-lab-dir"
TopoFileLabel = "clab-topo-file"
NodeMgmtNetBr = "clab-mgmt-net-bridge"
)

// supported kinds
Expand Down
83 changes: 83 additions & 0 deletions clab/iptables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2022 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause

package clab

import (
"bytes"
"fmt"
"os/exec"
"strings"

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/utils"
)

// InstallIPTablesFwdRule calls iptables to install `allow` rule for traffic destined nodes on the clab management network
func (c *CLab) InstallIPTablesFwdRule() (err error) {
if c.GlobalRuntime().GetName() != runtime.DockerRuntime {
log.Debug("iptables forwarding rules management is not supported for this runtime")
return nil
}

mgmtNet := c.GlobalRuntime().Mgmt()
if mgmtNet.Bridge == "" {
log.Debug("skipping setup of iptables forwarding rules for non-bridged management network")
return
}

// first check if a rule already exists to not create duplicates
v4checkCmd := "iptables -vL DOCKER-USER"

res, err := exec.Command("sudo", strings.Split(v4checkCmd, " ")...).Output()
if bytes.Contains(res, []byte(mgmtNet.Bridge)) {
log.Debugf("found iptables forwarding rule targeting the bridge %q. Skipping creation of the forwarding rule.", mgmtNet.Bridge)
return err
}

if err != nil {
return err
}

v4cmd := fmt.Sprintf("iptables -I DOCKER-USER -o %s -j ACCEPT", mgmtNet.Bridge)

_, err = exec.Command("sudo", strings.Split(v4cmd, " ")...).Output()
if err != nil {
return
}

return err
}

// DeleteIPTablesFwdRule deletes `allow` rule installed with InstallIPTablesFwdRule when the bridge interface doesn't exist anymore
func (c *CLab) DeleteIPTablesFwdRule(br string) (err error) {
if c.GlobalRuntime().GetName() != runtime.DockerRuntime {
log.Debug("iptables forwarding rules management is not supported for this runtime")
return nil
}

if br == "" || br == "docker0" {
log.Warn("wat1")
log.Debug("skipping deletion of iptables forwarding rule for non-bridged or default management network")
return
}

_, err = utils.BridgeByName(br)
if err == nil {
log.Warn("here1")
// nil error means the bridge interface was found, hence we don't need to delete the fwd rule, as the bridge is still in use
return nil
}

v4cmd := fmt.Sprintf("iptables -D DOCKER-USER -o %s -j ACCEPT", br)

_, err = exec.Command("sudo", strings.Split(v4cmd, " ")...).Output()
if err != nil {
log.Warn(err)
return
}

return err
}
17 changes: 17 additions & 0 deletions clab/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clab

import "context"

func (c *CLab) CreateNetwork(ctx context.Context) error {
// create docker network or use existing one
if err := c.GlobalRuntime().CreateNet(ctx); err != nil {
return err
}

// save mgmt bridge name as a label
for _, n := range c.Nodes {
n.Config().Labels[NodeMgmtNetBr] = c.GlobalRuntime().Mgmt().Bridge
}

return nil
}
Loading