func createVethPair(handle string, wshdPID int, containerIP string, containerIPAddressMask int, containerMTU int) (string, error) {
	hostIfName := fmt.Sprintf("o-%s-0", handle)
	containerIfName := fmt.Sprintf("o-%s-1", handle)

	vethCreator := devices.VethCreator{}
	hostIf, containerIf, err := vethCreator.Create(hostIfName, containerIfName)
	if err != nil {
		if !strings.Contains(err.Error(), "Network interface already exists") {
			return "", err
		}

		hostIf, err = net.InterfaceByName(hostIfName)
		if err != nil {
			return "", err
		}

		containerIf, err = net.InterfaceByName(containerIfName)
		if err != nil && !strings.Contains(err.Error(), "no such network interface") {
			return "", err
		}
	}

	link := devices.Link{}

	if containerIf != nil {
		err = link.SetNs(containerIf, wshdPID)
		if err != nil {
			return "", err
		}
		fmt.Printf("container if: %+v\n", containerIf)
	}

	if hostIf != nil {
		fmt.Printf("host if: %+v\n", hostIf)
	}

	err = runInNamespace(handle, fmt.Sprintf("ip addr add %s/%d dev %s", containerIP, containerIPAddressMask, containerIfName))
	if err != nil && !strings.Contains(err.Error(), "RTNETLINK answers: File exists") {
		return "", err
	}

	err = runInNamespace(handle, fmt.Sprintf("ip link set %s up", containerIfName))
	if err != nil {
		return "", err
	}

	err = runInNamespace(handle, fmt.Sprintf("ip link set dev %s mtu %d", containerIfName, containerMTU))
	if err != nil {
		return "", err
	}

	return hostIfName, nil
}
Beispiel #2
0
		return e
	}

	BeforeEach(func() {
		hostName = fmt.Sprintf("doesntexist-h-%d", GinkgoParallelNode())
		containerName = fmt.Sprintf("doesntexist-c-%d", GinkgoParallelNode())
	})

	AfterEach(func() {
		Expect(cleanup(hostName)).To(Succeed())
		Expect(cleanup(containerName)).To(Succeed())
	})

	Context("when neither host already exists", func() {
		It("creates both interfaces in the host", func() {
			Expect(l(v.Create(hostName, containerName))).To(Succeed())
			Expect(net.InterfaceByName(hostName)).ToNot(BeNil())
			Expect(net.InterfaceByName(containerName)).ToNot(BeNil())
		})

		It("returns the created interfaces", func() {
			a, b, err := v.Create(hostName, containerName)
			Expect(err).ToNot(HaveOccurred())

			Expect(a).To(Equal(f(net.InterfaceByName(hostName))))
			Expect(b).To(Equal(f(net.InterfaceByName(containerName))))
		})
	})

	Context("when one of the interfaces already exists", func() {
		It("returns an error", func() {