Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Add a MAC VLAN interfaces into Docker from the host machine
package main
import (
"fmt"
"log"
"net"
"github.com/milosgajdos83/tenus"
)
func main() {
// CREATE MACVLAN HOST INTERFACE
macVlanHost, err := tenus.NewMacVlanLinkWithOptions("eth1", tenus.MacVlanOptions{Mode: "bridge", Dev: "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)
}
// CREATE MACVLAN DOCKER INTERFACE
macVlanDocker, err := tenus.NewMacVlanLinkWithOptions("eth1", tenus.MacVlanOptions{Mode: "bridge",Dev: "macvlanDckrIfc"})
if err != nil {
log.Fatal(err)
}
// PASS MACVLAN INTERFACE TO A RUNNING DOCKER BY PID
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)
}
// ALLOCATE AND SET IP FOR THE NEW DOCKER INTERFACE
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment