Example #1
0
	"fmt"
	"net"
	"os"
	"os/exec"

	"code.cloudfoundry.org/guardian/kawasaki/devices"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/vishvananda/netlink"
)

var _ = Describe("Link Management", func() {
	var (
		l    devices.Link
		name string
		intf *net.Interface
	)

	BeforeEach(func() {
		cmd, err := gexec.Start(exec.Command("sh", "-c", "mountpoint /sys || mount -t sysfs sysfs /sys"), GinkgoWriter, GinkgoWriter)
		Expect(err).ToNot(HaveOccurred())
		Eventually(cmd).Should(gexec.Exit(0))

		name = fmt.Sprintf("gdn-test-%d", GinkgoParallelNode())
		link := &netlink.GenericLink{
			LinkAttrs: netlink.LinkAttrs{Name: name},
			LinkType:  "dummy",
		}

		Expect(netlink.LinkAdd(link)).To(Succeed())
Example #2
0
func init() {
	reexec.Register("configure-container-netns", func() {
		var netNsPath, containerIntf, containerIPStr, bridgeIPStr, subnetStr string
		var mtu int

		flag.StringVar(&netNsPath, "netNsPath", "", "netNsPath")
		flag.StringVar(&containerIntf, "containerIntf", "", "containerIntf")
		flag.StringVar(&containerIPStr, "containerIP", "", "containerIP")
		flag.StringVar(&bridgeIPStr, "bridgeIP", "", "bridgeIP")
		flag.StringVar(&subnetStr, "subnet", "", "subnet")
		flag.IntVar(&mtu, "mtu", 0, "mtu")
		flag.Parse()

		fd, err := os.Open(netNsPath)
		if err != nil {
			fmt.Fprintf(os.Stderr, "opening netns `%s`: %s", netNsPath, err)
			os.Exit(1)
		}
		defer fd.Close()

		netNsExecer := &netns.Execer{}

		if err = netNsExecer.Exec(fd, func() error {
			containerIP := net.ParseIP(containerIPStr)
			bridgeIP := net.ParseIP(bridgeIPStr)
			_, subnetIPNet, err := net.ParseCIDR(subnetStr)
			if err != nil {
				panic(err)
			}

			link := devices.Link{}

			intf, found, err := link.InterfaceByName(containerIntf)
			if err != nil {
				panic(err)
			}
			if !found {
				return fmt.Errorf("interface `%s` was not found", containerIntf)
			}

			if err := link.AddIP(intf, containerIP, subnetIPNet); err != nil {
				panic(err)
			}

			if err := link.SetUp(intf); err != nil {
				panic(err)
			}

			if err := link.AddDefaultGW(intf, bridgeIP); err != nil {
				panic(err)
			}

			if err := link.SetMTU(intf, mtu); err != nil {
				panic(err)
			}

			return nil
		}); err != nil {
			fmt.Fprintf(os.Stderr, err.Error())
			os.Exit(1)
		}
	})
}