Exemple #1
0
func main() {
	// CREATE VLAN HOST INTERFACE
	vlanDocker, err := tenus.NewVlanLinkWithOptions("eth1", tenus.VlanOptions{Dev: "vlanDckr", Id: 20})
	if err != nil {
		log.Fatal(err)
	}

	// PASS VLAN INTERFACE TO A RUNNING DOCKER BY PID
	pid, err := tenus.DockerPidByName("vlandckr", "/var/run/docker.sock")
	if err != nil {
		fmt.Println(err)
	}

	if err := vlanDocker.SetLinkNetNsPid(pid); err != nil {
		log.Fatal(err)
	}

	// ALLOCATE AND SET IP FOR THE NEW DOCKER INTERFACE
	vlanDckrIp, vlanDckrIpNet, err := net.ParseCIDR("10.1.41.3/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := vlanDocker.SetLinkNetInNs(pid, vlanDckrIp, vlanDckrIpNet, nil); err != nil {
		log.Fatal(err)
	}
}
Exemple #2
0
func main() {
	macVlanHost, err := tenus.NewMacVlanLinkWithOptions("eth1", tenus.MacVlanOptions{Mode: "bridge", MacVlanDev: "macvlanHostIfc"})
	if err != nil {
		log.Fatal(err)
	}

	macVlanHostIp, macVlanHostIpNet, err := net.ParseCIDR("10.0.41.2/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := macVlanHost.SetLinkIp(macVlanHostIp, macVlanHostIpNet); err != nil {
		fmt.Println(err)
	}

	if err = macVlanHost.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	macVlanDocker, err := tenus.NewMacVlanLinkWithOptions("eth1", tenus.MacVlanOptions{Mode: "bridge", MacVlanDev: "macvlanDckrIfc"})
	if err != nil {
		log.Fatal(err)
	}

	pid, err := tenus.DockerPidByName("mcvlandckr", "/var/run/docker.sock")
	if err != nil {
		log.Fatal(err)
	}

	if err := macVlanDocker.SetLinkNetNsPid(pid); err != nil {
		log.Fatal(err)
	}

	macVlanDckrIp, macVlanDckrIpNet, err := net.ParseCIDR("10.0.41.3/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := macVlanDocker.SetLinkNetInNs(pid, macVlanDckrIp, macVlanDckrIpNet, nil); err != nil {
		log.Fatal(err)
	}
}
Exemple #3
0
func main() {
	// CREATE BRIDGE AND BRING IT UP
	br, err := tenus.NewBridgeWithName("vethbridge")
	if err != nil {
		log.Fatal(err)
	}

	brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := br.SetLinkIp(brIp, brIpNet); err != nil {
		fmt.Println(err)
	}

	if err = br.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// CREATE VETH PAIR
	veth, err := tenus.NewVethPairWithOptions("myveth01", tenus.VethOptions{PeerName: "myveth02"})
	if err != nil {
		log.Fatal(err)
	}

	// ASSIGN IP ADDRESS TO THE HOST VETH INTERFACE
	vethHostIp, vethHostIpNet, err := net.ParseCIDR("10.0.41.2/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := veth.SetLinkIp(vethHostIp, vethHostIpNet); err != nil {
		fmt.Println(err)
	}

	// ADD MYVETH01 INTERFACE TO THE MYBRIDGE BRIDGE
	myveth01, err := net.InterfaceByName("myveth01")
	if err != nil {
		log.Fatal(err)
	}

	if err = br.AddSlaveIfc(myveth01); err != nil {
		fmt.Println(err)
	}

	if err = veth.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// PASS VETH PEER INTERFACE TO A RUNNING DOCKER BY PID
	pid, err := tenus.DockerPidByName("vethdckr", "/var/run/docker.sock")
	if err != nil {
		fmt.Println(err)
	}

	if err := veth.SetPeerLinkNsPid(pid); err != nil {
		log.Fatal(err)
	}

	// ALLOCATE AND SET IP FOR THE NEW DOCKER INTERFACE
	vethGuestIp, vethGuestIpNet, err := net.ParseCIDR("10.0.41.5/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := veth.SetPeerLinkNetInNs(pid, vethGuestIp, vethGuestIpNet, nil); err != nil {
		log.Fatal(err)
	}
}