Last active
March 12, 2019 05:34
-
-
Save milosgajdos/296fb90d076f259a5b0a to your computer and use it in GitHub Desktop.
Add a MAC VLAN interfaces into Docker from the host machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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