})

		Context("when the interface does not exist", func() {
			It("returns the error", func() {

				ip, subnet, _ := net.ParseCIDR("1.2.3.4/5")
				Expect(l.AddIP(intf, ip, subnet)).To(Succeed())
			})
		})
	})

	Describe("AddDefaultGW", func() {
		Context("when the interface does not exist", func() {
			It("returns the error", func() {
				ip := net.ParseIP("1.2.3.4")
				Expect(l.AddDefaultGW(&net.Interface{Name: "something"}, ip)).To(MatchError("devices: Link not found"))
			})
		})
	})

	Describe("SetUp", func() {
		Context("when the interface does not exist", func() {
			It("returns an error", func() {
				Expect(l.SetUp(&net.Interface{Name: "something"})).To(MatchError("devices: Link not found"))
			})
		})

		Context("when the interface exists", func() {
			Context("and it is down", func() {
				It("should bring the interface up", func() {
					Expect(l.SetUp(intf)).To(Succeed())
Beispiel #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)
		}
	})
}