Last active
November 6, 2020 19:51
-
-
Save 0xc0d/dd5ebfa5a3a5b7adaf4ffbb2eedebac3 to your computer and use it in GitHub Desktop.
creating network for container
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
func (c *Container) SetupNetwork(bridge string) (filesystem.Unmounter, error) { | |
nsMountTarget := filepath.Join(netnsPath, c.Digest) | |
vethName := fmt.Sprintf("veth%.7s", c.Digest) | |
peerName := fmt.Sprintf("P%s", vethName) | |
if err := network.SetupVirtualEthernet(vethName, peerName); err != nil { | |
return nil, err | |
} | |
if err := network.LinkSetMaster(vethName, bridge); err != nil { | |
return nil, err | |
} | |
unmount, err := network.MountNewNetworkNamespace(nsMountTarget) | |
if err != nil { | |
return unmount, err | |
} | |
if err := network.LinkSetNsByFile(nsMountTarget, peerName); err != nil { | |
return unmount, err | |
} | |
// Change current network namespace to setup the veth | |
unset, err := network.SetNetNSByFile(nsMountTarget) | |
if err != nil { | |
return unmount, nil | |
} | |
defer unset() | |
ctrEthName := "eth0" | |
ctrEthIPAddr := c.GetIP() | |
if err := network.LinkRename(peerName, ctrEthName); err != nil { | |
return unmount, err | |
} | |
if err := network.LinkAddAddr(ctrEthName, ctrEthIPAddr); err != nil { | |
return unmount, err | |
} | |
if err := network.LinkSetup(ctrEthName); err != nil { | |
return unmount, err | |
} | |
if err := network.LinkAddGateway(ctrEthName, "172.30.0.1"); err != nil { | |
return unmount, err | |
} | |
if err := network.LinkSetup("lo"); err != nil { | |
return unmount, err | |
} | |
return unmount, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment